Skip to content

[clang][bytecode] Diagnose member calls on inactive union fields #129709

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
Mar 4, 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
3 changes: 3 additions & 0 deletions clang/lib/AST/ByteCode/Interp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1341,6 +1341,9 @@ bool Call(InterpState &S, CodePtr OpPC, const Function *Func,
} else {
if (!CheckInvoke(S, OpPC, ThisPtr))
return cleanup();
if (!Func->isConstructor() &&
!CheckActive(S, OpPC, ThisPtr, AK_MemberCall))
return false;
}

if (Func->isConstructor() && !checkConstructor(S, OpPC, Func, ThisPtr))
Expand Down
18 changes: 18 additions & 0 deletions clang/test/AST/ByteCode/unions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -504,4 +504,22 @@ namespace AnonymousUnion {
static_assert(return_init_all().a.p == 7); // both-error {{}} \
// both-note {{read of member 'p' of union with no active member}}
}

namespace MemberCalls {
struct S {
constexpr bool foo() const { return true; }
};

constexpr bool foo() { // both-error {{never produces a constant expression}}
union {
int a;
S s;
} u;

u.a = 10;
return u.s.foo(); // both-note 2{{member call on member 's' of union with active member 'a'}}
}
static_assert(foo()); // both-error {{not an integral constant expression}} \
// both-note {{in call to}}
}
#endif