Skip to content

[Clang] Handle instantiating captures in addInstantiatedCapturesToScope() #128478

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 25, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 24 additions & 1 deletion clang/lib/Sema/SemaConcept.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -711,9 +711,32 @@ bool Sema::addInstantiatedCapturesToScope(

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 = LambdaClass->getCapture(Index)->getCapturedVar();
ValueDecl *CapturedVar =
InstantiatingScope ? InstantiatingScope->Captures[Index].getVariable()
: LambdaClass->getCapture(Index)->getCapturedVar();
assert(CapturedVar->isInitCapture());
Scope.InstantiatedLocal(CapturedPattern, CapturedVar);
};
Expand Down
18 changes: 18 additions & 0 deletions clang/test/SemaTemplate/concepts-lambda.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -307,3 +307,21 @@ void test() {
}

}

namespace GH128175 {

template <class> void f() {
[i{0}] {
[&] {
[&] {
[]()
requires true
{}();
}();
}();
}();
}

template void f<int>();

}