c++: Fix Bases(args...)... base initialization [PR88580]
authorPatrick Palka <ppalka@redhat.com>
Tue, 27 Apr 2021 18:18:25 +0000 (14:18 -0400)
committerPatrick Palka <ppalka@redhat.com>
Tue, 27 Apr 2021 18:18:25 +0000 (14:18 -0400)
When substituting into the arguments of a base initializer pack
expansion, tsubst_initializer_list uses a dummy EXPR_PACK_EXPANSION
in order to expand an initializer such as Bases(args)... into
Bases#{0}(args#{0}) and so on.  But when an argument inside the base
initializer is itself a pack expansion, as in Bases(args...)..., the
argument is already an EXPR_PACK_EXPANSION so we don't need to wrap it.
It's also independent from the outer expansion of Bases, so we need to
"multiplicatively" append the expansion of args... onto the argument
list of each expanded base.

gcc/cp/ChangeLog:

PR c++/88580
* pt.c (tsubst_initializer_list): Correctly handle the case
where an argument inside a base initializer pack expansion is
itself a pack expansion.

gcc/testsuite/ChangeLog:

PR c++/88580
* g++.dg/cpp0x/variadic182.C: New test.

gcc/cp/pt.c
gcc/testsuite/g++.dg/cpp0x/variadic182.C [new file with mode: 0644]

index 8c3c814..eaf4665 100644 (file)
@@ -26389,9 +26389,16 @@ tsubst_initializer_list (tree t, tree argvec)
                  tree expanded_exprs;
 
                  /* Expand the argument.  */
-                 SET_PACK_EXPANSION_PATTERN (expr, TREE_VALUE (arg));
+                 tree value;
+                 if (TREE_CODE (TREE_VALUE (arg)) == EXPR_PACK_EXPANSION)
+                   value = TREE_VALUE (arg);
+                 else
+                   {
+                     value = expr;
+                     SET_PACK_EXPANSION_PATTERN (value, TREE_VALUE (arg));
+                   }
                  expanded_exprs
-                   = tsubst_pack_expansion (expr, argvec,
+                   = tsubst_pack_expansion (value, argvec,
                                             tf_warning_or_error,
                                             NULL_TREE);
                  if (expanded_exprs == error_mark_node)
@@ -26400,12 +26407,17 @@ tsubst_initializer_list (tree t, tree argvec)
                  /* Prepend each of the expanded expressions to the
                     corresponding TREE_LIST in EXPANDED_ARGUMENTS.  */
                  for (i = 0; i < len; i++)
-                   {
-                     TREE_VEC_ELT (expanded_arguments, i) =
-                       tree_cons (NULL_TREE,
-                                  TREE_VEC_ELT (expanded_exprs, i),
-                                  TREE_VEC_ELT (expanded_arguments, i));
-                   }
+                   if (TREE_CODE (TREE_VALUE (arg)) == EXPR_PACK_EXPANSION)
+                     for (int j = 0; j < TREE_VEC_LENGTH (expanded_exprs); j++)
+                       TREE_VEC_ELT (expanded_arguments, i)
+                         = tree_cons (NULL_TREE,
+                                      TREE_VEC_ELT (expanded_exprs, j),
+                                      TREE_VEC_ELT (expanded_arguments, i));
+                   else
+                     TREE_VEC_ELT (expanded_arguments, i)
+                       = tree_cons (NULL_TREE,
+                                    TREE_VEC_ELT (expanded_exprs, i),
+                                    TREE_VEC_ELT (expanded_arguments, i));
                }
              in_base_initializer = 0;
 
diff --git a/gcc/testsuite/g++.dg/cpp0x/variadic182.C b/gcc/testsuite/g++.dg/cpp0x/variadic182.C
new file mode 100644 (file)
index 0000000..078de74
--- /dev/null
@@ -0,0 +1,18 @@
+// PR c++/88580
+// { dg-do compile { target c++11 } }
+
+template <class... Bases>
+struct Derived : Bases... {
+  template <class... Ts>
+  Derived(Ts... args) : Bases(args, args..., args)... { }
+};
+
+struct A { };
+struct B { };
+struct C { };
+
+struct Base1 { Base1(A, A, B, C, A); };
+struct Base2 { Base2(B, A, B, C, B); };
+struct Base3 { Base3(C, A, B, C, C); };
+
+Derived<Base1, Base2, Base3> d(A{}, B{}, C{});