[clang] [Clang] Fix the lambda context for constraint evaluation (PR #184319)
via cfe-commits
cfe-commits at lists.llvm.org
Tue Mar 3 02:58:12 PST 2026
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-clang
Author: Younan Zhang (zyn0217)
<details>
<summary>Changes</summary>
Constraint lambdas in the requires body need complete template arguments before they can be evaluated. That was connected by ImplicitConceptSpecializationDecl which is no longer created naturally after the normalization patch.
This patch fixes the bug by creating a temporary decl for that purpose. Though the temporary object should go away once we have the evaluation context track template arguments.
No release note for being a regression fix.
Fixes #<!-- -->184047
---
Full diff: https://github.com/llvm/llvm-project/pull/184319.diff
2 Files Affected:
- (modified) clang/lib/Sema/SemaConcept.cpp (+21-1)
- (modified) clang/test/SemaTemplate/concepts.cpp (+37)
``````````diff
diff --git a/clang/lib/Sema/SemaConcept.cpp b/clang/lib/Sema/SemaConcept.cpp
index 38791940247cb..09f8d1b4adde9 100644
--- a/clang/lib/Sema/SemaConcept.cpp
+++ b/clang/lib/Sema/SemaConcept.cpp
@@ -477,6 +477,8 @@ class ConstraintSatisfactionChecker {
ConstraintSatisfaction &Satisfaction;
bool BuildExpression;
+ const ConceptReference *ParentConcept = nullptr;
+
private:
ExprResult
EvaluateAtomicConstraint(const Expr *AtomicExpr,
@@ -693,7 +695,8 @@ ConstraintSatisfactionChecker::SubstitutionInTemplateArguments(
ExprResult ConstraintSatisfactionChecker::EvaluateSlow(
const AtomicConstraint &Constraint,
const MultiLevelTemplateArgumentList &MLTAL) {
- EnterExpressionEvaluationContext ConstantEvaluated(
+ std::optional<EnterExpressionEvaluationContext> EvaluationContext;
+ EvaluationContext.emplace(
S, Sema::ExpressionEvaluationContext::ConstantEvaluated,
Sema::ReuseLambdaContextDecl);
@@ -705,6 +708,21 @@ ExprResult ConstraintSatisfactionChecker::EvaluateSlow(
return ExprEmpty();
}
+ // This is dumb: generic lambdas inside requires body require a lambda context
+ // decl from which to fetch correct template arguments. But we don't have any
+ // proper decls because the constraints are already normalized.
+ if (ParentConcept && SubstitutedArgs->getNumSubstitutedLevels()) {
+ auto *CD = ParentConcept->getNamedConcept();
+ // FIXME: the evaluation context should learn to track template arguments
+ // separately from a Decl.
+ EvaluationContext.emplace(
+ S, Sema::ExpressionEvaluationContext::ConstantEvaluated,
+ /*LambdaContextDecl=*/
+ ImplicitConceptSpecializationDecl::Create(
+ S.Context, CD->getDeclContext(), CD->getBeginLoc(),
+ SubstitutedArgs->getOutermost()));
+ }
+
Sema::ArgPackSubstIndexRAII SubstIndex(S, PackSubstitutionIndex);
ExprResult SubstitutedAtomicExpr = EvaluateAtomicConstraint(
Constraint.getConstraintExpr(), *SubstitutedArgs);
@@ -1027,6 +1045,8 @@ ExprResult ConstraintSatisfactionChecker::Evaluate(
if (InstTemplate.isInvalid())
return ExprError();
+ llvm::SaveAndRestore PushConceptDecl(ParentConcept, ConceptId);
+
unsigned Size = Satisfaction.Details.size();
ExprResult E = Evaluate(Constraint.getNormalizedConstraint(), MLTAL);
diff --git a/clang/test/SemaTemplate/concepts.cpp b/clang/test/SemaTemplate/concepts.cpp
index 1d2ada3e0a398..321b69f79f2c8 100644
--- a/clang/test/SemaTemplate/concepts.cpp
+++ b/clang/test/SemaTemplate/concepts.cpp
@@ -1704,6 +1704,43 @@ struct ice_point : relative_point_origin<point<kelvin>> {};
}
+namespace GH184047 {
+
+template <typename T, int N>
+concept decomposable = requires {
+ []<template <typename...> class U, typename... Args> // #decomposable_lambda
+ requires(sizeof...(Args) >= N)
+ (U<Args...>*) {}(static_cast<T*>(nullptr));
+};
+
+template<typename T>
+struct foo {};
+
+template<typename T>
+concept decomposable_fails = decomposable<T, 2>; // #decomposable_fails
+
+template<typename T>
+concept decomposable_works = requires {
+ requires decomposable<T, 1>;
+};
+
+static_assert(decomposable<foo<int>, 1>);
+
+static_assert(decomposable<foo<int>, 200>);
+// expected-error at -1 {{static assertion failed}}
+// expected-note at -2 {{evaluated to false}}
+// expected-note@#decomposable_lambda {{invalid}}
+
+static_assert(decomposable_works<foo<int>>);
+
+static_assert(decomposable_fails<foo<int>>);
+// expected-error at -1 {{static assertion failed}}
+// expected-note at -2 {{'foo<int>' does not satisfy 'decomposable_fails'}}
+// expected-note@#decomposable_fails {{evaluated to false}}
+// expected-note@#decomposable_lambda {{invalid}}
+
+}
+
namespace GH182344 {
template <typename T>
``````````
</details>
https://github.com/llvm/llvm-project/pull/184319
More information about the cfe-commits
mailing list