[clang] [clang][bytecode] Fix a crash in Destroy op (PR #154695)
Timm Baeder via cfe-commits
cfe-commits at lists.llvm.org
Thu Aug 21 01:18:30 PDT 2025
https://github.com/tbaederr created https://github.com/llvm/llvm-project/pull/154695
The local we're destroying might've been created for an expression, in which case asDecl() on the DeclDesc returns nullptr.
Fixes #152958
>From 29c3212fa3d20e9ee84e2e86fc1282eb5f382a10 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Timm=20B=C3=A4der?= <tbaeder at redhat.com>
Date: Thu, 21 Aug 2025 10:16:28 +0200
Subject: [PATCH] [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
---
clang/lib/AST/ByteCode/Interp.h | 14 +++++++++++---
clang/test/AST/ByteCode/lifetimes.cpp | 12 ++++++++++++
2 files changed, 23 insertions(+), 3 deletions(-)
diff --git a/clang/lib/AST/ByteCode/Interp.h b/clang/lib/AST/ByteCode/Interp.h
index 149ce3b1042db..61036f9f9961f 100644
--- a/clang/lib/AST/ByteCode/Interp.h
+++ b/clang/lib/AST/ByteCode/Interp.h
@@ -2437,9 +2437,17 @@ inline bool Destroy(InterpState &S, CodePtr OpPC, uint32_t I) {
const Pointer &Ptr = S.Current->getLocalPointer(Local.Offset);
if (Ptr.getLifetime() == Lifetime::Ended) {
- auto *D = cast<NamedDecl>(Ptr.getFieldDesc()->asDecl());
- S.FFDiag(D->getLocation(), diag::note_constexpr_destroy_out_of_lifetime)
- << D->getNameAsString();
+ // Try to use the declaration for better diagnostics
+ if (const Decl *D = Ptr.getDeclDesc()->asDecl()) {
+ auto *ND = cast<NamedDecl>(D);
+ S.FFDiag(ND->getLocation(),
+ diag::note_constexpr_destroy_out_of_lifetime)
+ << ND->getNameAsString();
+ } else {
+ S.FFDiag(Ptr.getDeclDesc()->getLocation(),
+ diag::note_constexpr_destroy_out_of_lifetime)
+ << Ptr.toDiagnosticString(S.getASTContext());
+ }
return false;
}
}
diff --git a/clang/test/AST/ByteCode/lifetimes.cpp b/clang/test/AST/ByteCode/lifetimes.cpp
index c8bf02c228481..d3b02d215b442 100644
--- a/clang/test/AST/ByteCode/lifetimes.cpp
+++ b/clang/test/AST/ByteCode/lifetimes.cpp
@@ -104,3 +104,15 @@ namespace CallScope {
// expected-note {{member call on variable whose lifetime has ended}} \
// ref-note {{member call on object outside its lifetime}}
}
+
+namespace ExprDoubleDestroy {
+ template <typename T>
+ constexpr bool test() {
+ T{}.~T(); // both-note {{lifetime has already ended}}
+ return true;
+ }
+
+ struct S { int x; };
+ constexpr bool t = test<S>(); // both-error {{must be initialized by a constant expression}} \
+ // both-note {{in call to}}
+}
More information about the cfe-commits
mailing list