Skip to content

[Clang] Recover GLTemplateParameterList for generic lambdas in RebuildLambdaScopeInfo #118176

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
Dec 3, 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
2 changes: 2 additions & 0 deletions clang/docs/ReleaseNotes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -764,6 +764,8 @@ Bug Fixes to C++ Support
- Fixed an assertion failure caused by using ``consteval`` in condition in consumed analyses. (#GH117385)
- Fix a crash caused by incorrect argument position in merging deduced template arguments. (#GH113659)
- Fixed an assertion failure caused by mangled names with invalid identifiers. (#GH112205)
- Fixed an incorrect lambda scope of generic lambdas that caused Clang to crash when computing potential lambda
captures at the end of a full expression. (#GH115931)

Bug Fixes to AST Handling
^^^^^^^^^^^^^^^^^^^^^^^^^
Expand Down
21 changes: 18 additions & 3 deletions clang/lib/Sema/SemaDecl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15519,10 +15519,25 @@ LambdaScopeInfo *Sema::RebuildLambdaScopeInfo(CXXMethodDecl *CallOperator) {
LSI->CallOperator = CallOperator;
LSI->Lambda = LambdaClass;
LSI->ReturnType = CallOperator->getReturnType();
// This function in calls in situation where the context of the call operator
// is not entered, so we set AfterParameterList to false, so that
// When this function is called in situation where the context of the call
// operator is not entered, we set AfterParameterList to false, so that
// `tryCaptureVariable` finds explicit captures in the appropriate context.
LSI->AfterParameterList = false;
// There is also at least a situation as in FinishTemplateArgumentDeduction(),
// where we would set the CurContext to the lambda operator before
// substituting into it. In this case the flag needs to be true such that
// tryCaptureVariable can correctly handle potential captures thereof.
LSI->AfterParameterList = CurContext == CallOperator;

// GLTemplateParameterList is necessary for getCurGenericLambda() which is
// used at the point of dealing with potential captures.
//
// We don't use LambdaClass->isGenericLambda() because this value doesn't
// flip for instantiated generic lambdas, where no FunctionTemplateDecls are
// associated. (Technically, we could recover that list from their
// instantiation patterns, but for now, the GLTemplateParameterList seems
// unnecessary in these cases.)
if (FunctionTemplateDecl *FTD = CallOperator->getDescribedFunctionTemplate())
LSI->GLTemplateParameterList = FTD->getTemplateParameters();
const LambdaCaptureDefault LCD = LambdaClass->getLambdaCaptureDefault();

if (LCD == LCD_None)
Expand Down
16 changes: 16 additions & 0 deletions clang/test/SemaCXX/lambda-capture-type-deduction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,22 @@ void __trans_tmp_1() {

}

namespace GH115931 {

struct Range {};

template <Range>
struct LengthPercentage {};

void reflectSum() {
Range resultR;
[&] (auto) -> LengthPercentage<resultR> {
return {};
}(0);
}

} // namespace GH115931

namespace GH47400 {

struct Foo {};
Expand Down
Loading