Skip to content

Commit e1c3e16

Browse files
authored
[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
1 parent 852aaf5 commit e1c3e16

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
@@ -818,6 +818,7 @@ Bug Fixes to C++ Support
818818
Fixes (`#80252 <https://github.com/llvm/llvm-project/issues/80252>`_)
819819
- Fix a regression introduced in Clang 18 causing incorrect overload resolution in the presence of functions only
820820
differering by their constraints when only one of these function was variadic.
821+
- Fix a crash when a variable is captured by a block nested inside a lambda. (Fixes #GH93625).
821822

822823
Bug Fixes to AST Handling
823824
^^^^^^^^^^^^^^^^^^^^^^^^^

clang/lib/AST/Decl.cpp

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

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

24162418
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)