[clang] [CLANG] Fixes the crash on the use of nested requirements in require expressions (PR #169876)
Ebin Jose via cfe-commits
cfe-commits at lists.llvm.org
Sun Nov 30 20:57:34 PST 2025
https://github.com/ebinjose02 updated https://github.com/llvm/llvm-project/pull/169876
>From af9b88da83a7f2954a3a8ddc5f4796f2017ceaec Mon Sep 17 00:00:00 2001
From: ebinjose02 <ebin371 at gmail.com>
Date: Fri, 28 Nov 2025 06:46:40 +0000
Subject: [PATCH] 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.
---
clang/include/clang/Basic/DiagnosticSemaKinds.td | 2 ++
clang/lib/Sema/SemaExprCXX.cpp | 8 ++++++++
clang/test/SemaCXX/requires-nested-non-constant.cpp | 11 +++++++++++
3 files changed, 21 insertions(+)
create mode 100644 clang/test/SemaCXX/requires-nested-non-constant.cpp
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..490ce09e75208 100644
--- a/clang/lib/Sema/SemaExprCXX.cpp
+++ b/clang/lib/Sema/SemaExprCXX.cpp
@@ -7922,6 +7922,14 @@ concepts::Requirement *Sema::ActOnNestedRequirement(Expr *Constraint) {
concepts::NestedRequirement *
Sema::BuildNestedRequirement(Expr *Constraint) {
+ 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),
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}}
+ };
+ }();
+ }
+};
More information about the cfe-commits
mailing list