]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
c++: anon union designated init [PR105925]
authorJason Merrill <jason@redhat.com>
Thu, 23 Jun 2022 20:04:02 +0000 (16:04 -0400)
committerJason Merrill <jason@redhat.com>
Thu, 23 Jun 2022 21:16:17 +0000 (17:16 -0400)
This testcase was failing because CONSTRUCTOR_IS_DESIGNATED_INIT wasn't
getting set on the introduced CONSTRUCTOR for the anonymous union, and
build_aggr_conv uses that flag to decide whether to pay attention to the
indexes of the CONSTRUCTOR.  So set the flag when we see a designator rather
than relying on copying it from another CONSTRUCTOR.

This avoids some redundant errors on desig4.C because we stop setting
CONSTRUCTOR_IS_DESIGNATED_INIT on _Complex CONSTRUCTORs where it's
nonsense.

PR c++/105925

gcc/cp/ChangeLog:

* decl.cc (reshape_init_array_1): Set
CONSTRUCTOR_IS_DESIGNATED_INIT here.
(reshape_init_class): And here.
(reshape_init): Not here.

gcc/testsuite/ChangeLog:

* g++.dg/ext/desig4.C: Remove extra errors.
* g++.dg/cpp2a/desig26.C: New test.

gcc/cp/decl.cc
gcc/testsuite/g++.dg/cpp2a/desig26.C [new file with mode: 0644]
gcc/testsuite/g++.dg/ext/desig4.C

index 29fc36534c2f512119fea969a4a960c9e9e17d9f..aa6cf3c6c2eb827d12f5badf7edfbae4884a11a3 100644 (file)
@@ -6505,6 +6505,8 @@ reshape_init_array_1 (tree elt_type, tree max_index, reshape_iter *d,
       tree elt_init;
       constructor_elt *old_cur = d->cur;
 
+      if (d->cur->index)
+       CONSTRUCTOR_IS_DESIGNATED_INIT (new_init) = true;
       check_array_designated_initializer (d->cur, index);
       elt_init = reshape_init_r (elt_type, d,
                                 /*first_initializer_p=*/NULL_TREE,
@@ -6674,6 +6676,7 @@ reshape_init_class (tree type, reshape_iter *d, bool first_initializer_p,
            }
          else if (TREE_CODE (d->cur->index) == IDENTIFIER_NODE)
            {
+             CONSTRUCTOR_IS_DESIGNATED_INIT (new_init) = true;
              field = get_class_binding (type, d->cur->index);
              direct_desig = true;
            }
@@ -7158,7 +7161,8 @@ reshape_init (tree type, tree init, tsubst_flags_t complain)
     CONSTRUCTOR_IS_DIRECT_INIT (new_init) = true;
   if (CONSTRUCTOR_IS_DESIGNATED_INIT (init)
       && BRACE_ENCLOSED_INITIALIZER_P (new_init))
-    CONSTRUCTOR_IS_DESIGNATED_INIT (new_init) = true;
+    gcc_checking_assert (CONSTRUCTOR_IS_DESIGNATED_INIT (new_init)
+                        || seen_error ());
 
   return new_init;
 }
diff --git a/gcc/testsuite/g++.dg/cpp2a/desig26.C b/gcc/testsuite/g++.dg/cpp2a/desig26.C
new file mode 100644 (file)
index 0000000..443fa3d
--- /dev/null
@@ -0,0 +1,22 @@
+// PR c++/105925
+// { dg-do compile { target c++20 } }
+
+struct V
+{
+    int i;
+    double d;
+};
+
+struct X
+{
+    union
+    {
+        int x;
+        V y;
+    };
+};
+
+X foo()
+{
+    return {.y = {0, 0.0}};
+}
index 902bd1ff0b622381748d9a663c45d185fa9ceb63..9b92a6de24935efd186a68ebc176d0a33e30a0c4 100644 (file)
@@ -6,13 +6,9 @@ int a = { .foo = 6 };               // { dg-error "designator" }
 int b = { [0] = 1 };                // { dg-error "12:designator .0." }
 _Complex float c = { .foo = 0,  1 }; // { dg-error "designator" }
                                     // { dg-error "either all initializer clauses should be designated or none of them should be" "" { target c++2a } .-1 }
-                                    // { dg-error "cannot convert" "" { target *-*-* } .-2 }
 _Complex float d = { [0] = 0,  1 };  // { dg-error "23:designator .0." }
                                     // { dg-error "either all initializer clauses should be designated or none of them should be" "" { target c++2a } .-1 }
-                                    // { dg-error "cannot convert" "" { target *-*-* } .-2 }
 _Complex float e = { 0, .foo = 1 };  // { dg-error "designator" }
                                     // { dg-error "either all initializer clauses should be designated or none of them should be" "" { target c++2a } .-1 }
-                                    // { dg-error "cannot convert" "" { target *-*-* } .-2 }
 _Complex float f = { 0, [0] = 1 };   // { dg-error "26:designator .0." }
                                     // { dg-error "either all initializer clauses should be designated or none of them should be" "" { target c++2a } .-1 }
-                                    // { dg-error "cannot convert" "" { target *-*-* } .-2 }