llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT--> @llvm/pr-subscribers-clang Author: Ebin Jose (ebinjose02) <details> <summary>Changes</summary> 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 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}} + }; + }(); + } +}; `````````` </details> https://github.com/llvm/llvm-project/pull/169876 _______________________________________________ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
