Skip to content

[Clang] Fix a lambda pattern comparison mismatch after ecc7e6ce4 #133863

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 3 commits into from
Apr 3, 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
69 changes: 0 additions & 69 deletions clang/lib/Sema/SemaConcept.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
68 changes: 68 additions & 0 deletions clang/lib/Sema/SemaLambda.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2412,6 +2412,74 @@ 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 || getPatternFunctionDecl(LSI->CallOperator) != 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,
Expand Down
15 changes: 15 additions & 0 deletions clang/test/SemaTemplate/concepts-lambda.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

}