https://gcc.gnu.org/g:0be038ed1edb1c289a3e59de1336d4cdef568671

commit r17-926-g0be038ed1edb1c289a3e59de1336d4cdef568671
Author: Jakub Jelinek <[email protected]>
Date:   Fri May 29 10:16:10 2026 +0200

    c++: Fix build_value_init_noctor anon aggr handling
    
    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.
    
    2026-05-29  Jakub Jelinek  <[email protected]>
    
            * init.cc (build_value_init_noctor): Zero initialize anonymous
            union/struct subobjects.  Formatting fix.

Diff:
---
 gcc/cp/init.cc | 17 ++++++++++++++---
 1 file changed, 14 insertions(+), 3 deletions(-)

diff --git a/gcc/cp/init.cc b/gcc/cp/init.cc
index d0929c35bfcb..975bbd6a61eb 100644
--- a/gcc/cp/init.cc
+++ b/gcc/cp/init.cc
@@ -445,6 +445,14 @@ build_value_init_noctor (tree type, tsubst_flags_t 
complain)
                  && 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);
+
              /* 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
@@ -455,13 +463,16 @@ build_value_init_noctor (tree type, tsubst_flags_t 
complain)
                 corresponding to base classes as well.  Thus, iterating
                 over TYPE_FIELDs will result in correct initialization of
                 all of the subobjects.  */
-             value = build_value_init (ftype, complain);
-             value = maybe_constant_init (value);
+             else
+               {
+                 value = build_value_init (ftype, complain);
+                 value = maybe_constant_init (value);
+               }
 
              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

Reply via email to