Skip to content

[clang][bytecode] Don't call dtors of anonymous unions #110087

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 26, 2024
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
7 changes: 7 additions & 0 deletions clang/lib/AST/ByteCode/Compiler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5302,6 +5302,9 @@ bool Compiler<Emitter>::compileDestructor(const CXXDestructorDecl *Dtor) {
}

for (const Record::Base &Base : llvm::reverse(R->bases())) {
if (Base.R->isAnonymousUnion())
continue;

if (!this->emitGetPtrBase(Base.Offset, SourceInfo{}))
return false;
if (!this->emitRecordDestruction(Base.R))
Expand Down Expand Up @@ -6147,6 +6150,7 @@ bool Compiler<Emitter>::emitComplexComparison(const Expr *LHS, const Expr *RHS,
template <class Emitter>
bool Compiler<Emitter>::emitRecordDestruction(const Record *R) {
assert(R);
assert(!R->isAnonymousUnion());
const CXXDestructorDecl *Dtor = R->getDestructor();
if (!Dtor || Dtor->isTrivial())
return true;
Expand Down Expand Up @@ -6202,6 +6206,9 @@ bool Compiler<Emitter>::emitDestruction(const Descriptor *Desc) {
}

assert(Desc->ElemRecord);
if (Desc->ElemRecord->isAnonymousUnion())
return true;

return this->emitRecordDestruction(Desc->ElemRecord);
}

Expand Down
3 changes: 2 additions & 1 deletion clang/lib/AST/ByteCode/Record.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ Record::Record(const RecordDecl *Decl, BaseList &&SrcBases,
FieldList &&SrcFields, VirtualBaseList &&SrcVirtualBases,
unsigned VirtualSize, unsigned BaseSize)
: Decl(Decl), Bases(std::move(SrcBases)), Fields(std::move(SrcFields)),
BaseSize(BaseSize), VirtualSize(VirtualSize), IsUnion(Decl->isUnion()) {
BaseSize(BaseSize), VirtualSize(VirtualSize), IsUnion(Decl->isUnion()),
IsAnonymousUnion(IsUnion && Decl->isAnonymousStructOrUnion()) {
for (Base &V : SrcVirtualBases)
VirtualBases.push_back({V.Decl, V.Offset + BaseSize, V.Desc, V.R});

Expand Down
4 changes: 4 additions & 0 deletions clang/lib/AST/ByteCode/Record.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ class Record final {
const std::string getName() const;
/// Checks if the record is a union.
bool isUnion() const { return IsUnion; }
/// Checks if the record is an anonymous union.
bool isAnonymousUnion() const { return IsAnonymousUnion; }
/// Returns the size of the record.
unsigned getSize() const { return BaseSize; }
/// Returns the full size of the record, including records.
Expand Down Expand Up @@ -134,6 +136,8 @@ class Record final {
unsigned VirtualSize;
/// If this record is a union.
bool IsUnion;
/// If this is an anonymous union.
bool IsAnonymousUnion;
};

} // namespace interp
Expand Down
31 changes: 31 additions & 0 deletions clang/test/AST/ByteCode/cxx23.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -238,3 +238,34 @@ namespace TwosComplementShifts {
static_assert(-3 >> 1 == -2);
static_assert(-7 >> 1 == -4);
}

namespace AnonUnionDtor {
struct A {
A ();
~A();
};

template <class T>
struct opt
{
union { // all20-note {{is not literal}}
char c;
T data;
};

constexpr opt() {}

constexpr ~opt() {
if (engaged)
data.~T();
}

bool engaged = false;
};

consteval void foo() {
opt<A> a; // all20-error {{variable of non-literal type}}
}

void bar() { foo(); }
}
Loading