Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions clang/lib/AST/ByteCode/Interp.h
Original file line number Diff line number Diff line change
Expand Up @@ -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()) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why the switch from getFieldDesc to getDeclDesc

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;
}
}
Expand Down
12 changes: 12 additions & 0 deletions clang/test/AST/ByteCode/lifetimes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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}}
}