llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT--> @llvm/pr-subscribers-clang Author: Timm Baeder (tbaederr) <details> <summary>Changes</summary> The local we're destroying might've been created for an expression, in which case asDecl() on the DeclDesc returns nullptr. Fixes #<!-- -->152958 --- Full diff: https://github.com/llvm/llvm-project/pull/154695.diff 2 Files Affected: - (modified) clang/lib/AST/ByteCode/Interp.h (+11-3) - (modified) clang/test/AST/ByteCode/lifetimes.cpp (+12) ``````````diff 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}} +} `````````` </details> https://github.com/llvm/llvm-project/pull/154695 _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits