public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
* [pushed] c++: anon union designated init [PR105925]
@ 2022-06-23 21:16 Jason Merrill
  2022-06-23 21:17 ` [pushed] c++: designated init cleanup [PR105925] Jason Merrill
  0 siblings, 1 reply; 2+ messages in thread
From: Jason Merrill @ 2022-06-23 21:16 UTC (permalink / raw)
  To: gcc-patches

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.

Tested x86_64-pc-linux-gnu, applying to trunk.

	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                       |  6 +++++-
 gcc/testsuite/g++.dg/cpp2a/desig26.C | 22 ++++++++++++++++++++++
 gcc/testsuite/g++.dg/ext/desig4.C    |  4 ----
 3 files changed, 27 insertions(+), 5 deletions(-)
 create mode 100644 gcc/testsuite/g++.dg/cpp2a/desig26.C

diff --git a/gcc/cp/decl.cc b/gcc/cp/decl.cc
index 29fc36534c2..aa6cf3c6c2e 100644
--- a/gcc/cp/decl.cc
+++ b/gcc/cp/decl.cc
@@ -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
index 00000000000..443fa3d089d
--- /dev/null
+++ b/gcc/testsuite/g++.dg/cpp2a/desig26.C
@@ -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}};
+}
diff --git a/gcc/testsuite/g++.dg/ext/desig4.C b/gcc/testsuite/g++.dg/ext/desig4.C
index 902bd1ff0b6..9b92a6de249 100644
--- a/gcc/testsuite/g++.dg/ext/desig4.C
+++ b/gcc/testsuite/g++.dg/ext/desig4.C
@@ -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 }

base-commit: b00b95198e6720eb23a2618870d67800f6180fdd
-- 
2.27.0


^ permalink raw reply	[flat|nested] 2+ messages in thread

* [pushed] c++: designated init cleanup [PR105925]
  2022-06-23 21:16 [pushed] c++: anon union designated init [PR105925] Jason Merrill
@ 2022-06-23 21:17 ` Jason Merrill
  0 siblings, 0 replies; 2+ messages in thread
From: Jason Merrill @ 2022-06-23 21:17 UTC (permalink / raw)
  To: gcc-patches

build_aggr_conv expects to run after reshape_init, which will usually have
filled out all the CONSTRUCTOR indexes; there's no reason to limit using
those to the case where the user gave an explicit designator.

Tested x86_64-pc-linux-gnu, applying to trunk.

	PR c++/105925

gcc/cp/ChangeLog:

	* call.cc (build_aggr_conv): Don't depend on
	CONSTRUCTOR_IS_DESIGNATED_INIT.
---
 gcc/cp/call.cc | 60 +++++++++++++++++++++++++-------------------------
 1 file changed, 30 insertions(+), 30 deletions(-)

diff --git a/gcc/cp/call.cc b/gcc/cp/call.cc
index 4710c3777c5..f1dd8377628 100644
--- a/gcc/cp/call.cc
+++ b/gcc/cp/call.cc
@@ -969,7 +969,8 @@ build_aggr_conv (tree type, tree ctor, int flags, tsubst_flags_t complain)
   tree empty_ctor = NULL_TREE;
   hash_set<tree, true> pset;
 
-  /* We already called reshape_init in implicit_conversion.  */
+  /* We already called reshape_init in implicit_conversion, but it might not
+     have done anything in the case of parenthesized aggr init.  */
 
   /* The conversions within the init-list aren't affected by the enclosing
      context; they're always simple copy-initialization.  */
@@ -979,49 +980,48 @@ build_aggr_conv (tree type, tree ctor, int flags, tsubst_flags_t complain)
      to corresponding TREE_TYPE (ce->index) and mark those FIELD_DECLs as
      visited.  In the following loop then ignore already visited
      FIELD_DECLs.  */
-  if (CONSTRUCTOR_IS_DESIGNATED_INIT (ctor))
+  tree idx, val;
+  FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (ctor), i, idx, val)
     {
-      tree idx, val;
-      FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (ctor), i, idx, val)
+      if (!idx)
+	break;
+
+      gcc_checking_assert (TREE_CODE (idx) == FIELD_DECL);
+
+      tree ftype = TREE_TYPE (idx);
+      bool ok;
+
+      if (TREE_CODE (ftype) == ARRAY_TYPE)
+	ok = can_convert_array (ftype, val, flags, complain);
+      else
+	ok = can_convert_arg (ftype, TREE_TYPE (val), val, flags,
+			      complain);
+
+      if (!ok)
+	return NULL;
+
+      /* For unions, there should be just one initializer.  */
+      if (TREE_CODE (type) == UNION_TYPE)
 	{
-	  if (idx && TREE_CODE (idx) == FIELD_DECL)
-	    {
-	      tree ftype = TREE_TYPE (idx);
-	      bool ok;
-
-	      if (TREE_CODE (ftype) == ARRAY_TYPE)
-		ok = can_convert_array (ftype, val, flags, complain);
-	      else
-		ok = can_convert_arg (ftype, TREE_TYPE (val), val, flags,
-				      complain);
-
-	      if (!ok)
-		return NULL;
-	      /* For unions, there should be just one initializer.  */
-	      if (TREE_CODE (type) == UNION_TYPE)
-		{
-		  field = NULL_TREE;
-		  i = 1;
-		  break;
-		}
-	      pset.add (idx);
-	    }
-	  else
-	    return NULL;
+	  field = NULL_TREE;
+	  i = 1;
+	  break;
 	}
+      pset.add (idx);
     }
 
   for (; field; field = next_aggregate_field (DECL_CHAIN (field)))
     {
       tree ftype = TREE_TYPE (field);
-      tree val;
       bool ok;
 
       if (!pset.is_empty () && field_in_pset (pset, field))
 	continue;
       if (i < CONSTRUCTOR_NELTS (ctor))
 	{
-	  val = CONSTRUCTOR_ELT (ctor, i)->value;
+	  constructor_elt *ce = CONSTRUCTOR_ELT (ctor, i);
+	  gcc_checking_assert (!ce->index);
+	  val = ce->value;
 	  ++i;
 	}
       else if (DECL_INITIAL (field))

base-commit: d610ae121e8ecd738de4dc01e6ac11ecf7c2327e
-- 
2.27.0


^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2022-06-23 21:17 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-06-23 21:16 [pushed] c++: anon union designated init [PR105925] Jason Merrill
2022-06-23 21:17 ` [pushed] c++: designated init cleanup [PR105925] Jason Merrill

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).