]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
c++: designator and anon struct [PR101767]
authorJason Merrill <jason@redhat.com>
Fri, 18 Mar 2022 18:36:19 +0000 (14:36 -0400)
committerJason Merrill <jason@redhat.com>
Mon, 21 Mar 2022 20:46:31 +0000 (16:46 -0400)
We found .x in the anonymous struct, but then didn't find .y there; we
should decide that means we're done with the struct rather than that the
code is wrong.

PR c++/101767

gcc/cp/ChangeLog:

* decl.cc (reshape_init_class): Back out of anon struct
if a designator doesn't match.

gcc/testsuite/ChangeLog:

* g++.dg/ext/anon-struct10.C: New test.

gcc/cp/decl.cc
gcc/testsuite/g++.dg/ext/anon-struct10.C [new file with mode: 0644]

index 8afda8264c4e3c198ee17e6a37a24100c3bea9f5..375385e0013348e473283eef7ee19d7b6d408266 100644 (file)
@@ -6626,6 +6626,11 @@ reshape_init_class (tree type, reshape_iter *d, bool first_initializer_p,
              return error_mark_node;
            }
 
+         if (!field && ANON_AGGR_TYPE_P (type))
+           /* Apparently the designator isn't for a member of this anonymous
+              struct, so head back to the enclosing class.  */
+           break;
+
          if (!field || TREE_CODE (field) != FIELD_DECL)
            {
              if (complain & tf_error)
diff --git a/gcc/testsuite/g++.dg/ext/anon-struct10.C b/gcc/testsuite/g++.dg/ext/anon-struct10.C
new file mode 100644 (file)
index 0000000..9b01bf3
--- /dev/null
@@ -0,0 +1,21 @@
+// PR c++/101767
+// { dg-do compile { target c++11 } }
+// { dg-additional-options "-Wno-pedantic" }
+
+typedef struct {
+  struct {
+    int x;
+  };
+  union {
+    int y;
+    float z;
+  };
+} S;
+
+void foo(void)
+{
+  [[maybe_unused]] S a = {
+    .x = 1,
+    .y = 0
+  };
+}