Hi again,
the below tries to also fix the second problem I presented in this
thread. Passes testing on x86_64-linux. How does it look?
Thanks,
Paolo.
///////////////////
Index: cp/pt.c
===================================================================
--- cp/pt.c (revision 213287)
+++ cp/pt.c (working copy)
@@ -5517,13 +5517,21 @@ unify_method_type_error (bool explain_p, tree arg)
}
static int
-unify_arity (bool explain_p, int have, int wanted)
+unify_arity (bool explain_p, int have, int wanted, bool least_p = false)
{
if (explain_p)
- inform_n (input_location, wanted,
- " candidate expects %d argument, %d provided",
- " candidate expects %d arguments, %d provided",
- wanted, have);
+ {
+ if (least_p)
+ inform_n (input_location, wanted,
+ " candidate expects at least %d argument, %d provided",
+ " candidate expects at least %d arguments, %d provided",
+ wanted, have);
+ else
+ inform_n (input_location, wanted,
+ " candidate expects %d argument, %d provided",
+ " candidate expects %d arguments, %d provided",
+ wanted, have);
+ }
return 1;
}
@@ -5534,9 +5542,10 @@ unify_too_many_arguments (bool explain_p, int have
}
static int
-unify_too_few_arguments (bool explain_p, int have, int wanted)
+unify_too_few_arguments (bool explain_p, int have, int wanted,
+ bool least_p = false)
{
- return unify_arity (explain_p, have, wanted);
+ return unify_arity (explain_p, have, wanted, least_p);
}
static int
@@ -16546,6 +16555,7 @@ type_unification_real (tree tparms,
const tree *args;
unsigned int nargs;
unsigned int ia;
+ bool non_deduced_pack;
gcc_assert (TREE_CODE (tparms) == TREE_VEC);
gcc_assert (xparms == NULL_TREE || TREE_CODE (xparms) == TREE_LIST);
@@ -16559,6 +16569,7 @@ type_unification_real (tree tparms,
parms = xparms;
args = xargs;
nargs = xnargs;
+ non_deduced_pack = false;
ia = 0;
while (parms && parms != void_list_node
@@ -16577,10 +16588,13 @@ type_unification_real (tree tparms,
parms = TREE_CHAIN (parms);
if (TREE_CODE (parm) == TYPE_PACK_EXPANSION)
- /* For a function parameter pack that does not occur at the
- end of the parameter-declaration-list, the type of the
- parameter pack is a non-deduced context. */
- continue;
+ {
+ /* For a function parameter pack that does not occur at the
+ end of the parameter-declaration-list, the type of the
+ parameter pack is a non-deduced context. */
+ non_deduced_pack = true;
+ continue;
+ }
arg = args[ia];
++ia;
@@ -16594,6 +16608,18 @@ type_unification_real (tree tparms,
&& parms != void_list_node
&& TREE_CODE (TREE_VALUE (parms)) == TYPE_PACK_EXPANSION)
{
+ /* Eg (c++/57397):
+
+ template<class T1, class... Tn, class... Tm>
+ void foo(T1, Tn..., Tm...);
+
+ int main()
+ {
+ foo(1, 2);
+ } */
+ if (ia < nargs && non_deduced_pack)
+ return unify_too_many_arguments (explain_p, nargs, ia);
+
/* Unify the remaining arguments with the pack expansion type. */
tree argvec;
tree parmvec = make_tree_vec (1);
@@ -16617,18 +16643,33 @@ type_unification_real (tree tparms,
are present, and the parm list isn't variadic. */
if (ia < nargs && parms == void_list_node)
return unify_too_many_arguments (explain_p, nargs, ia);
- /* Fail if parms are left and they don't have default values. */
+ /* Fail if parms are left and they don't have default values and
+ they aren't all deduced as empty packs, eg (c++/57397):
+
+ template<class T1, class... Tn, class... Tm>
+ void foo(T1, Tn..., Tm...);
+
+ int main()
+ {
+ foo(1);
+ } */
if (parms && parms != void_list_node
&& TREE_PURPOSE (parms) == NULL_TREE)
{
unsigned int count = nargs;
+ bool trailing_packs = false;
tree p = parms;
while (p && p != void_list_node)
{
- count++;
+ if (TREE_CODE (TREE_VALUE (p)) == TYPE_PACK_EXPANSION)
+ trailing_packs = true;
+ else
+ count++;
p = TREE_CHAIN (p);
}
- return unify_too_few_arguments (explain_p, ia, count);
+ if (count != nargs)
+ return unify_too_few_arguments (explain_p, ia, count,
+ trailing_packs);
}
if (!subr)
Index: testsuite/g++.dg/cpp0x/vt-57397-1.C
===================================================================
--- testsuite/g++.dg/cpp0x/vt-57397-1.C (revision 0)
+++ testsuite/g++.dg/cpp0x/vt-57397-1.C (working copy)
@@ -0,0 +1,22 @@
+// PR c++/57397
+// { dg-do compile { target c++11 } }
+
+template<class T1, class... Tn>
+void foo(T1, Tn...);
+
+template<class T1, class T2, class... Tn>
+void bar(T1, T2, Tn...);
+
+int main()
+{
+ foo(); // { dg-error "no matching" }
+ // { dg-message "candidate expects at least 1 argument, 0 provided" "" {
target *-*-* } 12 }
+ foo(1);
+ foo(1, 2);
+ bar(); // { dg-error "no matching" }
+ // { dg-message "candidate expects at least 2 arguments, 0 provided" "" {
target *-*-* } 16 }
+ bar(1); // { dg-error "no matching" }
+ // { dg-message "candidate expects at least 2 arguments, 1 provided" "" {
target *-*-* } 18 }
+ bar(1, 2);
+ bar(1, 2, 3);
+}
Index: testsuite/g++.dg/cpp0x/vt-57397-2.C
===================================================================
--- testsuite/g++.dg/cpp0x/vt-57397-2.C (revision 0)
+++ testsuite/g++.dg/cpp0x/vt-57397-2.C (working copy)
@@ -0,0 +1,22 @@
+// PR c++/57397
+// { dg-do compile { target c++11 } }
+
+template<class T1, class... Tn, class... Tm>
+void foo(T1, Tn..., Tm...);
+
+template<class T1, class T2, class... Tn, class... Tm>
+void bar(T1, T2, Tn..., Tm...);
+
+int main()
+{
+ foo(1);
+ foo(1, 2); // { dg-error "no matching" }
+ // { dg-message "candidate expects 1 argument, 2 provided" "" { target *-*-*
} 13 }
+ foo(1, 2, 3); // { dg-error "no matching" }
+ // { dg-message "candidate expects 1 argument, 3 provided" "" { target *-*-*
} 15 }
+ bar(1, 2);
+ bar(1, 2, 3); // { dg-error "no matching" }
+ // { dg-message "candidate expects 2 arguments, 3 provided" "" { target
*-*-* } 18 }
+ bar(1, 2, 3, 4); // { dg-error "no matching" }
+ // { dg-message "candidate expects 2 arguments, 4 provided" "" { target
*-*-* } 20 }
+}