Skip to content

Commit b069797

Browse files
committed
[clang][Interp] Don't zero-init unions
Zero-initializing them would accidentally activate the members.
1 parent 3b57f6b commit b069797

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

clang/lib/AST/Interp/Compiler.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2523,7 +2523,7 @@ bool Compiler<Emitter>::VisitCXXConstructExpr(const CXXConstructExpr *E) {
25232523
if (E->requiresZeroInitialization()) {
25242524
const Record *R = getRecord(E->getType());
25252525

2526-
if (!this->visitZeroRecordInitializer(R, E))
2526+
if (!R->isUnion() && !this->visitZeroRecordInitializer(R, E))
25272527
return false;
25282528

25292529
// If the constructor is trivial anyway, we're done.

clang/test/AST/Interp/unions.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,4 +253,19 @@ namespace Nested {
253253
// both-note {{in call to}}
254254

255255
}
256+
257+
258+
namespace Zeroing {
259+
struct non_trivial_constructor {
260+
constexpr non_trivial_constructor() : x(100) {}
261+
int x;
262+
};
263+
union U2 {
264+
int a{1000};
265+
non_trivial_constructor b;
266+
};
267+
268+
static_assert(U2().b.x == 100, ""); // both-error {{not an integral constant expression}} \
269+
// both-note {{read of member 'b' of union with active member 'a'}}
270+
}
256271
#endif

0 commit comments

Comments
 (0)