Skip to content

Commit c0ae354

Browse files
ahatanakahatanaka
authored andcommitted
[clang] Fix a crash when a variable is captured by a block nested inside a lambda (llvm#93749)
`Eval->Value.get` returns a null pointer when the variable doesn't have an initializer. Use `cast_if_present` instead of `cast`. This fixes llvm#93625. rdar://128482541 (cherry picked from commit e1c3e16) Conflicts: clang/docs/ReleaseNotes.rst
1 parent cc2834b commit c0ae354

File tree

3 files changed

+24
-3
lines changed

3 files changed

+24
-3
lines changed

clang/docs/ReleaseNotes.rst

+1
Original file line numberDiff line numberDiff line change
@@ -506,6 +506,7 @@ Bug Fixes to C++ Support
506506
- Fix crash when inheriting from a cv-qualified type. Fixes:
507507
(`#35603 <https://github.com/llvm/llvm-project/issues/35603>`_)
508508
- Fix a crash when the using enum declaration uses an anonymous enumeration. Fixes (#GH86790).
509+
- Fix a crash when a variable is captured by a block nested inside a lambda. (Fixes #GH93625).
509510

510511
Bug Fixes to AST Handling
511512
^^^^^^^^^^^^^^^^^^^^^^^^^

clang/lib/AST/Decl.cpp

+5-3
Original file line numberDiff line numberDiff line change
@@ -2409,9 +2409,11 @@ Expr *VarDecl::getInit() {
24092409
return cast<Expr>(S);
24102410

24112411
auto *Eval = getEvaluatedStmt();
2412-
return cast<Expr>(Eval->Value.isOffset()
2413-
? Eval->Value.get(getASTContext().getExternalSource())
2414-
: Eval->Value.get(nullptr));
2412+
2413+
return cast_if_present<Expr>(
2414+
Eval->Value.isOffset()
2415+
? Eval->Value.get(getASTContext().getExternalSource())
2416+
: Eval->Value.get(nullptr));
24152417
}
24162418

24172419
Stmt **VarDecl::getInitAddress() {

clang/test/SemaObjCXX/block-capture.mm

+18
Original file line numberDiff line numberDiff line change
@@ -83,3 +83,21 @@
8383
SubMove(SubSubMove &&);
8484
};
8585
TEST(SubMove);
86+
87+
88+
#if __cplusplus >= 202302L
89+
// clang used to crash compiling this code.
90+
namespace BlockInLambda {
91+
struct S {
92+
constexpr ~S();
93+
};
94+
95+
void func(S const &a) {
96+
[a](auto b) {
97+
^{
98+
(void)a;
99+
}();
100+
}(12);
101+
}
102+
}
103+
#endif

0 commit comments

Comments
 (0)