Skip to content

Commit 29c3212

Browse files
committed
[clang][bytecode] Fix a crash in Destroy op
The local we're destroying might've been created for an expression, in which case asDecl() on the DeclDesc returns nullptr. Fixes #152958
1 parent e16ced3 commit 29c3212

File tree

2 files changed

+23
-3
lines changed

2 files changed

+23
-3
lines changed

clang/lib/AST/ByteCode/Interp.h

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2437,9 +2437,17 @@ inline bool Destroy(InterpState &S, CodePtr OpPC, uint32_t I) {
24372437
const Pointer &Ptr = S.Current->getLocalPointer(Local.Offset);
24382438

24392439
if (Ptr.getLifetime() == Lifetime::Ended) {
2440-
auto *D = cast<NamedDecl>(Ptr.getFieldDesc()->asDecl());
2441-
S.FFDiag(D->getLocation(), diag::note_constexpr_destroy_out_of_lifetime)
2442-
<< D->getNameAsString();
2440+
// Try to use the declaration for better diagnostics
2441+
if (const Decl *D = Ptr.getDeclDesc()->asDecl()) {
2442+
auto *ND = cast<NamedDecl>(D);
2443+
S.FFDiag(ND->getLocation(),
2444+
diag::note_constexpr_destroy_out_of_lifetime)
2445+
<< ND->getNameAsString();
2446+
} else {
2447+
S.FFDiag(Ptr.getDeclDesc()->getLocation(),
2448+
diag::note_constexpr_destroy_out_of_lifetime)
2449+
<< Ptr.toDiagnosticString(S.getASTContext());
2450+
}
24432451
return false;
24442452
}
24452453
}

clang/test/AST/ByteCode/lifetimes.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,3 +104,15 @@ namespace CallScope {
104104
// expected-note {{member call on variable whose lifetime has ended}} \
105105
// ref-note {{member call on object outside its lifetime}}
106106
}
107+
108+
namespace ExprDoubleDestroy {
109+
template <typename T>
110+
constexpr bool test() {
111+
T{}.~T(); // both-note {{lifetime has already ended}}
112+
return true;
113+
}
114+
115+
struct S { int x; };
116+
constexpr bool t = test<S>(); // both-error {{must be initialized by a constant expression}} \
117+
// both-note {{in call to}}
118+
}

0 commit comments

Comments
 (0)