Fix PR c++/70347 (default member initializer not picked up by union)
authorPatrick Palka <ppalka@gcc.gnu.org>
Wed, 23 Mar 2016 21:08:32 +0000 (21:08 +0000)
committerPatrick Palka <ppalka@gcc.gnu.org>
Wed, 23 Mar 2016 21:08:32 +0000 (21:08 +0000)
gcc/cp/ChangeLog:

PR c++/70347
* typeck.c (process_init_constructor_union): If the initializer
is empty, use the union's NSDMI if it has one.

gcc/testsuite/ChangeLog:

PR c++/70347
* g++.dg/cpp1y/nsdmi-union1.C: New test.

From-SVN: r234443

gcc/cp/ChangeLog
gcc/cp/typeck2.c
gcc/testsuite/ChangeLog
gcc/testsuite/g++.dg/cpp1y/nsdmi-union1.C [new file with mode: 0644]

index 984a030286b754f5306db3375b92cf9d2ce3d932..b32c72dac83862b1d8ec2f152ed6be243a69b917 100644 (file)
@@ -1,3 +1,9 @@
+2016-03-23  Patrick Palka  <ppalka@gcc.gnu.org>
+
+       PR c++/70347
+       * typeck.c (process_init_constructor_union): If the initializer
+       is empty, use the union's NSDMI if it has one.
+
 2016-03-23  Patrick Palka  <ppalka@gcc.gnu.org>
 
        PR c++/70332
index 2a76c96c7d48d27e8082fc14924ccc02e132ed0c..4ab77cda387cb12cf33fa2952ad4a07693cd3e3b 100644 (file)
@@ -1499,9 +1499,24 @@ process_init_constructor_union (tree type, tree init,
   constructor_elt *ce;
   int len;
 
-  /* If the initializer was empty, use default zero initialization.  */
+  /* If the initializer was empty, use the union's NSDMI if it has one.
+     Otherwise use default zero initialization.  */
   if (vec_safe_is_empty (CONSTRUCTOR_ELTS (init)))
-    return 0;
+    {
+      for (tree field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
+       {
+         if (DECL_INITIAL (field))
+           {
+             CONSTRUCTOR_APPEND_ELT (CONSTRUCTOR_ELTS (init),
+                                     field,
+                                     get_nsdmi (field, /*in_ctor=*/false));
+             break;
+           }
+       }
+
+      if (vec_safe_is_empty (CONSTRUCTOR_ELTS (init)))
+       return 0;
+    }
 
   len = CONSTRUCTOR_ELTS (init)->length ();
   if (len > 1)
index 5e3227643855dd00a39625a78c90bfea9ffc24dc..3a5cf65c2ab66cc8f27a28a949155e2ea6f6ab30 100644 (file)
@@ -1,3 +1,8 @@
+2016-03-23  Patrick Palka  <ppalka@gcc.gnu.org>
+
+       PR c++/70347
+       * g++.dg/cpp1y/nsdmi-union1.C: New test.
+
 2016-03-23  Patrick Palka  <ppalka@gcc.gnu.org>
 
        PR c++/70332
diff --git a/gcc/testsuite/g++.dg/cpp1y/nsdmi-union1.C b/gcc/testsuite/g++.dg/cpp1y/nsdmi-union1.C
new file mode 100644 (file)
index 0000000..d9dd0bf
--- /dev/null
@@ -0,0 +1,33 @@
+// PR c++/70347
+// { dg-do run { target c++14 } }
+
+union A {
+  char a;
+  long b = -42;
+};
+
+struct B {
+  union {
+    char a = 10;
+    long b;
+  };
+};
+
+A c1{};
+A c2{4};
+B c3{};
+B c4{{9}};
+
+int main() {
+  if (c1.b != -42)
+    __builtin_abort ();
+
+  if (c2.a != 4)
+    __builtin_abort ();
+
+  if (c3.a != 10)
+    __builtin_abort ();
+
+  if (c4.a != 9)
+    __builtin_abort ();
+}