Skip to content

[clang][bytecode] Only implicitly start lifetime of trivially-default-constructible union members #149835

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
Jul 23, 2025
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
9 changes: 8 additions & 1 deletion clang/lib/AST/ByteCode/Compiler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,18 @@ using APSInt = llvm::APSInt;
namespace clang {
namespace interp {

static bool hasTrivialDefaultCtorParent(const FieldDecl *FD) {
assert(FD);
assert(FD->getParent()->isUnion());
const auto *CXXRD = dyn_cast<CXXRecordDecl>(FD->getParent());
return !CXXRD || CXXRD->hasTrivialDefaultConstructor();
}

static bool refersToUnion(const Expr *E) {
for (;;) {
if (const auto *ME = dyn_cast<MemberExpr>(E)) {
if (const auto *FD = dyn_cast<FieldDecl>(ME->getMemberDecl());
FD && FD->getParent()->isUnion())
FD && FD->getParent()->isUnion() && hasTrivialDefaultCtorParent(FD))
Copy link
Contributor

Choose a reason for hiding this comment

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

Do you handle things like that?

struct s{
    union {
    union {
        int m;
    };
    };
};

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Handle what exactly? https://godbolt.org/z/581onze1G The diagnostics aren't a perfect 1:1 match but otherwise yes

return true;
E = ME->getBase();
continue;
Expand Down
55 changes: 55 additions & 0 deletions clang/test/AST/ByteCode/unions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -861,6 +861,61 @@ namespace CopyCtorMutable {
// both-note {{in call}}
}


namespace NonTrivialCtor {
struct A { int x = 1; constexpr int f() { return 1; } };
struct B : A { int y = 1; constexpr int g() { return 2; } };
struct C {
int x;
constexpr virtual int f() = 0;
};
struct D : C {
int y;
constexpr virtual int f() override { return 3; }
};

union U {
int n;
B b;
D d;
};

consteval int test(int which) {
if (which == 0) {}

U u{.n = 5};
assert_active(u);
assert_active(u.n);
assert_inactive(u.b);

switch (which) {
case 0:
u.b.x = 10; // both-note {{assignment to member 'b' of union with active member 'n'}}
return u.b.f();
case 1:
u.b.y = 10; // both-note {{assignment to member 'b' of union with active member 'n'}}
return u.b.g();
case 2:
u.d.x = 10; // both-note {{assignment to member 'd' of union with active member 'n'}}
return u.d.f();
case 3:
u.d.y = 10; // both-note {{assignment to member 'd' of union with active member 'n'}}
return u.d.f();
}

return 1;
}
static_assert(test(0)); // both-error {{not an integral constant expression}} \
// both-note {{in call}}
static_assert(test(1)); // both-error {{not an integral constant expression}} \
// both-note {{in call}}
static_assert(test(2)); // both-error {{not an integral constant expression}} \
// both-note {{in call}}
static_assert(test(3)); // both-error {{not an integral constant expression}} \
// both-note {{in call}}

}

#endif

namespace AddressComparison {
Expand Down
Loading