Hi!

As I've mentioned on Saturday, the CWG3130 patchset fail to bootstrap.
The problem is that we try to call build_value_init on anonymous unions
or structs, which doesn't work well when they don't have a default
constructor.
Now, if some non-trivial construction is needed, type will already have
either a user-provided constructor or at least non-trivial one, in that
case build_value_init_noctor isn't called at all.  So, this patch
just zero-initializes the anonymous aggregate members.

Bootstrapped/regtested on x86_64-linux and i686-linux, ok for trunk?

2026-05-25  Jakub Jelinek  <[email protected]>

        * init.cc (build_value_init_noctor): Zero initialize anonymous
        union/struct subobjects.  Formatting fix.

--- gcc/cp/init.cc.jj   2026-04-03 10:18:16.808121758 +0200
+++ gcc/cp/init.cc      2026-05-25 11:01:59.409845336 +0200
@@ -445,6 +445,20 @@ build_value_init_noctor (tree type, tsub
                  && integer_zerop (DECL_SIZE (field)))
                continue;
 
+             /* Zero-initialize anonymous union and struct members.  The
+                default constructor doesn't exist for those and if NSDMIs
+                are used, the containing type's default constructor is
+                non-trivial.  */
+             if (ANON_AGGR_TYPE_P (ftype))
+               {
+                 value = build_zero_init (ftype, NULL_TREE,
+                                          /*static_storage_p=*/false);
+                 if (value == error_mark_node)
+                   return error_mark_node;
+                 CONSTRUCTOR_APPEND_ELT (v, field, value);
+                 continue;
+               }
+
              /* We could skip vfields and fields of types with
                 user-defined constructors, but I think that won't improve
                 performance at all; it should be simpler in general just
@@ -461,7 +475,7 @@ build_value_init_noctor (tree type, tsub
              if (value == error_mark_node)
                return error_mark_node;
 
-             CONSTRUCTOR_APPEND_ELT(v, field, value);
+             CONSTRUCTOR_APPEND_ELT (v, field, value);
 
              /* We shouldn't have gotten here for anything that would need
                 non-trivial initialization, and gimplify_init_ctor_preeval

        Jakub

Reply via email to