From: Patrick Palka Date: Tue, 27 Apr 2021 18:18:25 +0000 (-0400) Subject: c++: Fix Bases(args...)... base initialization [PR88580] X-Git-Tag: basepoints/gcc-13~8139 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=37d2b98100cefcb9d312d379473c12aa6d61fc62;p=thirdparty%2Fgcc.git c++: Fix Bases(args...)... base initialization [PR88580] 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. --- diff --git a/gcc/cp/pt.c b/gcc/cp/pt.c index 8c3c814ce559..eaf46659f85c 100644 --- a/gcc/cp/pt.c +++ b/gcc/cp/pt.c @@ -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 index 000000000000..078de740e014 --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp0x/variadic182.C @@ -0,0 +1,18 @@ +// PR c++/88580 +// { dg-do compile { target c++11 } } + +template +struct Derived : Bases... { + template + 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 d(A{}, B{}, C{});