]> gcc.gnu.org Git - gcc.git/commitdiff
c++: aggregate CTAD and brace elision [PR101344]
authorPatrick Palka <ppalka@redhat.com>
Wed, 18 Aug 2021 12:37:45 +0000 (08:37 -0400)
committerPatrick Palka <ppalka@redhat.com>
Wed, 6 Oct 2021 13:43:13 +0000 (09:43 -0400)
Here the problem is ultimately that collect_ctor_idx_types always
recurses into an eligible sub-CONSTRUCTOR regardless of whether the
corresponding pair of braces was elided in the original initializer.
This causes us to reject some completely-braced forms of aggregate
CTAD as in the first testcase below, because collect_ctor_idx_types
effectively assumes that the original initializer is always minimally
braced (and so the aggregate deduction candidate is given a function
type that's incompatible with the original completely-braced initializer).

In order to fix this, collect_ctor_idx_types needs to somehow know the
shape of the original initializer when iterating over the reshaped
initializer.  To that end this patch makes reshape_init flag sub-ctors
that were built to undo brace elision in the original ctor, so that
collect_ctor_idx_types that determine whether to recurse into a sub-ctor
by simply inspecting this flag.

This happens to also fix PR101820, which is about aggregate CTAD using
designated initializers, for much the same reasons.

A curious case is the "intermediately-braced" initialization of 'e3'
(which we reject) in the first testcase below.  It seems to me we're
behaving as specified here (according to [over.match.class.deduct]/1)
because the initializer element x_1={1, 2, 3, 4} corresponds to the
subobject e_1=E::t, hence the type T_1 of the first function parameter
of the aggregate deduction candidate is T(&&)[2][2], but T can't be
deduced from x_1 using this parameter type (as opposed to say T(&&)[4]).

PR c++/101344
PR c++/101803

gcc/cp/ChangeLog:

* cp-tree.h (CONSTRUCTOR_BRACES_ELIDED_P): Define.
* decl.c (reshape_init_r): Set it.
* pt.c (collect_ctor_idx_types): Recurse into a sub-CONSTRUCTOR
iff CONSTRUCTOR_BRACES_ELIDED_P.

gcc/testsuite/ChangeLog:

* g++.dg/cpp2a/class-deduction-aggr11.C: New test.
* g++.dg/cpp2a/class-deduction-aggr12.C: New test.

(cherry picked from commit be4a4fb516688d7cfe28a80a4aa333f4ecf0b518)

gcc/cp/cp-tree.h
gcc/cp/decl.c
gcc/cp/pt.c
gcc/testsuite/g++.dg/cpp2a/class-deduction-aggr11.C [new file with mode: 0644]
gcc/testsuite/g++.dg/cpp2a/class-deduction-aggr12.C [new file with mode: 0644]

index 881cb4ab99956095ee779af8c2e645818d23edbb..97674a80df3e82f9ebf292dea5551a205f7790b5 100644 (file)
@@ -4467,6 +4467,12 @@ more_aggr_init_expr_args_p (const aggr_init_expr_arg_iterator *iter)
 #define CONSTRUCTOR_IS_PAREN_INIT(NODE) \
   (CONSTRUCTOR_CHECK(NODE)->base.private_flag)
 
+/* True if reshape_init built this sub-CONSTRUCTOR to undo the brace elision
+   of the original CONSTRUCTOR.  This flag is used during C++20 aggregate
+   CTAD.  */
+#define CONSTRUCTOR_BRACES_ELIDED_P(NODE) \
+  (CONSTRUCTOR_CHECK (NODE)->base.protected_flag)
+
 /* True if NODE represents a conversion for direct-initialization in a
    template.  Set by perform_implicit_conversion_flags.  */
 #define IMPLICIT_CONV_EXPR_DIRECT_INIT(NODE) \
index 9a6e3641aacc031af03eaf628d810f211cd3f223..f21db4d560dba857b73a35591a22f6a86972aae2 100644 (file)
@@ -6605,7 +6605,8 @@ reshape_init_r (tree type, reshape_iter *d, tree first_initializer_p,
   /* A non-aggregate type is always initialized with a single
      initializer.  */
   if (!CP_AGGREGATE_TYPE_P (type)
-      /* As is an array with dependent bound.  */
+      /* As is an array with dependent bound, which we can see
+        during C++20 aggregate CTAD.  */
       || (cxx_dialect >= cxx20
          && TREE_CODE (type) == ARRAY_TYPE
          && uses_template_parms (TYPE_DOMAIN (type))))
@@ -6720,6 +6721,7 @@ reshape_init_r (tree type, reshape_iter *d, tree first_initializer_p,
      initializer already, and there is not a CONSTRUCTOR, it means that there
      is a missing set of braces (that is, we are processing the case for
      which reshape_init exists).  */
+  bool braces_elided_p = false;
   if (!first_initializer_p)
     {
       if (TREE_CODE (stripped_init) == CONSTRUCTOR)
@@ -6755,17 +6757,25 @@ reshape_init_r (tree type, reshape_iter *d, tree first_initializer_p,
        warning (OPT_Wmissing_braces,
                 "missing braces around initializer for %qT",
                 type);
+      braces_elided_p = true;
     }
 
   /* Dispatch to specialized routines.  */
+  tree new_init;
   if (CLASS_TYPE_P (type))
-    return reshape_init_class (type, d, first_initializer_p, complain);
+    new_init = reshape_init_class (type, d, first_initializer_p, complain);
   else if (TREE_CODE (type) == ARRAY_TYPE)
-    return reshape_init_array (type, d, first_initializer_p, complain);
+    new_init = reshape_init_array (type, d, first_initializer_p, complain);
   else if (VECTOR_TYPE_P (type))
-    return reshape_init_vector (type, d, complain);
+    new_init = reshape_init_vector (type, d, complain);
   else
     gcc_unreachable();
+
+  if (braces_elided_p
+      && TREE_CODE (new_init) == CONSTRUCTOR)
+    CONSTRUCTOR_BRACES_ELIDED_P (new_init) = true;
+
+  return new_init;
 }
 
 /* Undo the brace-elision allowed by [dcl.init.aggr] in a
index 077097d91753641478dad94ecba4088e7ebee809..46ad819239ed204e47cfd469440dc14fdfc117f2 100644 (file)
@@ -28827,12 +28827,7 @@ collect_ctor_idx_types (tree ctor, tree list, tree elt = NULL_TREE)
     {
       tree ftype = elt ? elt : TREE_TYPE (idx);
       if (BRACE_ENCLOSED_INITIALIZER_P (val)
-         && CONSTRUCTOR_NELTS (val)
-         /* As in reshape_init_r, a non-aggregate or array-of-dependent-bound
-            type gets a single initializer.  */
-         && CP_AGGREGATE_TYPE_P (ftype)
-         && !(TREE_CODE (ftype) == ARRAY_TYPE
-              && uses_template_parms (TYPE_DOMAIN (ftype))))
+         && CONSTRUCTOR_BRACES_ELIDED_P (val))
        {
          tree subelt = NULL_TREE;
          if (TREE_CODE (ftype) == ARRAY_TYPE)
diff --git a/gcc/testsuite/g++.dg/cpp2a/class-deduction-aggr11.C b/gcc/testsuite/g++.dg/cpp2a/class-deduction-aggr11.C
new file mode 100644 (file)
index 0000000..c4806de
--- /dev/null
@@ -0,0 +1,29 @@
+// PR c++/101344
+// { dg-do compile { target c++20 } }
+
+template<class T=void>
+struct A { int m; int t[2]; };
+
+A a1{1, {2, 3}}; // previously rejected
+A a2{1, 2, 3};
+
+struct B { int x, y; };
+
+template<class T=void>
+struct C { int m; struct { int x, y; } t; };
+
+A b1{1, {2, 3}}; // previously rejected
+A b2{1, 2, 3};
+
+template<class T>
+struct D { T t[2]; };
+
+D d1{1, 2};
+D d2{{1, 2}}; // previously rejected
+
+template<class T>
+struct E { T t[2][2]; };
+
+E e1{1, 2, 3, 4};
+E e2{{{1, 2}, {3, 4}}}; // previously rejected
+E e3{{1, 2, 3, 4}}; // { dg-error "deduction|no match" }
diff --git a/gcc/testsuite/g++.dg/cpp2a/class-deduction-aggr12.C b/gcc/testsuite/g++.dg/cpp2a/class-deduction-aggr12.C
new file mode 100644 (file)
index 0000000..3e330ac
--- /dev/null
@@ -0,0 +1,15 @@
+// PR c++/101803
+// { dg-do compile { target c++20 } }
+
+struct Inner { int i = 0; };
+
+template <typename T = void>
+struct Outer { Inner s{}; };
+
+Outer o1{ .s = {} };                // works
+Outer o2{ .s = Inner{ .i = 1} };    // works
+Outer o3{ .s = { .i = 1} };         // does not
+
+Outer o4{ .s{} };                   // works
+Outer o5{ .s{Inner{ .i = 1} } };    // works
+Outer o6{ .s{ .i = 1} };            // does not
This page took 0.109794 seconds and 5 git commands to generate.