public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
From: Patrick Palka <ppalka@redhat.com>
To: gcc-patches@gcc.gnu.org
Cc: jason@redhat.com, Patrick Palka <ppalka@redhat.com>
Subject: [PATCH] c++: desig init in presence of list ctor [PR109871]
Date: Tue, 16 May 2023 11:38:11 -0400	[thread overview]
Message-ID: <20230516153811.3515353-1-ppalka@redhat.com> (raw)

add_list_candidates has logic to reject designated initialization of a
non-aggregate type, but this is inadvertendly being suppressed if the type
has a list constructor due to the order of case analysis, which in the
below testcase leads to us incorrectly treating the list initializer as
an ordinary non-designated one.  This patch fixes this by making us check
for invalid designated initialization sooner.

Bootstrapped and regtested on x86_64-pc-linux-gnu, does this look OK for
trunk and perhaps 13?  IIUC desig init is C++20 but we also accept it
with a pedwarn in earlier dialects, so not sure if this'd be suitable
for backporting.

	PR c++/109871

gcc/cp/ChangeLog:

	* call.cc (add_list_candidates): Check for invalid
	designated initialization sooner, even for types that have
	a list constructor.

gcc/testsuite/ChangeLog:

	* g++.dg/cpp0x/desig6.C: New test.
---
 gcc/cp/call.cc                      | 16 ++++++++--------
 gcc/testsuite/g++.dg/cpp0x/desig6.C | 16 ++++++++++++++++
 2 files changed, 24 insertions(+), 8 deletions(-)
 create mode 100644 gcc/testsuite/g++.dg/cpp0x/desig6.C

diff --git a/gcc/cp/call.cc b/gcc/cp/call.cc
index 48611bb16a3..908374a43c9 100644
--- a/gcc/cp/call.cc
+++ b/gcc/cp/call.cc
@@ -4129,6 +4129,14 @@ add_list_candidates (tree fns, tree first_arg,
   if (CONSTRUCTOR_NELTS (init_list) == 0
       && TYPE_HAS_DEFAULT_CONSTRUCTOR (totype))
     ;
+  else if (CONSTRUCTOR_IS_DESIGNATED_INIT (init_list)
+	   && !CP_AGGREGATE_TYPE_P (totype))
+    {
+      if (complain & tf_error)
+	error ("designated initializers cannot be used with a "
+	       "non-aggregate type %qT", totype);
+      return;
+    }
   /* If the class has a list ctor, try passing the list as a single
      argument first, but only consider list ctors.  */
   else if (TYPE_HAS_LIST_CTOR (totype))
@@ -4140,14 +4148,6 @@ add_list_candidates (tree fns, tree first_arg,
       if (any_strictly_viable (*candidates))
 	return;
     }
-  else if (CONSTRUCTOR_IS_DESIGNATED_INIT (init_list)
-	   && !CP_AGGREGATE_TYPE_P (totype))
-    {
-      if (complain & tf_error)
-	error ("designated initializers cannot be used with a "
-	       "non-aggregate type %qT", totype);
-      return;
-    }
 
   /* Expand the CONSTRUCTOR into a new argument vec.  */
   vec<tree, va_gc> *new_args;
diff --git a/gcc/testsuite/g++.dg/cpp0x/desig6.C b/gcc/testsuite/g++.dg/cpp0x/desig6.C
new file mode 100644
index 00000000000..8d4cf483176
--- /dev/null
+++ b/gcc/testsuite/g++.dg/cpp0x/desig6.C
@@ -0,0 +1,16 @@
+// PR c++/109871
+// { dg-do compile { target c++11 } }
+// { dg-options "" }
+
+#include <initializer_list>
+
+struct vector {
+  vector(std::initializer_list<int>); // #1
+  vector(int); // #2
+};
+
+void f(vector);
+
+int main() {
+  f({.blah = 42}); // { dg-error "designated" } previously incorrectly selected #2
+}
-- 
2.40.1.552.g91428f078b


             reply	other threads:[~2023-05-16 15:38 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-05-16 15:38 Patrick Palka [this message]
2023-05-16 16:17 ` Jason Merrill

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20230516153811.3515353-1-ppalka@redhat.com \
    --to=ppalka@redhat.com \
    --cc=gcc-patches@gcc.gnu.org \
    --cc=jason@redhat.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).