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] Implement CWG2517 Useless restriction on use of parameter in constraint-expression #132919

Open
wants to merge 10 commits into
base: main
Choose a base branch
from

Conversation

imdj
Copy link
Contributor

@imdj imdj commented Mar 25, 2025

Remove [expr.prim.req.nested] check which restrict that local parameters in constraint-expressions can only appear as unevaluated operands. This change makes the treatment of examples like requires expressions and other constant expression contexts uniform, consistent with the adoption of P2280.

References:

@imdj imdj marked this pull request as ready for review March 25, 2025 11:33
@llvmbot llvmbot added clang Clang issues not falling into any other category clang:frontend Language frontend issues, e.g. anything involving "Sema" labels Mar 25, 2025
@llvmbot
Copy link
Member

llvmbot commented Mar 25, 2025

@llvm/pr-subscribers-clang

Author: Imad Aldij (imdj)

Changes

Remove [expr.prim.req.nested] check which restrict that local parameters in constraint-expressions can only appear as unevaluated operands. This change makes the treatment of examples like requires expressions and other constant expression contexts uniform, consistent with the adoption of P2280.

References:


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

2 Files Affected:

  • (modified) clang/lib/Sema/SemaExpr.cpp (-11)
  • (modified) clang/test/SemaCXX/constant-expression-p2280r4.cpp (+13)
diff --git a/clang/lib/Sema/SemaExpr.cpp b/clang/lib/Sema/SemaExpr.cpp
index 3af6d6c23438f..fa8eeb644c179 100644
--- a/clang/lib/Sema/SemaExpr.cpp
+++ b/clang/lib/Sema/SemaExpr.cpp
@@ -396,17 +396,6 @@ bool Sema::DiagnoseUseOfDecl(NamedDecl *D, ArrayRef<SourceLocation> Locs,
           targetDiag(*Locs.begin(), diag::err_thread_unsupported);
   }
 
-  if (isa<ParmVarDecl>(D) && isa<RequiresExprBodyDecl>(D->getDeclContext()) &&
-      !isUnevaluatedContext()) {
-    // C++ [expr.prim.req.nested] p3
-    //   A local parameter shall only appear as an unevaluated operand
-    //   (Clause 8) within the constraint-expression.
-    Diag(Loc, diag::err_requires_expr_parameter_referenced_in_evaluated_context)
-        << D;
-    Diag(D->getLocation(), diag::note_entity_declared_at) << D;
-    return true;
-  }
-
   return false;
 }
 
diff --git a/clang/test/SemaCXX/constant-expression-p2280r4.cpp b/clang/test/SemaCXX/constant-expression-p2280r4.cpp
index f22430a0e88a2..9baedd9c6c263 100644
--- a/clang/test/SemaCXX/constant-expression-p2280r4.cpp
+++ b/clang/test/SemaCXX/constant-expression-p2280r4.cpp
@@ -155,6 +155,19 @@ int g() {
 }
 }
 
+namespace GH132825 {
+template<typename ArrayType> concept LargeArray =
+    requires (ArrayType my_array) { requires my_array.size() > 5; };
+
+struct Big {
+  constexpr int size() const { return 100; }
+};
+
+void g() {
+  static_assert(LargeArray<Big>);
+}
+}
+
 namespace GH128409 {
   int &ff();
   int &x = ff(); // nointerpreter-note {{declared here}}

@zyn0217 zyn0217 requested a review from Endilll March 25, 2025 11:43
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 for the prompt patch.

@imdj imdj marked this pull request as draft March 25, 2025 12:15
@imdj imdj marked this pull request as ready for review March 25, 2025 13:23
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.

Thanks for working on this

Comment on lines +38 to +40
concept LargeArray = requires (ArrayType my_array) {
requires my_array.size() > 5;
};
Copy link
Contributor

Choose a reason for hiding this comment

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

Do we have a test for the cases where the expression is not a constant expression?

Copy link
Contributor Author

@imdj imdj Mar 27, 2025

Choose a reason for hiding this comment

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

Would this be covered by the tests we updated in nested-requirement.cpp? Please let me know what you think or your suggestions since I'm not very experienced here.

template<typename T>
concept C2 = requires (T a) {
requires sizeof(a) == 4; // OK
requires a == 0; // expected-error{{substitution into constraint expression resulted in a non-constant expression}}
// expected-note@-1{{while checking the satisfaction of nested requirement requested here}}
// expected-note@-2{{in instantiation of requirement here}}
// expected-note@-3{{while checking the satisfaction of nested requirement requested here}}
// expected-note@-6{{while substituting template arguments into constraint expression here}}
// expected-note@-5{{function parameter 'a' with unknown value cannot be used in a constant expression}}
// expected-note@-8{{declared here}}
};
static_assert(C2<int>); // expected-error{{static assertion failed}}
// expected-note@-1{{while checking the satisfaction of concept 'C2<int>' requested here}}
// expected-note@-2{{because 'int' does not satisfy 'C2'}}

@Endilll
Copy link
Contributor

Endilll commented Mar 25, 2025

If you update your branch with changes from main, unrelated changes in cxx_dr_status.html will disappear.

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.

CC @mizvekov since it was suggested this is related to #126550 and it looked like based on the conversation there, there was some existing work going on.

@zyn0217
Copy link
Contributor

zyn0217 commented Mar 25, 2025

CC @mizvekov since it was suggested this is related to #126550 and it looked like based on the conversation there, there was some existing work going on.

I think the crash I presented is independent of this DR. (This DR makes that the parameters are usable within an unevaluated requires context) while that crash has to do with the lambda context, which is what Mizvekov has been working on.

@mizvekov
Copy link
Contributor

Yeah I agree with @zyn0217 here. Otherwise I haven't been able to take a look at the crash yet.

@cor3ntin cor3ntin requested a review from AaronBallman March 27, 2025 15:36
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.

8 participants