Skip to content

[clang][Sema] Fix a bug when instantiating a lambda with requires clause #65193

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 6 commits into from
Oct 4, 2023
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
2 changes: 2 additions & 0 deletions clang/docs/ReleaseNotes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,8 @@ Bug Fixes in This Version
Fixes (`#67603 <https://github.com/llvm/llvm-project/issues/67603>`_)
- Fixes a crash caused by a multidimensional array being captured by a lambda
(`#67722 <https://github.com/llvm/llvm-project/issues/67722>`_).
- Fixes a crash when instantiating a lambda with requires clause.
(`#64462 <https://github.com/llvm/llvm-project/issues/64462>`_)

Bug Fixes to Compiler Builtins
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Expand Down
8 changes: 7 additions & 1 deletion clang/include/clang/Sema/Sema.h
Original file line number Diff line number Diff line change
Expand Up @@ -7383,7 +7383,8 @@ class Sema final {
public:
LambdaScopeForCallOperatorInstantiationRAII(
Sema &SemasRef, FunctionDecl *FD, MultiLevelTemplateArgumentList MLTAL,
LocalInstantiationScope &Scope);
LocalInstantiationScope &Scope,
bool ShouldAddDeclsFromParentScope = true);
};

/// Check whether the given expression is a valid constraint expression.
Expand All @@ -7409,6 +7410,11 @@ class Sema final {
llvm::ContextualFoldingSet<ConstraintSatisfaction, const ASTContext &>
SatisfactionCache;

/// Introduce the instantiated local variables into the local
/// instantiation scope.
void addInstantiatedLocalVarsToScope(FunctionDecl *Function,
const FunctionDecl *PatternDecl,
LocalInstantiationScope &Scope);
/// Introduce the instantiated function parameters into the local
/// instantiation scope, and set the parameter names to those used
/// in the template.
Expand Down
6 changes: 3 additions & 3 deletions clang/lib/Sema/SemaConcept.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -702,8 +702,7 @@ bool Sema::CheckFunctionConstraints(const FunctionDecl *FD,
}

ContextRAII SavedContext{*this, CtxToSave};
LocalInstantiationScope Scope(*this, !ForOverloadResolution ||
isLambdaCallOperator(FD));
LocalInstantiationScope Scope(*this, !ForOverloadResolution);
std::optional<MultiLevelTemplateArgumentList> MLTAL =
SetupConstraintCheckingTemplateArgumentsAndScope(
const_cast<FunctionDecl *>(FD), {}, Scope);
Expand All @@ -720,7 +719,8 @@ bool Sema::CheckFunctionConstraints(const FunctionDecl *FD,
CXXThisScopeRAII ThisScope(*this, Record, ThisQuals, Record != nullptr);

LambdaScopeForCallOperatorInstantiationRAII LambdaScope(
*this, const_cast<FunctionDecl *>(FD), *MLTAL, Scope);
*this, const_cast<FunctionDecl *>(FD), *MLTAL, Scope,
ForOverloadResolution);

return CheckConstraintSatisfaction(
FD, {FD->getTrailingRequiresClause()}, *MLTAL,
Expand Down
62 changes: 42 additions & 20 deletions clang/lib/Sema/SemaLambda.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2296,33 +2296,55 @@ ExprResult Sema::BuildBlockForLambdaConversion(SourceLocation CurrentLocation,
return BuildBlock;
}

static FunctionDecl *getPatternFunctionDecl(FunctionDecl *FD) {
if (FD->getTemplatedKind() == FunctionDecl::TK_MemberSpecialization) {
while (FD->getInstantiatedFromMemberFunction())
FD = FD->getInstantiatedFromMemberFunction();
return FD;
}

if (FD->getTemplatedKind() == FunctionDecl::TK_DependentNonTemplate)
return FD->getInstantiatedFromDecl();

FunctionTemplateDecl *FTD = FD->getPrimaryTemplate();
if (!FTD)
return nullptr;

while (FTD->getInstantiatedFromMemberTemplate())
FTD = FTD->getInstantiatedFromMemberTemplate();

return FTD->getTemplatedDecl();
}

Sema::LambdaScopeForCallOperatorInstantiationRAII::
LambdaScopeForCallOperatorInstantiationRAII(
Sema &SemasRef, FunctionDecl *FD, MultiLevelTemplateArgumentList MLTAL,
LocalInstantiationScope &Scope)
: FunctionScopeRAII(SemasRef) {
Sema &SemaRef, FunctionDecl *FD, MultiLevelTemplateArgumentList MLTAL,
LocalInstantiationScope &Scope, bool ShouldAddDeclsFromParentScope)
: FunctionScopeRAII(SemaRef) {
if (!isLambdaCallOperator(FD)) {
FunctionScopeRAII::disable();
return;
}

if (FD->isTemplateInstantiation() && FD->getPrimaryTemplate()) {
FunctionTemplateDecl *PrimaryTemplate = FD->getPrimaryTemplate();
if (const auto *FromMemTempl =
PrimaryTemplate->getInstantiatedFromMemberTemplate()) {
SemasRef.addInstantiatedCapturesToScope(
FD, FromMemTempl->getTemplatedDecl(), Scope, MLTAL);
}
}
SemaRef.RebuildLambdaScopeInfo(cast<CXXMethodDecl>(FD));

else if (FD->getTemplatedKind() == FunctionDecl::TK_MemberSpecialization ||
FD->getTemplatedKind() == FunctionDecl::TK_DependentNonTemplate) {
FunctionDecl *InstantiatedFrom =
FD->getTemplatedKind() == FunctionDecl::TK_MemberSpecialization
? FD->getInstantiatedFromMemberFunction()
: FD->getInstantiatedFromDecl();
SemasRef.addInstantiatedCapturesToScope(FD, InstantiatedFrom, Scope, MLTAL);
}
FunctionDecl *Pattern = getPatternFunctionDecl(FD);
if (Pattern) {
SemaRef.addInstantiatedCapturesToScope(FD, Pattern, Scope, MLTAL);

FunctionDecl *ParentFD = FD;
while (ShouldAddDeclsFromParentScope) {

ParentFD =
dyn_cast<FunctionDecl>(getLambdaAwareParentOfDeclContext(ParentFD));
Pattern =
dyn_cast<FunctionDecl>(getLambdaAwareParentOfDeclContext(Pattern));

SemasRef.RebuildLambdaScopeInfo(cast<CXXMethodDecl>(FD));
if (!FD || !Pattern)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@0x59616e Should the !FD be !ParentFD instead? Just noticed this in a static analysis report

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm looking into it.

break;

SemaRef.addInstantiatedParametersToScope(ParentFD, Pattern, Scope, MLTAL);
SemaRef.addInstantiatedLocalVarsToScope(ParentFD, Pattern, Scope);
}
}
}
30 changes: 30 additions & 0 deletions clang/lib/Sema/SemaTemplateInstantiateDecl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4523,6 +4523,36 @@ TemplateDeclInstantiator::SubstFunctionType(FunctionDecl *D,
return NewTInfo;
}

/// Introduce the instantiated local variables into the local
/// instantiation scope.
void Sema::addInstantiatedLocalVarsToScope(FunctionDecl *Function,
const FunctionDecl *PatternDecl,
LocalInstantiationScope &Scope) {
LambdaScopeInfo *LSI = cast<LambdaScopeInfo>(getFunctionScopes().back());

for (auto *decl : PatternDecl->decls()) {
if (!isa<VarDecl>(decl) || isa<ParmVarDecl>(decl))
continue;

VarDecl *VD = cast<VarDecl>(decl);
IdentifierInfo *II = VD->getIdentifier();

auto it = llvm::find_if(Function->decls(), [&](Decl *inst) {
VarDecl *InstVD = dyn_cast<VarDecl>(inst);
return InstVD && InstVD->isLocalVarDecl() &&
InstVD->getIdentifier() == II;
});

if (it == Function->decls().end())
continue;

Scope.InstantiatedLocal(VD, *it);
LSI->addCapture(cast<VarDecl>(*it), /*isBlock=*/false, /*isByref=*/false,
/*isNested=*/false, VD->getLocation(), SourceLocation(),
VD->getType(), /*Invalid=*/false);
}
}

/// Introduce the instantiated function parameters into the local
/// instantiation scope, and set the parameter names to those used
/// in the template.
Expand Down
21 changes: 21 additions & 0 deletions clang/test/SemaCXX/pr64462.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// RUN: %clang_cc1 -verify -std=c++20 -fsyntax-only %s

auto c1(auto f, auto ...fs) {
constexpr bool a = true;
return [](auto) requires a {
constexpr bool b = true;
return []() requires a && b {
constexpr bool c = true;
return [](auto) requires a && b && c {
constexpr bool d = true;
// expected-note@+2{{because substituted constraint expression is ill-formed: no matching function for call to 'c1'}}
// expected-note@+1{{candidate function not viable: constraints not satisfied}}
return []() requires a && b && c && d && (c1(fs...)) {};
};
}();
}(1);
}

void foo() {
c1(true)(1.0)(); // expected-error{{no matching function for call to object of type}}
}