]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
c++: desig init in presence of list ctor [PR109871]
authorPatrick Palka <ppalka@redhat.com>
Tue, 16 May 2023 16:39:16 +0000 (12:39 -0400)
committerPatrick Palka <ppalka@redhat.com>
Tue, 16 May 2023 16:39:16 +0000 (12:39 -0400)
add_list_candidates has logic to reject designated initialization of a
non-aggregate type, but this is inadvertently 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 initializer list as if
it's non-designated.  This patch fixes this by making us check for invalid
designated initialization sooner.

PR c++/109871

gcc/cp/ChangeLog:

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

gcc/testsuite/ChangeLog:

* g++.dg/cpp2a/desig27.C: New test.

gcc/cp/call.cc
gcc/testsuite/g++.dg/cpp2a/desig27.C [new file with mode: 0644]

index 48611bb16a3a5c0546ad964f4c928d0437006dab..908374a43c989b10e81d9706952080f1e06e3f8c 100644 (file)
@@ -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/cpp2a/desig27.C b/gcc/testsuite/g++.dg/cpp2a/desig27.C
new file mode 100644 (file)
index 0000000..8d4cf48
--- /dev/null
@@ -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
+}