[2/2] c++: note other candidates when diagnosing deletedness

Message ID 20231010032300.1057862-2-ppalka@redhat.com
State Accepted
Headers
Series [1/2] c++: sort candidates according to viability |

Checks

Context Check Description
snail/gcc-patch-check success Github commit url

Commit Message

Patrick Palka Oct. 10, 2023, 3:23 a.m. UTC
  With the previous improvements in place, we can easily extend our
deletedness diagnostic to note the other candidates:

  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

These notes are disabled when a deleted special member function is
selected primarily 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 at
A(A&&).

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 | 11 +++++++++++
 2 files changed, 20 insertions(+), 1 deletion(-)
 create mode 100644 gcc/testsuite/g++.dg/cpp0x/deleted16.C
  

Patch

diff --git a/gcc/cp/call.cc b/gcc/cp/call.cc
index 648d383ca4e..55fd71636b1 100644
--- a/gcc/cp/call.cc
+++ b/gcc/cp/call.cc
@@ -9873,7 +9873,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;
     }
 
diff --git a/gcc/testsuite/g++.dg/cpp0x/deleted16.C b/gcc/testsuite/g++.dg/cpp0x/deleted16.C
new file mode 100644
index 00000000000..9fd2fbb1465
--- /dev/null
+++ b/gcc/testsuite/g++.dg/cpp0x/deleted16.C
@@ -0,0 +1,11 @@ 
+// 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" }
+
+int main() {
+  f(0); // { dg-error "deleted" }
+}