Author: Younan Zhang Date: 2026-03-04T21:25:18+08:00 New Revision: 05fdd5383967042c7cb00871de2397d3d5e87b92
URL: https://github.com/llvm/llvm-project/commit/05fdd5383967042c7cb00871de2397d3d5e87b92 DIFF: https://github.com/llvm/llvm-project/commit/05fdd5383967042c7cb00871de2397d3d5e87b92.diff LOG: [Clang] Fix the lambda context for constraint evaluation (#184319) 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 Added: Modified: clang/lib/Sema/SemaConcept.cpp clang/test/SemaTemplate/concepts.cpp Removed: ################################################################################ diff --git a/clang/lib/Sema/SemaConcept.cpp b/clang/lib/Sema/SemaConcept.cpp index 38791940247cb..b4462c944a4ce 100644 --- a/clang/lib/Sema/SemaConcept.cpp +++ b/clang/lib/Sema/SemaConcept.cpp @@ -477,6 +477,11 @@ class ConstraintSatisfactionChecker { ConstraintSatisfaction &Satisfaction; bool BuildExpression; + // The most closest concept declaration when evaluating atomic constriants. + // This is to make sure that lambdas in the atomic expression live in the + // right context. + ConceptDecl *ParentConcept = nullptr; + private: ExprResult EvaluateAtomicConstraint(const Expr *AtomicExpr, @@ -621,8 +626,11 @@ ConstraintSatisfactionChecker::SubstitutionInTemplateArguments( const MultiLevelTemplateArgumentList &MLTAL, llvm::SmallVector<TemplateArgument> &SubstitutedOutermost) { - if (!Constraint.hasParameterMapping()) - return std::move(MLTAL); + if (!Constraint.hasParameterMapping()) { + if (MLTAL.getNumSubstitutedLevels()) + SubstitutedOutermost.assign(MLTAL.getOutermost()); + return MLTAL; + } // The mapping is empty, meaning no template arguments are needed for // evaluation. @@ -693,7 +701,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 +714,20 @@ ExprResult ConstraintSatisfactionChecker::EvaluateSlow( return ExprEmpty(); } + // Note that 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) { + // 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, ParentConcept->getDeclContext(), + ParentConcept->getBeginLoc(), SubstitutedOutermost)); + } + Sema::ArgPackSubstIndexRAII SubstIndex(S, PackSubstitutionIndex); ExprResult SubstitutedAtomicExpr = EvaluateAtomicConstraint( Constraint.getConstraintExpr(), *SubstitutedArgs); @@ -1027,6 +1050,9 @@ ExprResult ConstraintSatisfactionChecker::Evaluate( if (InstTemplate.isInvalid()) return ExprError(); + llvm::SaveAndRestore PushConceptDecl( + ParentConcept, cast<ConceptDecl>(ConceptId->getNamedConcept())); + 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@-1 {{static assertion failed}} +// expected-note@-2 {{evaluated to false}} +// expected-note@#decomposable_lambda {{invalid}} + +static_assert(decomposable_works<foo<int>>); + +static_assert(decomposable_fails<foo<int>>); +// expected-error@-1 {{static assertion failed}} +// expected-note@-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> _______________________________________________ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
