https://github.com/zyn0217 created https://github.com/llvm/llvm-project/pull/133863
In ecc7e6ce4, we tried to inspect the `LambdaScopeInfo` on stack to recover the instantiating lambda captures. However, there was a mismatch in how we compared the pattern declarations of lambdas: the constraint instantiation used a tailored `getPatternFunctionDecl()` which is localized in SemaLambda that finds the very primal template declaration of a lambda, while `FunctionDecl::getTemplateInstantiationPattern` finds the latest template pattern of a lambda. This difference causes issues when lambdas are nested, as we always want the primary template declaration. This corrects that by moving `Sema::addInstantiatedCapturesToScope` from SemaConcept to SemaLambda, allowing it to use the localized version of `getPatternFunctionDecl`. It is also worth exploring to coalesce the implementation of `getPatternFunctionDecl` with `FunctionDecl::getTemplateInstantiationPattern`. But I’m leaving that for the future, as I’d like to backport this fix (ecc7e6ce4 made the issue more visible in clang 20, sorry!), and changing Sema’s ABI would not be suitable in that regards. Hence, no release note. Fixes https://github.com/llvm/llvm-project/issues/133719 (I split up the commits to make the review easier. Also note that in #133719, Shafik and I discovered additional cases where clang behaves incorrectly. However, since those aren't regressions, their fixes aren't being considered at this time.) >From f52371717e2e9c93c2aeb31e215134dcab06951e Mon Sep 17 00:00:00 2001 From: Younan Zhang <zyn7...@gmail.com> Date: Tue, 1 Apr 2025 11:13:41 +0800 Subject: [PATCH 1/3] Move Sema::addInstantiatedCapturesToScope to SemaLambda.cpp --- clang/lib/Sema/SemaConcept.cpp | 69 ---------------------------------- clang/lib/Sema/SemaLambda.cpp | 69 ++++++++++++++++++++++++++++++++++ 2 files changed, 69 insertions(+), 69 deletions(-) diff --git a/clang/lib/Sema/SemaConcept.cpp b/clang/lib/Sema/SemaConcept.cpp index e7e0b4cfb72a7..2d29af4258cd7 100644 --- a/clang/lib/Sema/SemaConcept.cpp +++ b/clang/lib/Sema/SemaConcept.cpp @@ -702,75 +702,6 @@ bool Sema::CheckConstraintSatisfaction(const Expr *ConstraintExpr, .isInvalid(); } -bool Sema::addInstantiatedCapturesToScope( - FunctionDecl *Function, const FunctionDecl *PatternDecl, - LocalInstantiationScope &Scope, - const MultiLevelTemplateArgumentList &TemplateArgs) { - const auto *LambdaClass = cast<CXXMethodDecl>(Function)->getParent(); - const auto *LambdaPattern = cast<CXXMethodDecl>(PatternDecl)->getParent(); - - unsigned Instantiated = 0; - - // FIXME: This is a workaround for not having deferred lambda body - // instantiation. - // When transforming a lambda's body, if we encounter another call to a - // nested lambda that contains a constraint expression, we add all of the - // outer lambda's instantiated captures to the current instantiation scope to - // facilitate constraint evaluation. However, these captures don't appear in - // the CXXRecordDecl until after the lambda expression is rebuilt, so we - // pull them out from the corresponding LSI. - LambdaScopeInfo *InstantiatingScope = nullptr; - if (LambdaPattern->capture_size() && !LambdaClass->capture_size()) { - for (FunctionScopeInfo *Scope : llvm::reverse(FunctionScopes)) { - auto *LSI = dyn_cast<LambdaScopeInfo>(Scope); - if (!LSI || - LSI->CallOperator->getTemplateInstantiationPattern() != PatternDecl) - continue; - InstantiatingScope = LSI; - break; - } - assert(InstantiatingScope); - } - - auto AddSingleCapture = [&](const ValueDecl *CapturedPattern, - unsigned Index) { - ValueDecl *CapturedVar = - InstantiatingScope ? InstantiatingScope->Captures[Index].getVariable() - : LambdaClass->getCapture(Index)->getCapturedVar(); - assert(CapturedVar->isInitCapture()); - Scope.InstantiatedLocal(CapturedPattern, CapturedVar); - }; - - for (const LambdaCapture &CapturePattern : LambdaPattern->captures()) { - if (!CapturePattern.capturesVariable()) { - Instantiated++; - continue; - } - ValueDecl *CapturedPattern = CapturePattern.getCapturedVar(); - - if (!CapturedPattern->isInitCapture()) { - Instantiated++; - continue; - } - - if (!CapturedPattern->isParameterPack()) { - AddSingleCapture(CapturedPattern, Instantiated++); - } else { - Scope.MakeInstantiatedLocalArgPack(CapturedPattern); - SmallVector<UnexpandedParameterPack, 2> Unexpanded; - SemaRef.collectUnexpandedParameterPacks( - dyn_cast<VarDecl>(CapturedPattern)->getInit(), Unexpanded); - auto NumArgumentsInExpansion = - getNumArgumentsInExpansionFromUnexpanded(Unexpanded, TemplateArgs); - if (!NumArgumentsInExpansion) - continue; - for (unsigned Arg = 0; Arg < *NumArgumentsInExpansion; ++Arg) - AddSingleCapture(CapturedPattern, Instantiated++); - } - } - return false; -} - bool Sema::SetupConstraintScope( FunctionDecl *FD, std::optional<ArrayRef<TemplateArgument>> TemplateArgs, const MultiLevelTemplateArgumentList &MLTAL, diff --git a/clang/lib/Sema/SemaLambda.cpp b/clang/lib/Sema/SemaLambda.cpp index 292406f886362..141136364a851 100644 --- a/clang/lib/Sema/SemaLambda.cpp +++ b/clang/lib/Sema/SemaLambda.cpp @@ -2412,6 +2412,75 @@ static FunctionDecl *getPatternFunctionDecl(FunctionDecl *FD) { return FTD->getTemplatedDecl(); } +bool Sema::addInstantiatedCapturesToScope( + FunctionDecl *Function, const FunctionDecl *PatternDecl, + LocalInstantiationScope &Scope, + const MultiLevelTemplateArgumentList &TemplateArgs) { + const auto *LambdaClass = cast<CXXMethodDecl>(Function)->getParent(); + const auto *LambdaPattern = cast<CXXMethodDecl>(PatternDecl)->getParent(); + + unsigned Instantiated = 0; + + // FIXME: This is a workaround for not having deferred lambda body + // instantiation. + // When transforming a lambda's body, if we encounter another call to a + // nested lambda that contains a constraint expression, we add all of the + // outer lambda's instantiated captures to the current instantiation scope to + // facilitate constraint evaluation. However, these captures don't appear in + // the CXXRecordDecl until after the lambda expression is rebuilt, so we + // pull them out from the corresponding LSI. + LambdaScopeInfo *InstantiatingScope = nullptr; + if (LambdaPattern->capture_size() && !LambdaClass->capture_size()) { + for (FunctionScopeInfo *Scope : llvm::reverse(FunctionScopes)) { + auto *LSI = dyn_cast<LambdaScopeInfo>(Scope); + if (!LSI || + LSI->CallOperator->getTemplateInstantiationPattern() != PatternDecl) + continue; + InstantiatingScope = LSI; + break; + } + assert(InstantiatingScope); + } + + auto AddSingleCapture = [&](const ValueDecl *CapturedPattern, + unsigned Index) { + ValueDecl *CapturedVar = + InstantiatingScope ? InstantiatingScope->Captures[Index].getVariable() + : LambdaClass->getCapture(Index)->getCapturedVar(); + assert(CapturedVar->isInitCapture()); + Scope.InstantiatedLocal(CapturedPattern, CapturedVar); + }; + + for (const LambdaCapture &CapturePattern : LambdaPattern->captures()) { + if (!CapturePattern.capturesVariable()) { + Instantiated++; + continue; + } + ValueDecl *CapturedPattern = CapturePattern.getCapturedVar(); + + if (!CapturedPattern->isInitCapture()) { + Instantiated++; + continue; + } + + if (!CapturedPattern->isParameterPack()) { + AddSingleCapture(CapturedPattern, Instantiated++); + } else { + Scope.MakeInstantiatedLocalArgPack(CapturedPattern); + SmallVector<UnexpandedParameterPack, 2> Unexpanded; + SemaRef.collectUnexpandedParameterPacks( + dyn_cast<VarDecl>(CapturedPattern)->getInit(), Unexpanded); + auto NumArgumentsInExpansion = + getNumArgumentsInExpansionFromUnexpanded(Unexpanded, TemplateArgs); + if (!NumArgumentsInExpansion) + continue; + for (unsigned Arg = 0; Arg < *NumArgumentsInExpansion; ++Arg) + AddSingleCapture(CapturedPattern, Instantiated++); + } + } + return false; +} + Sema::LambdaScopeForCallOperatorInstantiationRAII:: LambdaScopeForCallOperatorInstantiationRAII( Sema &SemaRef, FunctionDecl *FD, MultiLevelTemplateArgumentList MLTAL, >From b6877bcbe1fa6e6ab357f42ae6acdab678f518ad Mon Sep 17 00:00:00 2001 From: Younan Zhang <zyn7...@gmail.com> Date: Tue, 1 Apr 2025 11:16:17 +0800 Subject: [PATCH 2/3] Use getPatternFunctionDecl() over getTemplateInstantiationPattern() The latter version doesn't find the very original template pattern for a transformed lambda, while getPatternFunctionDecl() finds what we want for constraint instantiation. --- clang/lib/Sema/SemaLambda.cpp | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/clang/lib/Sema/SemaLambda.cpp b/clang/lib/Sema/SemaLambda.cpp index 141136364a851..6f114b71981fa 100644 --- a/clang/lib/Sema/SemaLambda.cpp +++ b/clang/lib/Sema/SemaLambda.cpp @@ -2433,8 +2433,7 @@ bool Sema::addInstantiatedCapturesToScope( if (LambdaPattern->capture_size() && !LambdaClass->capture_size()) { for (FunctionScopeInfo *Scope : llvm::reverse(FunctionScopes)) { auto *LSI = dyn_cast<LambdaScopeInfo>(Scope); - if (!LSI || - LSI->CallOperator->getTemplateInstantiationPattern() != PatternDecl) + if (!LSI || getPatternFunctionDecl(LSI->CallOperator) != PatternDecl) continue; InstantiatingScope = LSI; break; >From 910b73c019a48002332540a5e44ea12b5dd72338 Mon Sep 17 00:00:00 2001 From: Younan Zhang <zyn7...@gmail.com> Date: Tue, 1 Apr 2025 13:11:52 +0800 Subject: [PATCH 3/3] Test --- clang/test/SemaTemplate/concepts-lambda.cpp | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/clang/test/SemaTemplate/concepts-lambda.cpp b/clang/test/SemaTemplate/concepts-lambda.cpp index dcb09c76d26b6..1f67c2511e096 100644 --- a/clang/test/SemaTemplate/concepts-lambda.cpp +++ b/clang/test/SemaTemplate/concepts-lambda.cpp @@ -325,3 +325,18 @@ template <class> void f() { template void f<int>(); } + +namespace GH133719 { + +template <class T> +constexpr auto f{[] (auto arg) { + return [a{arg}] { + [] () requires true {}(); + }; +}}; + +void foo() { + f<int>(0); +} + +} _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits