https://github.com/ebinjose02 created https://github.com/llvm/llvm-project/pull/169876
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. >From d9bf88bc2ef7604e8b96bb5eb949281c31ce07e4 Mon Sep 17 00:00:00 2001 From: ebinjose02 <[email protected]> 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 | 10 +++++++++- clang/test/SemaCXX/requires-nested-non-constant.cpp | 11 +++++++++++ 3 files changed, 22 insertions(+), 1 deletion(-) 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..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}} + }; + }(); + } +}; _______________________________________________ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
