Skip to content

Commit f5f3d5d

Browse files
authored
[Clang][Sema] Fix a crash in lambda instantiation (#85565)
Fix #85343 When build lambda expression in lambda instantiation, `ThisType` is required in `Sema::CheckCXXThisCapture` to build `this` capture. Set `this` type by import `Sema::CXXThisScopeRAII` and it will be used later in lambda expression transformation. Co-authored-by: huqizhi <[email protected]>
1 parent 22ac5f7 commit f5f3d5d

File tree

3 files changed

+33
-0
lines changed

3 files changed

+33
-0
lines changed

clang/docs/ReleaseNotes.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -401,6 +401,7 @@ Bug Fixes to C++ Support
401401
expression references to an entity declared outside of the lambda. (#GH64808)
402402
- Clang's __builtin_bit_cast will now produce a constant value for records with empty bases. See:
403403
(#GH82383)
404+
- Fix a crash when instantiating a lambda that captures ``this`` outside of its context. Fixes (#GH85343).
404405

405406
Bug Fixes to AST Handling
406407
^^^^^^^^^^^^^^^^^^^^^^^^^

clang/lib/Sema/TreeTransform.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13714,6 +13714,16 @@ TreeTransform<Derived>::TransformLambdaExpr(LambdaExpr *E) {
1371413714

1371513715
// Capturing 'this' is trivial.
1371613716
if (C->capturesThis()) {
13717+
// If this is a lambda that is part of a default member initialiser
13718+
// and which we're instantiating outside the class that 'this' is
13719+
// supposed to refer to, adjust the type of 'this' accordingly.
13720+
//
13721+
// Otherwise, leave the type of 'this' as-is.
13722+
Sema::CXXThisScopeRAII ThisScope(
13723+
getSema(),
13724+
dyn_cast_if_present<CXXRecordDecl>(
13725+
getSema().getFunctionLevelDeclContext()),
13726+
Qualifiers());
1371713727
getSema().CheckCXXThisCapture(C->getLocation(), C->isExplicit(),
1371813728
/*BuildAndDiagnose*/ true, nullptr,
1371913729
C->getCaptureKind() == LCK_StarThis);

clang/test/Sema/PR85343.cpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// RUN: %clang_cc1 -std=c++14 -verify %s
2+
// expected-no-diagnostics
3+
4+
template <typename c> auto ab() -> c ;
5+
6+
template <typename> struct e {};
7+
8+
template <typename f> struct ac {
9+
template <typename h> static e<decltype(ab<h>()(ab<int>))> i;
10+
decltype(i<f>) j;
11+
};
12+
13+
struct d {
14+
template <typename f>
15+
d(f) {
16+
ac<f> a;
17+
}
18+
};
19+
struct a {
20+
d b = [=](auto) { (void)[this] {}; };
21+
};
22+
void b() { new a; }

0 commit comments

Comments
 (0)