This patch fixes PR c++/79360, a regression from PR c++/70347.

The TYPE_FIELDS of a type may contain TYPE_DECLs and CONST_DECLs as well
as FIELD_DECLs, but when looking for an NSDMI we are only interested in
the FIELD_DECLs.  Otherwise we may try to initialize the union with the
DECL_INITIAL of a nested CONST_DECL.  Does this look OK to commit after
bootstrap + regtest?

gcc/cp/ChangeLog:

    PR c++/79360
    * typeck2.c (process_init_constructor_union): Consider only
    FIELD_DECLs when looking for an NSDMI.

gcc/testsuite/ChangeLog:

    PR c++/79360
    * g++.dg/cpp1y/nsdmi-union2.C: New test.
---
 gcc/cp/typeck2.c                          |  3 ++-
 gcc/testsuite/g++.dg/cpp1y/nsdmi-union2.C | 12 ++++++++++++
 2 files changed, 14 insertions(+), 1 deletion(-)
 create mode 100644 gcc/testsuite/g++.dg/cpp1y/nsdmi-union2.C

diff --git a/gcc/cp/typeck2.c b/gcc/cp/typeck2.c
index 014de5c..1e0354d 100644
--- a/gcc/cp/typeck2.c
+++ b/gcc/cp/typeck2.c
@@ -1510,7 +1510,8 @@ process_init_constructor_union (tree type, tree init,
     {
       for (tree field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
     {
-      if (DECL_INITIAL (field))
+      if (TREE_CODE (field) == FIELD_DECL
+          && DECL_INITIAL (field) != NULL_TREE)
         {
           CONSTRUCTOR_APPEND_ELT (CONSTRUCTOR_ELTS (init),
                       field,
diff --git a/gcc/testsuite/g++.dg/cpp1y/nsdmi-union2.C
b/gcc/testsuite/g++.dg/cpp1y/nsdmi-union2.C
new file mode 100644
index 0000000..08217d7
--- /dev/null
+++ b/gcc/testsuite/g++.dg/cpp1y/nsdmi-union2.C
@@ -0,0 +1,12 @@
+// PR c++/79360
+// { dg-do compile { target c++14 } }
+
+union U
+{
+  enum E { e };
+};
+
+struct A
+{
+  U u{};
+};
-- 
2.10.1.456.g9cf5127

Reply via email to