[clang] [Clang] Partial implementation of support for P3074 (trivial unions) (PR #146815)

via cfe-commits cfe-commits at lists.llvm.org
Fri Jul 11 08:06:30 PDT 2025


================
@@ -0,0 +1,80 @@
+// RUN: %clang_cc1 -verify -std=c++26 %s -Wno-defaulted-function-deleted -triple x86_64-linux-gnu
+
+struct NonTrivial {
+  int i;
+  constexpr NonTrivial(int i) :i(i) { }
+  constexpr ~NonTrivial() { }
+};
+
+union U0 {
+  NonTrivial nt;
+  int i;
+};
+U0 u0;
+
+// check for constant evaluation failure
+constexpr NonTrivial make() {
+    U0 u0;
+    return u0.nt;
+}
+constexpr NonTrivial nt = make(); // expected-error {{must be initialized by a constant expression}}}
+                                  // expected-note at -3 {{union with no active member}}
+                                  // expected-note at -4 {{in call to 'NonTrivial(u0.nt)'}}
+                                  // expected-note at -3 {{in call to 'make()'}}
+
+// overload resolution to select a constructor to default-initialize an object of type X either fails
+union U1 {
+  U1(int);
+  NonTrivial nt; // #1
+};
+U1 u1(1); // expected-error {{deleted function}} expected-note@#1 {{non-trivial destructor}}
+
+// or selects a constructor that is either deleted or not trivial, or
+union U2 {
+  U2() : nt(2) { }
+  NonTrivial nt; // #2
+};
+U2 u2; // expected-error {{deleted function}} expected-note@#2 {{non-trivial destructor}}
+
+union U3 {
+  U3() = delete;
+  U3(int);
+  NonTrivial nt; // #3
+};
+U3 u3(1); // expected-error {{deleted function}} expected-note@#3 {{non-trivial destructor}}
+
+// or X has a variant member V of class type M (or possibly multi-dimensional array thereof) where V has a default member initializer and M has a destructor that is non-trivial,
----------------
Sirraide wrote:

> or possibly multi-dimensional array thereof

Can you add a test for that case?

https://github.com/llvm/llvm-project/pull/146815


More information about the cfe-commits mailing list