[clang] [clang][Interp] Don't zero-init unions (PR #102744)
Timm Baeder via cfe-commits
cfe-commits at lists.llvm.org
Sat Aug 10 05:18:12 PDT 2024
https://github.com/tbaederr created https://github.com/llvm/llvm-project/pull/102744
Zero-initializing them would accidentally activate the members.
>From b0697976fe655ad9c08e95bc7095d28cda16dfda Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Timm=20B=C3=A4der?= <tbaeder at redhat.com>
Date: Sat, 10 Aug 2024 13:48:47 +0200
Subject: [PATCH] [clang][Interp] Don't zero-init unions
Zero-initializing them would accidentally activate the members.
---
clang/lib/AST/Interp/Compiler.cpp | 2 +-
clang/test/AST/Interp/unions.cpp | 15 +++++++++++++++
2 files changed, 16 insertions(+), 1 deletion(-)
diff --git a/clang/lib/AST/Interp/Compiler.cpp b/clang/lib/AST/Interp/Compiler.cpp
index d0e4d409b6580..45999e120b312 100644
--- a/clang/lib/AST/Interp/Compiler.cpp
+++ b/clang/lib/AST/Interp/Compiler.cpp
@@ -2523,7 +2523,7 @@ bool Compiler<Emitter>::VisitCXXConstructExpr(const CXXConstructExpr *E) {
if (E->requiresZeroInitialization()) {
const Record *R = getRecord(E->getType());
- if (!this->visitZeroRecordInitializer(R, E))
+ if (!R->isUnion() && !this->visitZeroRecordInitializer(R, E))
return false;
// If the constructor is trivial anyway, we're done.
diff --git a/clang/test/AST/Interp/unions.cpp b/clang/test/AST/Interp/unions.cpp
index a51f30cd9185b..40edebe5de407 100644
--- a/clang/test/AST/Interp/unions.cpp
+++ b/clang/test/AST/Interp/unions.cpp
@@ -253,4 +253,19 @@ namespace Nested {
// both-note {{in call to}}
}
+
+
+namespace Zeroing {
+ struct non_trivial_constructor {
+ constexpr non_trivial_constructor() : x(100) {}
+ int x;
+ };
+ union U2 {
+ int a{1000};
+ non_trivial_constructor b;
+ };
+
+ static_assert(U2().b.x == 100, ""); // both-error {{not an integral constant expression}} \
+ // both-note {{read of member 'b' of union with active member 'a'}}
+}
#endif
More information about the cfe-commits
mailing list