-
Notifications
You must be signed in to change notification settings - Fork 15.3k
[CLANG] Fixes the crash on the use of nested requirements in require expressions #169876
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
base: main
Are you sure you want to change the base?
Conversation
Nested requirements in requires-expressions must be constant expressions. Previously, using an invented parameter in a nested requirement caused a crash. Now emit a clear diagnostic and recover.
|
Thank you for submitting a Pull Request (PR) to the LLVM Project! This PR will be automatically labeled and the relevant teams will be notified. If you wish to, you can add reviewers by using the "Reviewers" section on this page. If this is not working for you, it is probably because you do not have write permissions for the repository. In which case you can instead tag reviewers by name in a comment by using If you have received no comments on your PR for a week, you can request a review by "ping"ing the PR by adding a comment “Ping”. The common courtesy "ping" rate is once a week. Please remember that you are asking for valuable time from other developers. If you have further questions, they may be answered by the LLVM GitHub User Guide. You can also ask questions in a comment on this PR, on the LLVM Discord or on the forums. |
|
@llvm/pr-subscribers-clang Author: Ebin Jose (ebinjose02) ChangesFixes #165386 Full diff: https://github.com/llvm/llvm-project/pull/169876.diff 3 Files Affected:
diff --git a/clang/include/clang/Basic/DiagnosticSemaKinds.td b/clang/include/clang/Basic/DiagnosticSemaKinds.td
index 4a145fd71eedd..8083fde95f2ab 100644
--- a/clang/include/clang/Basic/DiagnosticSemaKinds.td
+++ b/clang/include/clang/Basic/DiagnosticSemaKinds.td
@@ -3190,6 +3190,8 @@ def note_ambiguous_atomic_constraints_similar_expression : Note<
def err_unsupported_placeholder_constraint : Error<
"constrained placeholder types other than simple 'auto' on non-type template "
"parameters not supported yet">;
+def err_nested_requirement_not_constant : Error<
+ "nested requirement is not a constant expression">;
def err_template_different_requires_clause : Error<
"requires clause differs in template redeclaration">;
diff --git a/clang/lib/Sema/SemaExprCXX.cpp b/clang/lib/Sema/SemaExprCXX.cpp
index d6f70e728be29..c8c36fce846a8 100644
--- a/clang/lib/Sema/SemaExprCXX.cpp
+++ b/clang/lib/Sema/SemaExprCXX.cpp
@@ -7922,7 +7922,15 @@ concepts::Requirement *Sema::ActOnNestedRequirement(Expr *Constraint) {
concepts::NestedRequirement *
Sema::BuildNestedRequirement(Expr *Constraint) {
- ConstraintSatisfaction Satisfaction;
+ if (!Constraint->isValueDependent() &&
+ !Constraint->isInstantiationDependent()) {
+ Expr::EvalResult Result;
+ if (!Constraint->EvaluateAsConstantExpr(Result, Context)) {
+ Diag(Constraint->getExprLoc(), diag::err_nested_requirement_not_constant);
+ return nullptr;
+ }
+ }
+ ConstraintSatisfaction Satisfaction;
if (!Constraint->isInstantiationDependent() &&
CheckConstraintSatisfaction(nullptr, AssociatedConstraint(Constraint),
/*TemplateArgs=*/{},
diff --git a/clang/test/SemaCXX/requires-nested-non-constant.cpp b/clang/test/SemaCXX/requires-nested-non-constant.cpp
new file mode 100644
index 0000000000000..acb516392e616
--- /dev/null
+++ b/clang/test/SemaCXX/requires-nested-non-constant.cpp
@@ -0,0 +1,11 @@
+// RUN: %clang_cc1 -std=c++23 -fsyntax-only -verify %s
+
+template <class C> class A {
+ void f() {
+ auto result = []() constexpr {
+ return requires (int x) {
+ requires (x > 0) && (x < 10); // expected-error {{nested requirement is not a constant expression}}
+ };
+ }();
+ }
+};
|
You can test this locally with the following command:git-clang-format --diff origin/main HEAD --extensions cpp -- clang/test/SemaCXX/requires-nested-non-constant.cpp clang/lib/Sema/SemaExprCXX.cpp --diff_from_common_commit
View the diff from clang-format here.diff --git a/clang/lib/Sema/SemaExprCXX.cpp b/clang/lib/Sema/SemaExprCXX.cpp
index c8c36fce8..490ce09e7 100644
--- a/clang/lib/Sema/SemaExprCXX.cpp
+++ b/clang/lib/Sema/SemaExprCXX.cpp
@@ -7930,7 +7930,7 @@ Sema::BuildNestedRequirement(Expr *Constraint) {
return nullptr;
}
}
- ConstraintSatisfaction Satisfaction;
+ ConstraintSatisfaction Satisfaction;
if (!Constraint->isInstantiationDependent() &&
CheckConstraintSatisfaction(nullptr, AssociatedConstraint(Constraint),
/*TemplateArgs=*/{},
|
Fixes #165386
Nested requirements in requires-expressions must be constant expressions. Previously, using an invented parameter in a nested requirement caused a crash. Now emit a clear diagnostic and recover.