Author: Timm Baeder Date: 2025-08-21T13:23:57+02:00 New Revision: 2ea5ec78db3e76f2c31309d89b7697ec02a8ab5e
URL: https://github.com/llvm/llvm-project/commit/2ea5ec78db3e76f2c31309d89b7697ec02a8ab5e DIFF: https://github.com/llvm/llvm-project/commit/2ea5ec78db3e76f2c31309d89b7697ec02a8ab5e.diff LOG: [clang][bytecode] Fix a crash in Destroy op (#154695) The local we're destroying might've been created for an expression, in which case asDecl() on the DeclDesc returns nullptr. Fixes #152958 Added: Modified: clang/lib/AST/ByteCode/Interp.h clang/test/AST/ByteCode/lifetimes.cpp Removed: ################################################################################ diff --git a/clang/lib/AST/ByteCode/Interp.h b/clang/lib/AST/ByteCode/Interp.h index 4eaaa018824be..92e60b6b88e6a 100644 --- a/clang/lib/AST/ByteCode/Interp.h +++ b/clang/lib/AST/ByteCode/Interp.h @@ -2459,9 +2459,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}} +} _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits