Skip to content
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

[clang] Bailout when the substitution of template parameter mapping is invalid. #86869

Merged
merged 1 commit into from
Mar 28, 2024

Conversation

hokein
Copy link
Collaborator

@hokein hokein commented Mar 27, 2024

Fixes #86757

We missed to handle the invalid case when substituting into the parameter mapping of an constraint during normalization.
The constructor of InstantiatingTemplate will bail out (no CodeSynthesisContext will be added to the instantiation stack) if there was a fatal error, consequently we should stop doing any further template instantiations.

invalid.

Fixes llvm#86757

We missed to handle the invalid case when substituting into the parameter
mapping of an constraint during normalization.
@hokein hokein requested review from shafik, Sirraide and zyn0217 March 27, 2024 20:42
@llvmbot llvmbot added clang Clang issues not falling into any other category clang:frontend Language frontend issues, e.g. anything involving "Sema" labels Mar 27, 2024
@llvmbot
Copy link
Member

llvmbot commented Mar 27, 2024

@llvm/pr-subscribers-clang

Author: Haojian Wu (hokein)

Changes

Fixes #86757

We missed to handle the invalid case when substituting into the parameter mapping of an constraint during normalization.


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

3 Files Affected:

  • (modified) clang/docs/ReleaseNotes.rst (+1)
  • (modified) clang/lib/Sema/SemaConcept.cpp (+2)
  • (added) clang/test/SemaTemplate/concepts-GH86757.cpp (+13)
diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 0fdd9e3fb3eee2..1411e15a649ae0 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -455,6 +455,7 @@ Bug Fixes to C++ Support
 - Fix an issue where a namespace alias could be defined using a qualified name (all name components
   following the first `::` were ignored).
 - Fix an out-of-bounds crash when checking the validity of template partial specializations. (part of #GH86757).
+- Fix an issue caused by not handling invalid cases when substituting into the parameter mapping of a constraint. Fixes (#GH86757).
 
 Bug Fixes to AST Handling
 ^^^^^^^^^^^^^^^^^^^^^^^^^
diff --git a/clang/lib/Sema/SemaConcept.cpp b/clang/lib/Sema/SemaConcept.cpp
index b6c4d3d540ef50..a2d8ba9a96d7a4 100644
--- a/clang/lib/Sema/SemaConcept.cpp
+++ b/clang/lib/Sema/SemaConcept.cpp
@@ -1281,6 +1281,8 @@ substituteParameterMappings(Sema &S, NormalizedConstraint &N,
       S, InstLocBegin,
       Sema::InstantiatingTemplate::ParameterMappingSubstitution{}, Concept,
       {InstLocBegin, InstLocEnd});
+  if (Inst.isInvalid())
+    return true;
   if (S.SubstTemplateArguments(*Atomic.ParameterMapping, MLTAL, SubstArgs))
     return true;
 
diff --git a/clang/test/SemaTemplate/concepts-GH86757.cpp b/clang/test/SemaTemplate/concepts-GH86757.cpp
new file mode 100644
index 00000000000000..3122381b20359e
--- /dev/null
+++ b/clang/test/SemaTemplate/concepts-GH86757.cpp
@@ -0,0 +1,13 @@
+// RUN: %clang_cc1 -std=c++20 -Wfatal-errors -verify %s
+
+template <typename> int a;
+template <typename... b> concept c = a<b...>;
+template <typename> concept e = c<>;
+
+// must be a fatal error to trigger the crash
+undefined; // expected-error {{a type specifier is required for all declarations}}
+
+template <typename d> concept g = e<d>;
+template <g> struct h
+template <g d>
+struct h<d>;

Copy link
Member

@Sirraide Sirraide left a comment

Choose a reason for hiding this comment

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

LGTM

@Sirraide
Copy link
Member

Side note: Now it’s starting to make sense why something like an unknown include was triggering this otherwise entirely unrelated failure: The first thing that the constructor of InstantiatingTemplate does is bail out if there was a fatal error.

Copy link
Contributor

@zyn0217 zyn0217 left a comment

Choose a reason for hiding this comment

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

Thanks! This reminds me that I should also check the validity of InstantiatingTemplate after introducing a new CodeSynthesisContext in my patch of fixing lambda calls within type alias decls.

Copy link
Collaborator

@shafik shafik left a comment

Choose a reason for hiding this comment

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

LGTM, interesting it looks like we don't do this check in fromContraintExpr either.

@hokein hokein merged commit a042fcb into llvm:main Mar 28, 2024
8 checks passed
@hokein
Copy link
Collaborator Author

hokein commented Mar 28, 2024

LGTM, interesting it looks like we don't do this check in fromContraintExpr either.

Yes, I think it is an oversight, we should do the check for it as well.

hokein added a commit to hokein/llvm-project that referenced this pull request Mar 28, 2024
This is an oversight spot in llvm#86869, we should always check the invalid bit after
constructing the `Sema::InstantiatingTemplate` RAII object.
hokein added a commit that referenced this pull request Mar 28, 2024
#86943)

This is an oversight spot in #86869, we should always check the invalid
bit after constructing the `Sema::InstantiatingTemplate` RAII object.
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] Assertion when including a header that does not exist
5 participants