Skip to content
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

[Clang] Fix the order of addInstantiatedParameters in LambdaScopeForCallOperatorInstantiationRAII #97215

Merged
merged 2 commits into from
Jul 9, 2024
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
1 change: 1 addition & 0 deletions clang/docs/ReleaseNotes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -929,6 +929,7 @@ Bug Fixes to C++ Support
- Fix a crash caused by improper use of ``__array_extent``. (#GH80474)
- Fixed several bugs in capturing variables within unevaluated contexts. (#GH63845), (#GH67260), (#GH69307),
(#GH88081), (#GH89496), (#GH90669) and (#GH91633).
- Fixed a crash in constraint instantiation under nested lambdas with dependent parameters.
- Fixed handling of brace ellison when building deduction guides. (#GH64625), (#GH83368).
- Clang now instantiates local constexpr functions eagerly for constant evaluators. (#GH35052), (#GH94849)
- Fixed a failed assertion when attempting to convert an integer representing the difference
Expand Down
42 changes: 28 additions & 14 deletions clang/lib/Sema/SemaLambda.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2379,23 +2379,37 @@ Sema::LambdaScopeForCallOperatorInstantiationRAII::

SemaRef.RebuildLambdaScopeInfo(cast<CXXMethodDecl>(FD));

FunctionDecl *Pattern = getPatternFunctionDecl(FD);
if (Pattern) {
SemaRef.addInstantiatedCapturesToScope(FD, Pattern, Scope, MLTAL);
FunctionDecl *FDPattern = getPatternFunctionDecl(FD);
if (!FDPattern)
return;

Comment on lines -2382 to 2385
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: Can you revert unrelated changes?

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 would prefer to keep the current change. Firstly, this change is minimal and won’t significantly impact the review workload for this PR. Secondly, it reduces one level of indentation, which slightly improves readability. ;)

FunctionDecl *ParentFD = FD;
while (ShouldAddDeclsFromParentScope) {
SemaRef.addInstantiatedCapturesToScope(FD, FDPattern, Scope, MLTAL);

ParentFD =
dyn_cast<FunctionDecl>(getLambdaAwareParentOfDeclContext(ParentFD));
Pattern =
dyn_cast<FunctionDecl>(getLambdaAwareParentOfDeclContext(Pattern));
if (!ShouldAddDeclsFromParentScope)
return;

if (!ParentFD || !Pattern)
break;
llvm::SmallVector<std::pair<FunctionDecl *, FunctionDecl *>, 4>
ParentInstantiations;
while (true) {
FDPattern =
dyn_cast<FunctionDecl>(getLambdaAwareParentOfDeclContext(FDPattern));
FD = dyn_cast<FunctionDecl>(getLambdaAwareParentOfDeclContext(FD));

SemaRef.addInstantiatedParametersToScope(ParentFD, Pattern, Scope, MLTAL);
SemaRef.addInstantiatedLocalVarsToScope(ParentFD, Pattern, Scope);
}
if (!FDPattern || !FD)
break;

ParentInstantiations.emplace_back(FDPattern, FD);
}

// Add instantiated parameters and local vars to scopes, starting from the
// outermost lambda to the innermost lambda. This ordering ensures that
// parameters in inner lambdas can correctly depend on those defined
// in outer lambdas, e.g. auto L = [](auto... x) {
// return [](decltype(x)... y) { }; // `y` depends on `x`
// };

for (const auto &[FDPattern, FD] : llvm::reverse(ParentInstantiations)) {
SemaRef.addInstantiatedParametersToScope(FD, FDPattern, Scope, MLTAL);
SemaRef.addInstantiatedLocalVarsToScope(FD, FDPattern, Scope);
}
}
14 changes: 14 additions & 0 deletions clang/test/SemaTemplate/concepts-lambda.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -237,3 +237,17 @@ concept D = []<C T = int>() { return true; }();
D auto x = 0;

} // namespace GH93821

namespace dependent_param_concept {
template <typename... Ts> void sink(Ts...) {}
void dependent_param() {
auto L = [](auto... x) {
return [](decltype(x)... y) {
return [](int z)
requires requires { sink(y..., z); }
{};
};
};
L(0, 1)(1, 2)(1);
}
} // namespace dependent_param_concept
Loading