Skip to content

Conversation

@ojhunt
Copy link
Contributor

@ojhunt ojhunt commented Jul 10, 2025

Make sure to mark a concept decl as being invalid if the constraint is invalid.

Fixes #138823

@ojhunt ojhunt requested a review from cor3ntin July 10, 2025 11:11
@ojhunt ojhunt self-assigned this Jul 10, 2025
@llvmbot llvmbot added clang Clang issues not falling into any other category clang:frontend Language frontend issues, e.g. anything involving "Sema" labels Jul 10, 2025
@llvmbot
Copy link
Member

llvmbot commented Jul 10, 2025

@llvm/pr-subscribers-clang

Author: Oliver Hunt (ojhunt)

Changes

Make sure to mark a concept decl as being invalid if the constraint is invalid.


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

2 Files Affected:

  • (modified) clang/lib/Sema/SemaTemplate.cpp (+3-1)
  • (added) clang/test/SemaCXX/concept-diagnose-unexpanded-pack.cpp (+13)
diff --git a/clang/lib/Sema/SemaTemplate.cpp b/clang/lib/Sema/SemaTemplate.cpp
index 1a98b3583185e..b76619fc50268 100644
--- a/clang/lib/Sema/SemaTemplate.cpp
+++ b/clang/lib/Sema/SemaTemplate.cpp
@@ -8968,8 +8968,10 @@ Sema::ActOnFinishConceptDefinition(Scope *S, ConceptDecl *C,
                                    Expr *ConstraintExpr,
                                    const ParsedAttributesView &Attrs) {
   assert(!C->hasDefinition() && "Concept already defined");
-  if (DiagnoseUnexpandedParameterPack(ConstraintExpr))
+  if (DiagnoseUnexpandedParameterPack(ConstraintExpr)) {
+    C->setInvalidDecl();
     return nullptr;
+  }
   C->setDefinition(ConstraintExpr);
   ProcessDeclAttributeList(S, C, Attrs);
 
diff --git a/clang/test/SemaCXX/concept-diagnose-unexpanded-pack.cpp b/clang/test/SemaCXX/concept-diagnose-unexpanded-pack.cpp
new file mode 100644
index 0000000000000..549801f04dac3
--- /dev/null
+++ b/clang/test/SemaCXX/concept-diagnose-unexpanded-pack.cpp
@@ -0,0 +1,13 @@
+// RUN: %clang_cc1 -fsyntax-only -std=c++20 -verify %s
+
+template <typename T> void foo();
+template <class... Ts>
+concept ConceptA = requires { foo<Ts>(); };
+// expected-error@-1 {{expression contains unexpanded parameter pack 'Ts'}}
+
+template <class>
+concept ConceptB = ConceptA<int>;
+
+template <ConceptB Foo> void bar(Foo);
+
+void test() { bar(1); }

@cor3ntin cor3ntin changed the title [clang] Fixes #138823 [Clang] Mark a concept as being invalid if the constraint is invalid Jul 10, 2025
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, thanks!

Comment on lines 1 to 13
// RUN: %clang_cc1 -fsyntax-only -std=c++20 -verify %s

template <typename T> void foo();
template <class... Ts>
concept ConceptA = requires { foo<Ts>(); };
// expected-error@-1 {{expression contains unexpanded parameter pack 'Ts'}}

template <class>
concept ConceptB = ConceptA<int>;

template <ConceptB Foo> void bar(Foo);

void test() { bar(1); }
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 can put that in an existing file, test/SemaCXX/concept-crash-on-diagnostic.cpp seems appropriate (in a GH138823 namespace)

Make sure to mark a concept decl as being invalid if the constraint is
invalid.
@ojhunt ojhunt force-pushed the users/ojhunt/pr-138823 branch from 8beb2ee to 5f8556c Compare July 11, 2025 00:32
@ojhunt ojhunt merged commit 34a1daa into main Jul 11, 2025
6 of 8 checks passed
@ojhunt ojhunt deleted the users/ojhunt/pr-138823 branch July 11, 2025 00:33
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.

I think this should have had a release note

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.

ICE with concept pack expansion + std::swap in C++20 constraint expressions with -std=c++20 since clang 20

5 participants