public inbox for gcc-cvs@sourceware.org
help / color / mirror / Atom feed
* [gcc r14-4109] c++: always check arity before deduction
@ 2023-09-18 18:41 Patrick Palka
0 siblings, 0 replies; only message in thread
From: Patrick Palka @ 2023-09-18 18:41 UTC (permalink / raw)
To: gcc-cvs
https://gcc.gnu.org/g:155178ccb5f5bc89dcc8261ae1b64bc2fbfdbd45
commit r14-4109-g155178ccb5f5bc89dcc8261ae1b64bc2fbfdbd45
Author: Patrick Palka <ppalka@redhat.com>
Date: Mon Sep 18 14:41:05 2023 -0400
c++: always check arity before deduction
This simple patch extends the r12-3271-gf1e73199569287 optimization
to happen for deduction without explicit template arguments as well.
The motivation for this is to accept testcases such as conv20.C and
ttp40.C below, which don't use explicit template arguments but for
which unnecessary template instantiation during deduction could be
avoided if we uniformly pruned overloads according to arity early.
This incidentally causes us to accept one reduced testcase from
PR c++/84075, but the underlying issue there remains at large.
As a nice side effect, this change causes the "candidate expects N
argument(s)" note during overload resolution failure to point to the
template candidate instead of the call site, which seems like an
improvement along the lines of r14-309-g14e881eb030509.
gcc/cp/ChangeLog:
* call.cc (add_template_candidate_real): Check arity even
when there are no explicit template arguments. Combine the
two adjacent '!obj' tests into one.
gcc/testsuite/ChangeLog:
* g++.dg/cpp0x/vt-57397-1.C: Expect "candidate expects ... N
argument(s)" at the declaration site instead of the call site.
* g++.dg/cpp0x/vt-57397-2.C: Likewise.
* g++.dg/overload/template5.C: Likewise.
* g++.dg/template/local6.C: Likewise.
* g++.dg/template/conv20.C: New test.
* g++.dg/template/ttp40.C: New test.
Diff:
---
gcc/cp/call.cc | 14 ++++++--------
gcc/testsuite/g++.dg/cpp0x/vt-57397-1.C | 6 +++---
gcc/testsuite/g++.dg/cpp0x/vt-57397-2.C | 6 +++---
gcc/testsuite/g++.dg/overload/template5.C | 4 ++--
gcc/testsuite/g++.dg/template/conv20.C | 17 +++++++++++++++++
gcc/testsuite/g++.dg/template/local6.C | 4 ++--
gcc/testsuite/g++.dg/template/ttp40.C | 25 +++++++++++++++++++++++++
7 files changed, 58 insertions(+), 18 deletions(-)
diff --git a/gcc/cp/call.cc b/gcc/cp/call.cc
index 399345307ea..2bbaeee039d 100644
--- a/gcc/cp/call.cc
+++ b/gcc/cp/call.cc
@@ -3535,13 +3535,13 @@ add_template_candidate_real (struct z_candidate **candidates, tree tmpl,
}
gcc_assert (ia == nargs_without_in_chrg);
- if (!obj && explicit_targs)
+ if (!obj)
{
/* Check that there's no obvious arity mismatch before proceeding with
deduction. This avoids substituting explicit template arguments
- into the template (which could result in an error outside the
- immediate context) when the resulting candidate would be unviable
- anyway. */
+ into the template or e.g. derived-to-base parm/arg unification
+ (which could result in an error outside the immediate context) when
+ the resulting candidate would be unviable anyway. */
int min_arity = 0, max_arity = 0;
tree parms = TYPE_ARG_TYPES (TREE_TYPE (tmpl));
parms = skip_artificial_parms_for (tmpl, parms);
@@ -3571,11 +3571,7 @@ add_template_candidate_real (struct z_candidate **candidates, tree tmpl,
reason = arity_rejection (NULL_TREE, max_arity, ia);
goto fail;
}
- }
- errs = errorcount+sorrycount;
- if (!obj)
- {
convs = alloc_conversions (nargs);
if (shortcut_bad_convs
@@ -3602,6 +3598,8 @@ add_template_candidate_real (struct z_candidate **candidates, tree tmpl,
}
}
}
+
+ errs = errorcount+sorrycount;
fn = fn_type_unification (tmpl, explicit_targs, targs,
args_without_in_chrg,
nargs_without_in_chrg,
diff --git a/gcc/testsuite/g++.dg/cpp0x/vt-57397-1.C b/gcc/testsuite/g++.dg/cpp0x/vt-57397-1.C
index 440bea5b2f7..bac3b64ad7e 100644
--- a/gcc/testsuite/g++.dg/cpp0x/vt-57397-1.C
+++ b/gcc/testsuite/g++.dg/cpp0x/vt-57397-1.C
@@ -3,20 +3,20 @@
template<class T1, class... Tn>
void foo(T1, Tn...);
+// { dg-message "candidate expects at least 1 argument, 0 provided" "" { target *-*-* } .-1 }
template<class T1, class T2, class... Tn>
void bar(T1, T2, Tn...);
+// { dg-message "candidate expects at least 2 arguments, 0 provided" "" { target *-*-* } .-1 }
+// { dg-message "candidate expects at least 2 arguments, 1 provided" "" { target *-*-* } .-2 }
int main()
{
foo(); // { dg-error "no matching" }
- // { dg-message "candidate expects at least 1 argument, 0 provided" "" { target *-*-* } .-1 }
foo(1);
foo(1, 2);
bar(); // { dg-error "no matching" }
- // { dg-message "candidate expects at least 2 arguments, 0 provided" "" { target *-*-* } .-1 }
bar(1); // { dg-error "no matching" }
- // { dg-message "candidate expects at least 2 arguments, 1 provided" "" { target *-*-* } .-1 }
bar(1, 2);
bar(1, 2, 3);
}
diff --git a/gcc/testsuite/g++.dg/cpp0x/vt-57397-2.C b/gcc/testsuite/g++.dg/cpp0x/vt-57397-2.C
index 1a99e22c5cb..22b19ef6c1a 100644
--- a/gcc/testsuite/g++.dg/cpp0x/vt-57397-2.C
+++ b/gcc/testsuite/g++.dg/cpp0x/vt-57397-2.C
@@ -3,21 +3,21 @@
template<class T1, class... Tn, class... Tm>
void foo(T1, Tn..., Tm...);
+// { dg-message "candidate expects at least 1 argument, 0 provided" "" { target *-*-* } .-1 }
template<class T1, class T2, class... Tn, class... Tm>
void bar(T1, T2, Tn..., Tm...);
+// { dg-message "candidate expects at least 2 arguments, 0 provided" "" { target *-*-* } .-1 }
+// { dg-message "candidate expects at least 2 arguments, 1 provided" "" { target *-*-* } .-2 }
int main()
{
foo(); // { dg-error "no matching" }
- // { dg-message "candidate expects at least 1 argument, 0 provided" "" { target *-*-* } .-1 }
foo(1);
foo(1, 2);
foo(1, 2, 3);
bar(); // { dg-error "no matching" }
- // { dg-message "candidate expects at least 2 arguments, 0 provided" "" { target *-*-* } .-1 }
bar(1); // { dg-error "no matching" }
- // { dg-message "candidate expects at least 2 arguments, 1 provided" "" { target *-*-* } .-1 }
bar(1, 2);
bar(1, 2, 3);
bar(1, 2, 3, 4);
diff --git a/gcc/testsuite/g++.dg/overload/template5.C b/gcc/testsuite/g++.dg/overload/template5.C
index 902684059a8..932c13bde30 100644
--- a/gcc/testsuite/g++.dg/overload/template5.C
+++ b/gcc/testsuite/g++.dg/overload/template5.C
@@ -2,14 +2,14 @@
template<typename T>
int low(T a, T b, T c) { return a + b + c; } // { dg-message "template" }
+// { dg-message "(candidate|3 arguments, 2 provided)" "" { target *-*-* } .-1 }
template<typename T>
int high(T a, T b, T c) { return a + b + c; } // { dg-message "template" }
+// { dg-message "(candidate|3 arguments, 4 provided)" "" { target *-*-* } .-1 }
void test (void)
{
low (5, 6); // { dg-error "no matching function" }
- // { dg-message "(candidate|3 arguments, 2 provided)" "" { target *-*-* } .-1 }
high (5, 6, 7, 8); // { dg-error "no matching function" }
- // { dg-message "(candidate|3 arguments, 4 provided)" "" { target *-*-* } .-1 }
}
diff --git a/gcc/testsuite/g++.dg/template/conv20.C b/gcc/testsuite/g++.dg/template/conv20.C
new file mode 100644
index 00000000000..202549a7eab
--- /dev/null
+++ b/gcc/testsuite/g++.dg/template/conv20.C
@@ -0,0 +1,17 @@
+// Verify we check arity early before deduction without explicit
+// template arguments.
+
+template<class T>
+struct A;
+
+template<class T>
+struct B : A<T> { };
+
+template<class T> void f(A<T>&, int); // #1
+template<class T> void f(B<T>&); // #2
+
+int main() {
+ extern B<int> b;
+ ::f(b); // OK, deduction for #1 short-circuited and B<int> not instantiated,
+ // which would have resulted in a hard error
+}
diff --git a/gcc/testsuite/g++.dg/template/local6.C b/gcc/testsuite/g++.dg/template/local6.C
index 94c19be56a2..d8ca5aa66eb 100644
--- a/gcc/testsuite/g++.dg/template/local6.C
+++ b/gcc/testsuite/g++.dg/template/local6.C
@@ -1,11 +1,11 @@
template <class T> struct PCVector2 // { dg-message "note" }
{
- template <class T2> PCVector2(const PCVector2<T> &cv) ; // { dg-message "note" }
+ template <class T2> PCVector2(const PCVector2<T> &cv) ; // { dg-message "candidate:" }
+ // { dg-message "(candidate|expects 1 argument, 2 provided|cannot convert)" "candidate note" { target *-*-* } .-1 }
PCVector2<T> operator- (const PCVector2<T> &ov) const
{
return PCVector2<T>(ov.xFIELD, ov.yFIELD); // { dg-error "matching" }
- // { dg-message "(candidate|expects 1 argument, 2 provided|cannot convert)" "candidate note" { target *-*-* } .-1 }
}
T xFIELD, yFIELD;
diff --git a/gcc/testsuite/g++.dg/template/ttp40.C b/gcc/testsuite/g++.dg/template/ttp40.C
new file mode 100644
index 00000000000..9d68b9e8bbc
--- /dev/null
+++ b/gcc/testsuite/g++.dg/template/ttp40.C
@@ -0,0 +1,25 @@
+// Verify we check arity early before deduction without explicit
+// template arguments.
+// PR c++/84075
+
+template<class T>
+struct trait {
+ static const int value = T::value; // { dg-bogus "not a member of 'B'" }
+};
+
+template<class T, int N = trait<T>::value>
+struct A { };
+
+template<class T>
+void f(A<T, 42>, int); // #1
+
+struct B { };
+
+template<template<class> class TT>
+void f(TT<B>); // #2
+
+int main() {
+ A<int, 42> a;
+ f(a, 0); // OK, deduction for #2 short-circuited and A<B> not specialized,
+ // which would have resulted in a hard error
+}
^ permalink raw reply [flat|nested] only message in thread
only message in thread, other threads:[~2023-09-18 18:41 UTC | newest]
Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-09-18 18:41 [gcc r14-4109] c++: always check arity before deduction Patrick Palka
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).