Skip to content

[Concepts] Avoid substituting into constraints for invalid TemplateDecls #75697

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
Jul 17, 2024

Conversation

zyn0217
Copy link
Contributor

@zyn0217 zyn0217 commented Dec 16, 2023

Fixes #73885.

Substituting into constraints for invalid TemplateDecls might still yield dependent expressions and end up crashing later while attempting evaluation.

Fixes llvm#73885.

Substituting into constraints for invalid TemplateDecls might still
yield dependent expressions and end up crashing later while
attempting evaluation.
@zyn0217 zyn0217 added the clang:frontend Language frontend issues, e.g. anything involving "Sema" label Dec 16, 2023
@llvmbot llvmbot added the clang Clang issues not falling into any other category label Dec 16, 2023
@llvmbot
Copy link
Member

llvmbot commented Dec 16, 2023

@llvm/pr-subscribers-clang

Author: Younan Zhang (zyn0217)

Changes

Fixes #73885.

Substituting into constraints for invalid TemplateDecls might still yield dependent expressions and end up crashing later while attempting evaluation.


Full diff: https://github.com/llvm/llvm-project/pull/75697.diff

3 Files Affected:

  • (modified) clang/docs/ReleaseNotes.rst (+3)
  • (modified) clang/lib/Sema/SemaConcept.cpp (+4)
  • (modified) clang/test/SemaTemplate/instantiate-requires-expr.cpp (+10)
diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index b4b5352a306c1c..83f98b97925250 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -831,6 +831,9 @@ Bug Fixes to C++ Support
 - Fix crash when parsing nested requirement. Fixes:
   (`#73112 <https://github.com/llvm/llvm-project/issues/73112>`_)
 
+- Fixed a crash when substituting into constraint expressions for invalid variable templates.
+  Fixes: (`#73885 <https://github.com/llvm/llvm-project/issues/73885>`_)
+
 Bug Fixes to AST Handling
 ^^^^^^^^^^^^^^^^^^^^^^^^^
 - Fixed an import failure of recursive friend class template.
diff --git a/clang/lib/Sema/SemaConcept.cpp b/clang/lib/Sema/SemaConcept.cpp
index 719c6aab74e017..73683fee7c1487 100644
--- a/clang/lib/Sema/SemaConcept.cpp
+++ b/clang/lib/Sema/SemaConcept.cpp
@@ -353,6 +353,10 @@ static ExprResult calculateConstraintSatisfaction(
           if (Inst.isInvalid())
             return ExprError();
 
+          // An empty expression for substitution failure messages.
+          if (Template && Template->isInvalidDecl())
+            return ExprEmpty();
+
           llvm::FoldingSetNodeID ID;
           if (Template &&
               DiagRecursiveConstraintEval(S, ID, Template, AtomicExpr, MLTAL)) {
diff --git a/clang/test/SemaTemplate/instantiate-requires-expr.cpp b/clang/test/SemaTemplate/instantiate-requires-expr.cpp
index ba82fc1313fc95..fb4127182d1cb6 100644
--- a/clang/test/SemaTemplate/instantiate-requires-expr.cpp
+++ b/clang/test/SemaTemplate/instantiate-requires-expr.cpp
@@ -227,3 +227,13 @@ struct r6 {};
 
 using r6i = r6<int>;
 // expected-error@-1 {{constraints not satisfied for class template 'r6' [with T = int]}}
+
+namespace GH73885 {
+template <typename T> // expected-error {{extraneous template parameter list}}
+template <typename T> // expected-error {{'T' shadows template parameter}}
+                      // expected-note@-2 {{template parameter is declared here}}
+requires(T{})
+T e_v;
+double e = e_v<double>;
+// expected-error@-1 {{constraints not satisfied for variable template 'e_v' [with T = double]}}
+}

@cor3ntin
Copy link
Contributor

@zyn0217 This fell under the cracks, I'm sorry.
I think this is good enough as is

@zyn0217
Copy link
Contributor Author

zyn0217 commented Jul 12, 2024

Hmmm, actually I'm thinking about whether we can move the check to somewhere in Parser so that these don't end up in the evaluation function.

@zyn0217
Copy link
Contributor Author

zyn0217 commented Jul 17, 2024

@cor3ntin I moved the check to CheckConstraintSatisfaction so we don't end up with many substitution failure notes. Can you check it out again?

Copy link
Contributor

@cor3ntin cor3ntin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM modulo comments

Comment on lines 240 to 242
template <> // expected-warning {{extraneous}}
template <class T> requires(T{true})
constexpr bool e = true;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Really?
This is... wild. Is that intentional?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is brought by 6591149, which I think is inappropriate nowadays but we probably need to keep it for ... historical reasons?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should remove test that is unrelated to the issue.
Would you mind creating a new issue for this behavior? Clang is being peculiar https://compiler-explorer.com/z/TcPGjWMGM - I think we should reconsider

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I'd love to. Wait a minute.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@zyn0217
Copy link
Contributor Author

zyn0217 commented Jul 17, 2024

(And thanks for the review.)

@zyn0217 zyn0217 merged commit 440fffa into llvm:main Jul 17, 2024
6 of 7 checks passed
@zyn0217 zyn0217 deleted the issue-73885 branch July 17, 2024 11:17
yuxuanchen1997 pushed a commit that referenced this pull request Jul 25, 2024
…plateDecls (#75697)

Summary:
Fixes #73885.

Substituting into constraints for invalid TemplateDecls might still
yield dependent expressions and end up crashing later in evaluation.

Test Plan: 

Reviewers: 

Subscribers: 

Tasks: 

Tags: 


Differential Revision: https://phabricator.intern.facebook.com/D60250965
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
clang:frontend Language frontend issues, e.g. anything involving "Sema" clang Clang issues not falling into any other category
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[clang] crash on invalid code involving constrained variable template
3 participants