[v2,3/3] c++: note other candidates when diagnosing deletedness
Checks
Commit Message
With the previous two patches in place, we can now extend our
deletedness diagnostic to note the other considered candidates, e.g.:
deleted16.C: In function 'int main()':
deleted16.C:10:4: error: use of deleted function 'void f(int)'
10 | f(0);
| ~^~~
deleted16.C:5:6: note: declared here
5 | void f(int) = delete;
| ^
deleted16.C:5:6: note: candidate: 'void f(int)' (deleted)
deleted16.C:6:6: note: candidate: 'void f(...)'
6 | void f(...);
| ^
deleted16.C:7:6: note: candidate: 'void f(int, int)'
7 | void f(int, int);
| ^
deleted16.C:7:6: note: candidate expects 2 arguments, 1 provided
For now, these these notes are disabled when a deleted special member
function is selected because it introduces a lot of new "cannot bind
reference" errors in the testsuite when noting non-viable candidates,
e.g. in cpp0x/initlist-opt1.C we would need to expect an error when
noting unviability of A(A&&). (It'd be nice if we could downgrade such
errors into notes when noting candidates...)
gcc/cp/ChangeLog:
* call.cc (build_over_call): Call print_z_candidates when
diagnosing deletedness.
gcc/testsuite/ChangeLog:
* g++.dg/cpp0x/deleted16.C: New test.
---
gcc/cp/call.cc | 10 +++++++++-
gcc/testsuite/g++.dg/cpp0x/deleted16.C | 24 ++++++++++++++++++++++++
2 files changed, 33 insertions(+), 1 deletion(-)
create mode 100644 gcc/testsuite/g++.dg/cpp0x/deleted16.C
Comments
On 10/23/23 19:51, Patrick Palka wrote:
> With the previous two patches in place, we can now extend our
> deletedness diagnostic to note the other considered candidates, e.g.:
>
> deleted16.C: In function 'int main()':
> deleted16.C:10:4: error: use of deleted function 'void f(int)'
> 10 | f(0);
> | ~^~~
> deleted16.C:5:6: note: declared here
> 5 | void f(int) = delete;
> | ^
> deleted16.C:5:6: note: candidate: 'void f(int)' (deleted)
> deleted16.C:6:6: note: candidate: 'void f(...)'
> 6 | void f(...);
> | ^
> deleted16.C:7:6: note: candidate: 'void f(int, int)'
> 7 | void f(int, int);
> | ^
> deleted16.C:7:6: note: candidate expects 2 arguments, 1 provided
>
> For now, these these notes are disabled when a deleted special member
> function is selected because it introduces a lot of new "cannot bind
> reference" errors in the testsuite when noting non-viable candidates,
> e.g. in cpp0x/initlist-opt1.C we would need to expect an error when
> noting unviability of A(A&&). (It'd be nice if we could downgrade such
> errors into notes when noting candidates...)
What about my suggestion to make printing the other candidates an
opt-in, with the normal output just suggesting the use of that option
for more information, like -ftemplate-backtrace-limit?
Jason
@@ -9932,7 +9932,15 @@ build_over_call (struct z_candidate *cand, int flags, tsubst_flags_t complain)
if (DECL_DELETED_FN (fn))
{
if (complain & tf_error)
- mark_used (fn);
+ {
+ mark_used (fn);
+ /* Note the other candidates we considered unless we selected a
+ special member function since the mismatch reasons for other
+ candidates are usually uninteresting, e.g. rvalue vs lvalue
+ reference binding . */
+ if (cand->next && !special_memfn_p (fn))
+ print_z_candidates (input_location, cand, /*only_viable_p=*/false);
+ }
return error_mark_node;
}
new file mode 100644
@@ -0,0 +1,24 @@
+// Verify we note other candidates when a deleted function is
+// selected by overload resolution.
+// { dg-do compile { target c++11 } }
+
+void f(int) = delete; // { dg-message "declared here|candidate" }
+void f(...); // { dg-message "candidate" }
+void f(int, int); // { dg-message "candidate" }
+
+// An example where the perfect candidate optimization causes us
+// to ignore function templates.
+void g(int) = delete; // { dg-message "declared here|candidate" }
+template<class T> void g(T); // { dg-message "candidate" }
+
+// An example where we have a strictly viable candidate and
+// an incompletely considered bad candidate.
+template<class T> void h(T, T) = delete; // { dg-message "declared here|candidate" }
+void h(int*, int) = delete; // { dg-message "candidate" }
+
+int main() {
+ f(0); // { dg-error "deleted" }
+ g(0); // { dg-error "deleted" }
+ h(1, 1); // { dg-error "deleted" }
+ // { dg-error "invalid conversion" "" { target *-*-* } .-1 } when noting 2nd cand
+}