Skip to content

Commit f634557

Browse files
committed
[Clang] Fix a DeclContext mismatch when parsing nested lambda parameters
1 parent 4336f00 commit f634557

File tree

4 files changed

+28
-1
lines changed

4 files changed

+28
-1
lines changed

clang/docs/ReleaseNotes.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -487,6 +487,8 @@ Bug Fixes to C++ Support
487487
- Clang now uses the correct set of template argument lists when comparing the constraints of
488488
out-of-line definitions and member templates explicitly specialized for a given implicit instantiation of
489489
a class template. (#GH102320)
490+
- Fixed a bug in lambda captures where ``constexpr`` class-type objects were not properly considered ODR-used in
491+
certain situations. (#GH47400), (#GH90896)
490492

491493
Bug Fixes to AST Handling
492494
^^^^^^^^^^^^^^^^^^^^^^^^^

clang/lib/Sema/SemaExpr.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18843,7 +18843,16 @@ bool Sema::tryCaptureVariable(
1884318843
// We need to sync up the Declaration Context with the
1884418844
// FunctionScopeIndexToStopAt
1884518845
if (FunctionScopeIndexToStopAt) {
18846+
assert(!FunctionScopes.empty() && "No function scopes to stop at?");
1884618847
unsigned FSIndex = FunctionScopes.size() - 1;
18848+
// When we're parsing the lambda parameter list, the current DeclContext is
18849+
// NOT the lambda but its parent. So move off the current LSI before
18850+
// aligning DC and FunctionScopeIndexToStopAt.
18851+
if (auto *LSI = dyn_cast<LambdaScopeInfo>(FunctionScopes[FSIndex]);
18852+
LSI && !LSI->AfterParameterList)
18853+
--FSIndex;
18854+
assert(MaxFunctionScopesIndex <= FSIndex &&
18855+
"FSIndex is larger than the size of function scopes?");
1884718856
while (FSIndex != MaxFunctionScopesIndex) {
1884818857
DC = getLambdaAwareParentOfDeclContext(DC);
1884918858
--FSIndex;

clang/lib/Sema/SemaExprCXX.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8679,7 +8679,7 @@ static void CheckIfAnyEnclosingLambdasMustCaptureAnyPotentialCaptures(
86798679
while (isa_and_nonnull<CapturedDecl>(DC))
86808680
DC = DC->getParent();
86818681
assert(
8682-
CurrentLSI->CallOperator == DC &&
8682+
(CurrentLSI->CallOperator == DC || !CurrentLSI->AfterParameterList) &&
86838683
"The current call operator must be synchronized with Sema's CurContext");
86848684
#endif // NDEBUG
86858685

clang/test/SemaCXX/lambda-capture-type-deduction.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -297,3 +297,19 @@ void __trans_tmp_1() {
297297
}
298298

299299
}
300+
301+
namespace GH47400 {
302+
303+
struct Foo {};
304+
305+
template <int, Foo f> struct Arr {};
306+
307+
constexpr bool foo() {
308+
constexpr Foo fff;
309+
[&]<int is>() {
310+
[&](Arr<is, fff>) {}({});
311+
}.template operator()<42>();
312+
return true;
313+
}
314+
315+
} // namespace GH47400

0 commit comments

Comments
 (0)