Skip to content

Conversation

@ebinjose02
Copy link

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.

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.
@github-actions
Copy link

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 @ followed by their GitHub username.

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.

@llvmbot llvmbot added clang Clang issues not falling into any other category clang:frontend Language frontend issues, e.g. anything involving "Sema" labels Nov 28, 2025
@llvmbot
Copy link
Member

llvmbot commented Nov 28, 2025

@llvm/pr-subscribers-clang

Author: Ebin Jose (ebinjose02)

Changes

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.


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

3 Files Affected:

  • (modified) clang/include/clang/Basic/DiagnosticSemaKinds.td (+2)
  • (modified) clang/lib/Sema/SemaExprCXX.cpp (+9-1)
  • (added) clang/test/SemaCXX/requires-nested-non-constant.cpp (+11)
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}}
+      };
+    }();
+  }
+};

@zwuis zwuis requested review from cor3ntin and zyn0217 November 28, 2025 15:00
@github-actions
Copy link

⚠️ C/C++ code formatter, clang-format found issues in your code. ⚠️

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

⚠️
The reproduction instructions above might return results for more than one PR
in a stack if you are using a stacked PR workflow. You can limit the results by
changing origin/main to the base branch/commit you want to compare against.
⚠️

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=*/{},

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 requires keyword in expression context

2 participants