diff mbox series

[v3,3/3] c++: note other candidates when diagnosing deletedness

Message ID 20231027195532.2566822-3-ppalka@redhat.com
State New
Headers show
Series [v3,1/3] c++: sort candidates according to viability | expand

Commit Message

Patrick Palka Oct. 27, 2023, 7:55 p.m. UTC
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

These notes are controlled by a new command line flag -fnote-all-cands,
which also controls whether we note ignored candidates more generally.

gcc/ChangeLog:

	* doc/invoke.texi (C++ Dialect Options): Document -fnote-all-cands.

gcc/c-family/ChangeLog:

	* c.opt: Add -fnote-all-cands.

gcc/cp/ChangeLog:

	* call.cc (print_z_candidates): Only print ignored candidates
	when -fnote-all-cands is set.
	(build_over_call): When diagnosing deletedness, call
	print_z_candidates if -fnote-all-cands is set.

gcc/testsuite/ChangeLog:

	* g++.dg/overload/error6.C: Pass -fnote-all-cands.
	* g++.dg/cpp0x/deleted16.C: New test.
---
 gcc/c-family/c.opt                     |  4 ++++
 gcc/cp/call.cc                         |  8 +++++++-
 gcc/doc/invoke.texi                    |  5 +++++
 gcc/testsuite/g++.dg/cpp0x/deleted16.C | 25 +++++++++++++++++++++++++
 gcc/testsuite/g++.dg/overload/error6.C |  1 +
 5 files changed, 42 insertions(+), 1 deletion(-)
 create mode 100644 gcc/testsuite/g++.dg/cpp0x/deleted16.C

Comments

Jason Merrill Oct. 27, 2023, 10:29 p.m. UTC | #1
On 10/27/23 15:55, 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
> 
> These notes are controlled by a new command line flag -fnote-all-cands,
> which also controls whether we note ignored candidates more generally.
> 
> gcc/ChangeLog:
> 
> 	* doc/invoke.texi (C++ Dialect Options): Document -fnote-all-cands.
> 
> gcc/c-family/ChangeLog:
> 
> 	* c.opt: Add -fnote-all-cands.
> 
> gcc/cp/ChangeLog:
> 
> 	* call.cc (print_z_candidates): Only print ignored candidates
> 	when -fnote-all-cands is set.
> 	(build_over_call): When diagnosing deletedness, call
> 	print_z_candidates if -fnote-all-cands is set.

My suggestion was also to suggest using the flag in cases where it would 
make a difference, e.g.

note: some candidates omitted, use '-fnote-all-cands' to display them

Maybe "-fdiagnostics-all-candidates"?

Jason
Patrick Palka Oct. 27, 2023, 11:22 p.m. UTC | #2
On Fri, 27 Oct 2023, Jason Merrill wrote:

> On 10/27/23 15:55, 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
> > 
> > These notes are controlled by a new command line flag -fnote-all-cands,
> > which also controls whether we note ignored candidates more generally.
> > 
> > gcc/ChangeLog:
> > 
> > 	* doc/invoke.texi (C++ Dialect Options): Document -fnote-all-cands.
> > 
> > gcc/c-family/ChangeLog:
> > 
> > 	* c.opt: Add -fnote-all-cands.
> > 
> > gcc/cp/ChangeLog:
> > 
> > 	* call.cc (print_z_candidates): Only print ignored candidates
> > 	when -fnote-all-cands is set.
> > 	(build_over_call): When diagnosing deletedness, call
> > 	print_z_candidates if -fnote-all-cands is set.
> 
> My suggestion was also to suggest using the flag in cases where it would make
> a difference, e.g.
> 
> note: some candidates omitted, use '-fnote-all-cands' to display them

Ah thanks, fixed.  That'll help a lot with discoverability of the flag.

> 
> Maybe "-fdiagnostics-all-candidates"?

Nice, that's a better name indeed :)

How does the following look?  Full bootstrap/regtest in progress.

Here's the output of e.g. deleted16a.C.  I think I'd prefer to not print
the source line when emitting the suggestion, but I don't know how to do
that properly (aside from e.g. emitting the note at UNKNOWN_LOCATION).

In file included from gcc/testsuite/g++.dg/cpp0x/deleted16a.C:4:
gcc/testsuite/g++.dg/cpp0x/deleted16.C: In function ‘int main()’:
gcc/testsuite/g++.dg/cpp0x/deleted16.C:21:4: error: use of deleted function ‘void f(int)’
   21 |   f(0); // { dg-error "deleted" }
      |   ~^~~
gcc/testsuite/g++.dg/cpp0x/deleted16.C:6:6: note: declared here
    6 | void f(int) = delete; // { dg-message "declared here" }
      |      ^
gcc/testsuite/g++.dg/cpp0x/deleted16.C:21:4: note: use ‘-fdiagnostics-all-candidates’ to display considered candidates
   21 |   f(0); // { dg-error "deleted" }
      |   ~^~~
gcc/testsuite/g++.dg/cpp0x/deleted16.C:22:4: error: use of deleted function ‘void g(int)’
   22 |   g(0); // { dg-error "deleted" }
      |   ~^~~
gcc/testsuite/g++.dg/cpp0x/deleted16.C:12:6: note: declared here
   12 | void g(int) = delete; // { dg-message "declared here" }
      |      ^
gcc/testsuite/g++.dg/cpp0x/deleted16.C:22:4: note: use ‘-fdiagnostics-all-candidates’ to display considered candidates
   22 |   g(0); // { dg-error "deleted" }
      |   ~^~~
gcc/testsuite/g++.dg/cpp0x/deleted16.C:23:4: error: use of deleted function ‘void h(T, T) [with T = int]’
   23 |   h(1, 1); // { dg-error "deleted" }
      |   ~^~~~~~
gcc/testsuite/g++.dg/cpp0x/deleted16.C:17:24: note: declared here
   17 | template<class T> void h(T, T) = delete; // { dg-message "declared here|candidate" }
      |                        ^
gcc/testsuite/g++.dg/cpp0x/deleted16.C:23:4: note: use ‘-fdiagnostics-all-candidates’ to display considered candidates
   23 |   h(1, 1); // { dg-error "deleted" }
      |   ~^~~~~~

-- >8 --


Subject: [PATCH 3/3] c++: note other candidates when diagnosing deletedness

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

These notes are controlled by a new command line flag
-fdiagnostics-all-candidates which also controls whether we note
ignored candidates more generally.

gcc/ChangeLog:

	* doc/invoke.texi (C++ Dialect Options): Document
	-fdiagnostics-all-candidates.

gcc/c-family/ChangeLog:

	* c.opt: Add -fdiagnostics-all-candidates.

gcc/cp/ChangeLog:

	* call.cc (print_z_candidates): Only print ignored candidates
	when -fdiagnostics-all-candidates is set, otherwise suggest
	the flag.
	(build_over_call): When diagnosing deletedness, note
	other candidates only if -fdiagnostics-all-candidates is
	set, otherwise suggest the flag.

gcc/testsuite/ChangeLog:

	* g++.dg/overload/error6.C: Pass -fdiagnostics-all-candidates.
	* g++.dg/cpp0x/deleted16.C: New test.
	* g++.dg/cpp0x/deleted16a.C: New test.
	* g++.dg/overload/error6a.C: New test.
---
 gcc/c-family/c.opt                      |  4 ++++
 gcc/cp/call.cc                          | 19 ++++++++++++++++++-
 gcc/doc/invoke.texi                     |  5 +++++
 gcc/testsuite/g++.dg/cpp0x/deleted16.C  | 25 +++++++++++++++++++++++++
 gcc/testsuite/g++.dg/cpp0x/deleted16a.C | 12 ++++++++++++
 gcc/testsuite/g++.dg/overload/error6.C  |  1 +
 gcc/testsuite/g++.dg/overload/error6a.C |  6 ++++++
 7 files changed, 71 insertions(+), 1 deletion(-)
 create mode 100644 gcc/testsuite/g++.dg/cpp0x/deleted16.C
 create mode 100644 gcc/testsuite/g++.dg/cpp0x/deleted16a.C
 create mode 100644 gcc/testsuite/g++.dg/overload/error6a.C

diff --git a/gcc/c-family/c.opt b/gcc/c-family/c.opt
index 44b9c862c14..c12c86bb8ed 100644
--- a/gcc/c-family/c.opt
+++ b/gcc/c-family/c.opt
@@ -1781,6 +1781,10 @@ fdiagnostics-show-template-tree
 C++ ObjC++ Var(flag_diagnostics_show_template_tree) Init(0)
 Print hierarchical comparisons when template types are mismatched.
 
+fdiagnostics-all-candidates
+C++ ObjC++ Var(flag_diagnostics_all_candidates)
+Note all candidates during overload resolution failure.
+
 fdirectives-only
 C ObjC C++ ObjC++
 Preprocess directives only.
diff --git a/gcc/cp/call.cc b/gcc/cp/call.cc
index 81cc029dddb..e9c099eedb8 100644
--- a/gcc/cp/call.cc
+++ b/gcc/cp/call.cc
@@ -4090,6 +4090,12 @@ print_z_candidates (location_t loc, struct z_candidate *candidates,
     {
       if (only_viable_p.is_true () && candidates->viable != 1)
 	break;
+      if (ignored_candidate_p (candidates) && !flag_diagnostics_all_candidates)
+	{
+	  inform (loc, "some candidates omitted; "
+		       "use %<-fdiagnostics-all-candidates%> to display them");
+	  break;
+	}
       print_z_candidate (loc, N_("candidate:"), candidates);
     }
 }
@@ -9933,7 +9939,18 @@ 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);
+	  if (cand->next)
+	    {
+	      if (flag_diagnostics_all_candidates)
+		print_z_candidates (input_location, cand, /*only_viable_p=*/false);
+	      else
+		inform (input_location,
+			"use %<-fdiagnostics-all-candidates%> to display "
+			"considered candidates");
+	    }
+	}
       return error_mark_node;
     }
 
diff --git a/gcc/doc/invoke.texi b/gcc/doc/invoke.texi
index 5a9284d635c..7e9f68d31b3 100644
--- a/gcc/doc/invoke.texi
+++ b/gcc/doc/invoke.texi
@@ -3311,6 +3311,11 @@ called.  If the handler returns, execution continues normally.
 @item -fcoroutines
 Enable support for the C++ coroutines extension (experimental).
 
+@opindex fdiagnostics-all-candidates
+@item -fdiagnostics-all-candidates
+Permit the C++ front end to note all candidates during overload resolution
+failure, including when a deleted function is selected.
+
 @opindex fno-elide-constructors
 @opindex felide-constructors
 @item -fno-elide-constructors
diff --git a/gcc/testsuite/g++.dg/cpp0x/deleted16.C b/gcc/testsuite/g++.dg/cpp0x/deleted16.C
new file mode 100644
index 00000000000..d4347942c42
--- /dev/null
+++ b/gcc/testsuite/g++.dg/cpp0x/deleted16.C
@@ -0,0 +1,25 @@
+// Verify -fdiagnostics-all-candidates makes us note other candidates
+// when a deleted function is selected by overload resolution.
+// { dg-do compile { target c++11 } }
+// { dg-additional-options "-fdiagnostics-all-candidates" }
+
+void f(int) = delete; // { dg-message "declared here" }
+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" }
+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
+}
diff --git a/gcc/testsuite/g++.dg/cpp0x/deleted16a.C b/gcc/testsuite/g++.dg/cpp0x/deleted16a.C
new file mode 100644
index 00000000000..e62306fa3d1
--- /dev/null
+++ b/gcc/testsuite/g++.dg/cpp0x/deleted16a.C
@@ -0,0 +1,12 @@
+// Verify we suggest -fdiagnostics-all-candidates when diagnosing
+// overload resolution selecting a deleted function.
+// { dg-do compile { target c++11 } }
+#include "deleted16.C"
+
+// { dg-error "deleted" "" { target *-*-* } 21 }
+// { dg-error "deleted" "" { target *-*-* } 22 }
+// { dg-error "deleted" "" { target *-*-* } 23 }
+
+// { dg-message "use '-fdiagnostics-all-candidates'" "" { target *-*-* } 21 }
+// { dg-message "use '-fdiagnostics-all-candidates'" "" { target *-*-* } 22 }
+// { dg-message "use '-fdiagnostics-all-candidates'" "" { target *-*-* } 23 }
diff --git a/gcc/testsuite/g++.dg/overload/error6.C b/gcc/testsuite/g++.dg/overload/error6.C
index 86a12eaa8de..3186a297bfc 100644
--- a/gcc/testsuite/g++.dg/overload/error6.C
+++ b/gcc/testsuite/g++.dg/overload/error6.C
@@ -1,5 +1,6 @@
 // Verify we note even non-template candidates when diagnosing
 // overload resolution failure for a template-id.
+// { dg-additional-options "-fdiagnostics-all-candidates" }
 
 template<class T> void f(T); // { dg-message "candidate" }
 void f(int); // { dg-message {candidate: 'void f\(int\)' \(ignored\)} }
diff --git a/gcc/testsuite/g++.dg/overload/error6a.C b/gcc/testsuite/g++.dg/overload/error6a.C
new file mode 100644
index 00000000000..e86ab5158ab
--- /dev/null
+++ b/gcc/testsuite/g++.dg/overload/error6a.C
@@ -0,0 +1,6 @@
+// Verify we suggest -fdiagnostics-all-candidates when there are
+// omitted candidates.
+#include "error6.C"
+
+// { dg-error "no match" "" { target *-*-* } 9 }
+// { dg-message "use '-fdiagnostics-all-candidates'" "" { target *-*-* } 9 }
Patrick Palka Oct. 27, 2023, 11:28 p.m. UTC | #3
On Fri, 27 Oct 2023, Patrick Palka wrote:

> On Fri, 27 Oct 2023, Jason Merrill wrote:
> 
> > On 10/27/23 15:55, 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
> > > 
> > > These notes are controlled by a new command line flag -fnote-all-cands,
> > > which also controls whether we note ignored candidates more generally.
> > > 
> > > gcc/ChangeLog:
> > > 
> > > 	* doc/invoke.texi (C++ Dialect Options): Document -fnote-all-cands.

It just occurred to me that this despite this flag being C++ specific, it
probably should be documented under "Diagnostic Message Formatting Options",
like -fdiagnostics-show-template-tree is.

> > > 
> > > gcc/c-family/ChangeLog:
> > > 
> > > 	* c.opt: Add -fnote-all-cands.
> > > 
> > > gcc/cp/ChangeLog:
> > > 
> > > 	* call.cc (print_z_candidates): Only print ignored candidates
> > > 	when -fnote-all-cands is set.
> > > 	(build_over_call): When diagnosing deletedness, call
> > > 	print_z_candidates if -fnote-all-cands is set.
> > 
> > My suggestion was also to suggest using the flag in cases where it would make
> > a difference, e.g.
> > 
> > note: some candidates omitted, use '-fnote-all-cands' to display them
> 
> Ah thanks, fixed.  That'll help a lot with discoverability of the flag.
> 
> > 
> > Maybe "-fdiagnostics-all-candidates"?
> 
> Nice, that's a better name indeed :)
> 
> How does the following look?  Full bootstrap/regtest in progress.
> 
> Here's the output of e.g. deleted16a.C.  I think I'd prefer to not print
> the source line when emitting the suggestion, but I don't know how to do
> that properly (aside from e.g. emitting the note at UNKNOWN_LOCATION).
> 
> In file included from gcc/testsuite/g++.dg/cpp0x/deleted16a.C:4:
> gcc/testsuite/g++.dg/cpp0x/deleted16.C: In function ‘int main()’:
> gcc/testsuite/g++.dg/cpp0x/deleted16.C:21:4: error: use of deleted function ‘void f(int)’
>    21 |   f(0); // { dg-error "deleted" }
>       |   ~^~~
> gcc/testsuite/g++.dg/cpp0x/deleted16.C:6:6: note: declared here
>     6 | void f(int) = delete; // { dg-message "declared here" }
>       |      ^
> gcc/testsuite/g++.dg/cpp0x/deleted16.C:21:4: note: use ‘-fdiagnostics-all-candidates’ to display considered candidates
>    21 |   f(0); // { dg-error "deleted" }
>       |   ~^~~
> gcc/testsuite/g++.dg/cpp0x/deleted16.C:22:4: error: use of deleted function ‘void g(int)’
>    22 |   g(0); // { dg-error "deleted" }
>       |   ~^~~
> gcc/testsuite/g++.dg/cpp0x/deleted16.C:12:6: note: declared here
>    12 | void g(int) = delete; // { dg-message "declared here" }
>       |      ^
> gcc/testsuite/g++.dg/cpp0x/deleted16.C:22:4: note: use ‘-fdiagnostics-all-candidates’ to display considered candidates
>    22 |   g(0); // { dg-error "deleted" }
>       |   ~^~~
> gcc/testsuite/g++.dg/cpp0x/deleted16.C:23:4: error: use of deleted function ‘void h(T, T) [with T = int]’
>    23 |   h(1, 1); // { dg-error "deleted" }
>       |   ~^~~~~~
> gcc/testsuite/g++.dg/cpp0x/deleted16.C:17:24: note: declared here
>    17 | template<class T> void h(T, T) = delete; // { dg-message "declared here|candidate" }
>       |                        ^
> gcc/testsuite/g++.dg/cpp0x/deleted16.C:23:4: note: use ‘-fdiagnostics-all-candidates’ to display considered candidates
>    23 |   h(1, 1); // { dg-error "deleted" }
>       |   ~^~~~~~
> 
> -- >8 --
> 
> 
> Subject: [PATCH 3/3] c++: note other candidates when diagnosing deletedness
> 
> 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
> 
> These notes are controlled by a new command line flag
> -fdiagnostics-all-candidates which also controls whether we note
> ignored candidates more generally.
> 
> gcc/ChangeLog:
> 
> 	* doc/invoke.texi (C++ Dialect Options): Document
> 	-fdiagnostics-all-candidates.
> 
> gcc/c-family/ChangeLog:
> 
> 	* c.opt: Add -fdiagnostics-all-candidates.
> 
> gcc/cp/ChangeLog:
> 
> 	* call.cc (print_z_candidates): Only print ignored candidates
> 	when -fdiagnostics-all-candidates is set, otherwise suggest
> 	the flag.
> 	(build_over_call): When diagnosing deletedness, note
> 	other candidates only if -fdiagnostics-all-candidates is
> 	set, otherwise suggest the flag.
> 
> gcc/testsuite/ChangeLog:
> 
> 	* g++.dg/overload/error6.C: Pass -fdiagnostics-all-candidates.
> 	* g++.dg/cpp0x/deleted16.C: New test.
> 	* g++.dg/cpp0x/deleted16a.C: New test.
> 	* g++.dg/overload/error6a.C: New test.
> ---
>  gcc/c-family/c.opt                      |  4 ++++
>  gcc/cp/call.cc                          | 19 ++++++++++++++++++-
>  gcc/doc/invoke.texi                     |  5 +++++
>  gcc/testsuite/g++.dg/cpp0x/deleted16.C  | 25 +++++++++++++++++++++++++
>  gcc/testsuite/g++.dg/cpp0x/deleted16a.C | 12 ++++++++++++
>  gcc/testsuite/g++.dg/overload/error6.C  |  1 +
>  gcc/testsuite/g++.dg/overload/error6a.C |  6 ++++++
>  7 files changed, 71 insertions(+), 1 deletion(-)
>  create mode 100644 gcc/testsuite/g++.dg/cpp0x/deleted16.C
>  create mode 100644 gcc/testsuite/g++.dg/cpp0x/deleted16a.C
>  create mode 100644 gcc/testsuite/g++.dg/overload/error6a.C
> 
> diff --git a/gcc/c-family/c.opt b/gcc/c-family/c.opt
> index 44b9c862c14..c12c86bb8ed 100644
> --- a/gcc/c-family/c.opt
> +++ b/gcc/c-family/c.opt
> @@ -1781,6 +1781,10 @@ fdiagnostics-show-template-tree
>  C++ ObjC++ Var(flag_diagnostics_show_template_tree) Init(0)
>  Print hierarchical comparisons when template types are mismatched.
>  
> +fdiagnostics-all-candidates
> +C++ ObjC++ Var(flag_diagnostics_all_candidates)
> +Note all candidates during overload resolution failure.
> +
>  fdirectives-only
>  C ObjC C++ ObjC++
>  Preprocess directives only.
> diff --git a/gcc/cp/call.cc b/gcc/cp/call.cc
> index 81cc029dddb..e9c099eedb8 100644
> --- a/gcc/cp/call.cc
> +++ b/gcc/cp/call.cc
> @@ -4090,6 +4090,12 @@ print_z_candidates (location_t loc, struct z_candidate *candidates,
>      {
>        if (only_viable_p.is_true () && candidates->viable != 1)
>  	break;
> +      if (ignored_candidate_p (candidates) && !flag_diagnostics_all_candidates)
> +	{
> +	  inform (loc, "some candidates omitted; "
> +		       "use %<-fdiagnostics-all-candidates%> to display them");
> +	  break;
> +	}
>        print_z_candidate (loc, N_("candidate:"), candidates);
>      }
>  }
> @@ -9933,7 +9939,18 @@ 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);
> +	  if (cand->next)
> +	    {
> +	      if (flag_diagnostics_all_candidates)
> +		print_z_candidates (input_location, cand, /*only_viable_p=*/false);
> +	      else
> +		inform (input_location,
> +			"use %<-fdiagnostics-all-candidates%> to display "
> +			"considered candidates");
> +	    }
> +	}
>        return error_mark_node;
>      }
>  
> diff --git a/gcc/doc/invoke.texi b/gcc/doc/invoke.texi
> index 5a9284d635c..7e9f68d31b3 100644
> --- a/gcc/doc/invoke.texi
> +++ b/gcc/doc/invoke.texi
> @@ -3311,6 +3311,11 @@ called.  If the handler returns, execution continues normally.
>  @item -fcoroutines
>  Enable support for the C++ coroutines extension (experimental).
>  
> +@opindex fdiagnostics-all-candidates
> +@item -fdiagnostics-all-candidates
> +Permit the C++ front end to note all candidates during overload resolution
> +failure, including when a deleted function is selected.
> +
>  @opindex fno-elide-constructors
>  @opindex felide-constructors
>  @item -fno-elide-constructors
> diff --git a/gcc/testsuite/g++.dg/cpp0x/deleted16.C b/gcc/testsuite/g++.dg/cpp0x/deleted16.C
> new file mode 100644
> index 00000000000..d4347942c42
> --- /dev/null
> +++ b/gcc/testsuite/g++.dg/cpp0x/deleted16.C
> @@ -0,0 +1,25 @@
> +// Verify -fdiagnostics-all-candidates makes us note other candidates
> +// when a deleted function is selected by overload resolution.
> +// { dg-do compile { target c++11 } }
> +// { dg-additional-options "-fdiagnostics-all-candidates" }
> +
> +void f(int) = delete; // { dg-message "declared here" }
> +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" }
> +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
> +}
> diff --git a/gcc/testsuite/g++.dg/cpp0x/deleted16a.C b/gcc/testsuite/g++.dg/cpp0x/deleted16a.C
> new file mode 100644
> index 00000000000..e62306fa3d1
> --- /dev/null
> +++ b/gcc/testsuite/g++.dg/cpp0x/deleted16a.C
> @@ -0,0 +1,12 @@
> +// Verify we suggest -fdiagnostics-all-candidates when diagnosing
> +// overload resolution selecting a deleted function.
> +// { dg-do compile { target c++11 } }
> +#include "deleted16.C"
> +
> +// { dg-error "deleted" "" { target *-*-* } 21 }
> +// { dg-error "deleted" "" { target *-*-* } 22 }
> +// { dg-error "deleted" "" { target *-*-* } 23 }
> +
> +// { dg-message "use '-fdiagnostics-all-candidates'" "" { target *-*-* } 21 }
> +// { dg-message "use '-fdiagnostics-all-candidates'" "" { target *-*-* } 22 }
> +// { dg-message "use '-fdiagnostics-all-candidates'" "" { target *-*-* } 23 }
> diff --git a/gcc/testsuite/g++.dg/overload/error6.C b/gcc/testsuite/g++.dg/overload/error6.C
> index 86a12eaa8de..3186a297bfc 100644
> --- a/gcc/testsuite/g++.dg/overload/error6.C
> +++ b/gcc/testsuite/g++.dg/overload/error6.C
> @@ -1,5 +1,6 @@
>  // Verify we note even non-template candidates when diagnosing
>  // overload resolution failure for a template-id.
> +// { dg-additional-options "-fdiagnostics-all-candidates" }
>  
>  template<class T> void f(T); // { dg-message "candidate" }
>  void f(int); // { dg-message {candidate: 'void f\(int\)' \(ignored\)} }
> diff --git a/gcc/testsuite/g++.dg/overload/error6a.C b/gcc/testsuite/g++.dg/overload/error6a.C
> new file mode 100644
> index 00000000000..e86ab5158ab
> --- /dev/null
> +++ b/gcc/testsuite/g++.dg/overload/error6a.C
> @@ -0,0 +1,6 @@
> +// Verify we suggest -fdiagnostics-all-candidates when there are
> +// omitted candidates.
> +#include "error6.C"
> +
> +// { dg-error "no match" "" { target *-*-* } 9 }
> +// { dg-message "use '-fdiagnostics-all-candidates'" "" { target *-*-* } 9 }
> -- 
> 2.42.0.482.g2e8e77cbac
>
Patrick Palka Nov. 30, 2023, 3:46 p.m. UTC | #4
On Fri, 27 Oct 2023, Patrick Palka wrote:

> On Fri, 27 Oct 2023, Jason Merrill wrote:
> 
> > On 10/27/23 15:55, 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
> > > 
> > > These notes are controlled by a new command line flag -fnote-all-cands,
> > > which also controls whether we note ignored candidates more generally.
> > > 
> > > gcc/ChangeLog:
> > > 
> > > 	* doc/invoke.texi (C++ Dialect Options): Document -fnote-all-cands.
> > > 
> > > gcc/c-family/ChangeLog:
> > > 
> > > 	* c.opt: Add -fnote-all-cands.
> > > 
> > > gcc/cp/ChangeLog:
> > > 
> > > 	* call.cc (print_z_candidates): Only print ignored candidates
> > > 	when -fnote-all-cands is set.
> > > 	(build_over_call): When diagnosing deletedness, call
> > > 	print_z_candidates if -fnote-all-cands is set.
> > 
> > My suggestion was also to suggest using the flag in cases where it would make
> > a difference, e.g.
> > 
> > note: some candidates omitted, use '-fnote-all-cands' to display them
> 
> Ah thanks, fixed.  That'll help a lot with discoverability of the flag.
> 
> > 
> > Maybe "-fdiagnostics-all-candidates"?
> 
> Nice, that's a better name indeed :)
> 
> How does the following look?  Full bootstrap/regtest in progress.
> 
> Here's the output of e.g. deleted16a.C.  I think I'd prefer to not print
> the source line when emitting the suggestion, but I don't know how to do
> that properly (aside from e.g. emitting the note at UNKNOWN_LOCATION).
> 
> In file included from gcc/testsuite/g++.dg/cpp0x/deleted16a.C:4:
> gcc/testsuite/g++.dg/cpp0x/deleted16.C: In function ‘int main()’:
> gcc/testsuite/g++.dg/cpp0x/deleted16.C:21:4: error: use of deleted function ‘void f(int)’
>    21 |   f(0); // { dg-error "deleted" }
>       |   ~^~~
> gcc/testsuite/g++.dg/cpp0x/deleted16.C:6:6: note: declared here
>     6 | void f(int) = delete; // { dg-message "declared here" }
>       |      ^
> gcc/testsuite/g++.dg/cpp0x/deleted16.C:21:4: note: use ‘-fdiagnostics-all-candidates’ to display considered candidates
>    21 |   f(0); // { dg-error "deleted" }
>       |   ~^~~
> gcc/testsuite/g++.dg/cpp0x/deleted16.C:22:4: error: use of deleted function ‘void g(int)’
>    22 |   g(0); // { dg-error "deleted" }
>       |   ~^~~
> gcc/testsuite/g++.dg/cpp0x/deleted16.C:12:6: note: declared here
>    12 | void g(int) = delete; // { dg-message "declared here" }
>       |      ^
> gcc/testsuite/g++.dg/cpp0x/deleted16.C:22:4: note: use ‘-fdiagnostics-all-candidates’ to display considered candidates
>    22 |   g(0); // { dg-error "deleted" }
>       |   ~^~~
> gcc/testsuite/g++.dg/cpp0x/deleted16.C:23:4: error: use of deleted function ‘void h(T, T) [with T = int]’
>    23 |   h(1, 1); // { dg-error "deleted" }
>       |   ~^~~~~~
> gcc/testsuite/g++.dg/cpp0x/deleted16.C:17:24: note: declared here
>    17 | template<class T> void h(T, T) = delete; // { dg-message "declared here|candidate" }
>       |                        ^
> gcc/testsuite/g++.dg/cpp0x/deleted16.C:23:4: note: use ‘-fdiagnostics-all-candidates’ to display considered candidates
>    23 |   h(1, 1); // { dg-error "deleted" }
>       |   ~^~~~~~
> 
> -- >8 --
> 
> 
> Subject: [PATCH 3/3] c++: note other candidates when diagnosing deletedness
> 
> 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
> 
> These notes are controlled by a new command line flag
> -fdiagnostics-all-candidates which also controls whether we note
> ignored candidates more generally.
> 
> gcc/ChangeLog:
> 
> 	* doc/invoke.texi (C++ Dialect Options): Document
> 	-fdiagnostics-all-candidates.
> 
> gcc/c-family/ChangeLog:
> 
> 	* c.opt: Add -fdiagnostics-all-candidates.
> 
> gcc/cp/ChangeLog:
> 
> 	* call.cc (print_z_candidates): Only print ignored candidates
> 	when -fdiagnostics-all-candidates is set, otherwise suggest
> 	the flag.
> 	(build_over_call): When diagnosing deletedness, note
> 	other candidates only if -fdiagnostics-all-candidates is
> 	set, otherwise suggest the flag.
> 
> gcc/testsuite/ChangeLog:
> 
> 	* g++.dg/overload/error6.C: Pass -fdiagnostics-all-candidates.
> 	* g++.dg/cpp0x/deleted16.C: New test.
> 	* g++.dg/cpp0x/deleted16a.C: New test.
> 	* g++.dg/overload/error6a.C: New test.

Ping.

> ---
>  gcc/c-family/c.opt                      |  4 ++++
>  gcc/cp/call.cc                          | 19 ++++++++++++++++++-
>  gcc/doc/invoke.texi                     |  5 +++++
>  gcc/testsuite/g++.dg/cpp0x/deleted16.C  | 25 +++++++++++++++++++++++++
>  gcc/testsuite/g++.dg/cpp0x/deleted16a.C | 12 ++++++++++++
>  gcc/testsuite/g++.dg/overload/error6.C  |  1 +
>  gcc/testsuite/g++.dg/overload/error6a.C |  6 ++++++
>  7 files changed, 71 insertions(+), 1 deletion(-)
>  create mode 100644 gcc/testsuite/g++.dg/cpp0x/deleted16.C
>  create mode 100644 gcc/testsuite/g++.dg/cpp0x/deleted16a.C
>  create mode 100644 gcc/testsuite/g++.dg/overload/error6a.C
> 
> diff --git a/gcc/c-family/c.opt b/gcc/c-family/c.opt
> index 44b9c862c14..c12c86bb8ed 100644
> --- a/gcc/c-family/c.opt
> +++ b/gcc/c-family/c.opt
> @@ -1781,6 +1781,10 @@ fdiagnostics-show-template-tree
>  C++ ObjC++ Var(flag_diagnostics_show_template_tree) Init(0)
>  Print hierarchical comparisons when template types are mismatched.
>  
> +fdiagnostics-all-candidates
> +C++ ObjC++ Var(flag_diagnostics_all_candidates)
> +Note all candidates during overload resolution failure.
> +
>  fdirectives-only
>  C ObjC C++ ObjC++
>  Preprocess directives only.
> diff --git a/gcc/cp/call.cc b/gcc/cp/call.cc
> index 81cc029dddb..e9c099eedb8 100644
> --- a/gcc/cp/call.cc
> +++ b/gcc/cp/call.cc
> @@ -4090,6 +4090,12 @@ print_z_candidates (location_t loc, struct z_candidate *candidates,
>      {
>        if (only_viable_p.is_true () && candidates->viable != 1)
>  	break;
> +      if (ignored_candidate_p (candidates) && !flag_diagnostics_all_candidates)
> +	{
> +	  inform (loc, "some candidates omitted; "
> +		       "use %<-fdiagnostics-all-candidates%> to display them");
> +	  break;
> +	}
>        print_z_candidate (loc, N_("candidate:"), candidates);
>      }
>  }
> @@ -9933,7 +9939,18 @@ 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);
> +	  if (cand->next)
> +	    {
> +	      if (flag_diagnostics_all_candidates)
> +		print_z_candidates (input_location, cand, /*only_viable_p=*/false);
> +	      else
> +		inform (input_location,
> +			"use %<-fdiagnostics-all-candidates%> to display "
> +			"considered candidates");
> +	    }
> +	}
>        return error_mark_node;
>      }
>  
> diff --git a/gcc/doc/invoke.texi b/gcc/doc/invoke.texi
> index 5a9284d635c..7e9f68d31b3 100644
> --- a/gcc/doc/invoke.texi
> +++ b/gcc/doc/invoke.texi
> @@ -3311,6 +3311,11 @@ called.  If the handler returns, execution continues normally.
>  @item -fcoroutines
>  Enable support for the C++ coroutines extension (experimental).
>  
> +@opindex fdiagnostics-all-candidates
> +@item -fdiagnostics-all-candidates
> +Permit the C++ front end to note all candidates during overload resolution
> +failure, including when a deleted function is selected.
> +
>  @opindex fno-elide-constructors
>  @opindex felide-constructors
>  @item -fno-elide-constructors
> diff --git a/gcc/testsuite/g++.dg/cpp0x/deleted16.C b/gcc/testsuite/g++.dg/cpp0x/deleted16.C
> new file mode 100644
> index 00000000000..d4347942c42
> --- /dev/null
> +++ b/gcc/testsuite/g++.dg/cpp0x/deleted16.C
> @@ -0,0 +1,25 @@
> +// Verify -fdiagnostics-all-candidates makes us note other candidates
> +// when a deleted function is selected by overload resolution.
> +// { dg-do compile { target c++11 } }
> +// { dg-additional-options "-fdiagnostics-all-candidates" }
> +
> +void f(int) = delete; // { dg-message "declared here" }
> +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" }
> +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
> +}
> diff --git a/gcc/testsuite/g++.dg/cpp0x/deleted16a.C b/gcc/testsuite/g++.dg/cpp0x/deleted16a.C
> new file mode 100644
> index 00000000000..e62306fa3d1
> --- /dev/null
> +++ b/gcc/testsuite/g++.dg/cpp0x/deleted16a.C
> @@ -0,0 +1,12 @@
> +// Verify we suggest -fdiagnostics-all-candidates when diagnosing
> +// overload resolution selecting a deleted function.
> +// { dg-do compile { target c++11 } }
> +#include "deleted16.C"
> +
> +// { dg-error "deleted" "" { target *-*-* } 21 }
> +// { dg-error "deleted" "" { target *-*-* } 22 }
> +// { dg-error "deleted" "" { target *-*-* } 23 }
> +
> +// { dg-message "use '-fdiagnostics-all-candidates'" "" { target *-*-* } 21 }
> +// { dg-message "use '-fdiagnostics-all-candidates'" "" { target *-*-* } 22 }
> +// { dg-message "use '-fdiagnostics-all-candidates'" "" { target *-*-* } 23 }
> diff --git a/gcc/testsuite/g++.dg/overload/error6.C b/gcc/testsuite/g++.dg/overload/error6.C
> index 86a12eaa8de..3186a297bfc 100644
> --- a/gcc/testsuite/g++.dg/overload/error6.C
> +++ b/gcc/testsuite/g++.dg/overload/error6.C
> @@ -1,5 +1,6 @@
>  // Verify we note even non-template candidates when diagnosing
>  // overload resolution failure for a template-id.
> +// { dg-additional-options "-fdiagnostics-all-candidates" }
>  
>  template<class T> void f(T); // { dg-message "candidate" }
>  void f(int); // { dg-message {candidate: 'void f\(int\)' \(ignored\)} }
> diff --git a/gcc/testsuite/g++.dg/overload/error6a.C b/gcc/testsuite/g++.dg/overload/error6a.C
> new file mode 100644
> index 00000000000..e86ab5158ab
> --- /dev/null
> +++ b/gcc/testsuite/g++.dg/overload/error6a.C
> @@ -0,0 +1,6 @@
> +// Verify we suggest -fdiagnostics-all-candidates when there are
> +// omitted candidates.
> +#include "error6.C"
> +
> +// { dg-error "no match" "" { target *-*-* } 9 }
> +// { dg-message "use '-fdiagnostics-all-candidates'" "" { target *-*-* } 9 }
> -- 
> 2.42.0.482.g2e8e77cbac
>
Jason Merrill Dec. 10, 2023, 7:32 p.m. UTC | #5
On 11/30/23 10:46, Patrick Palka wrote:
> On Fri, 27 Oct 2023, Patrick Palka wrote:
>> On Fri, 27 Oct 2023, Jason Merrill wrote:
>>> On 10/27/23 15:55, 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
>>>>
>>>> These notes are controlled by a new command line flag -fnote-all-cands,
>>>> which also controls whether we note ignored candidates more generally.
>>>>
>>>> gcc/ChangeLog:
>>>>
>>>> 	* doc/invoke.texi (C++ Dialect Options): Document -fnote-all-cands.
>>>>
>>>> gcc/c-family/ChangeLog:
>>>>
>>>> 	* c.opt: Add -fnote-all-cands.
>>>>
>>>> gcc/cp/ChangeLog:
>>>>
>>>> 	* call.cc (print_z_candidates): Only print ignored candidates
>>>> 	when -fnote-all-cands is set.
>>>> 	(build_over_call): When diagnosing deletedness, call
>>>> 	print_z_candidates if -fnote-all-cands is set.
>>>
>>> My suggestion was also to suggest using the flag in cases where it would make
>>> a difference, e.g.
>>>
>>> note: some candidates omitted, use '-fnote-all-cands' to display them
>>
>> Ah thanks, fixed.  That'll help a lot with discoverability of the flag.
>>
>>>
>>> Maybe "-fdiagnostics-all-candidates"?
>>
>> Nice, that's a better name indeed :)
>>
>> How does the following look?  Full bootstrap/regtest in progress.
>>
>> Here's the output of e.g. deleted16a.C.  I think I'd prefer to not print
>> the source line when emitting the suggestion, but I don't know how to do
>> that properly (aside from e.g. emitting the note at UNKNOWN_LOCATION).
>>
>> In file included from gcc/testsuite/g++.dg/cpp0x/deleted16a.C:4:
>> gcc/testsuite/g++.dg/cpp0x/deleted16.C: In function ‘int main()’:
>> gcc/testsuite/g++.dg/cpp0x/deleted16.C:21:4: error: use of deleted function ‘void f(int)’
>>     21 |   f(0); // { dg-error "deleted" }
>>        |   ~^~~
>> gcc/testsuite/g++.dg/cpp0x/deleted16.C:6:6: note: declared here
>>      6 | void f(int) = delete; // { dg-message "declared here" }
>>        |      ^
>> gcc/testsuite/g++.dg/cpp0x/deleted16.C:21:4: note: use ‘-fdiagnostics-all-candidates’ to display considered candidates
>>     21 |   f(0); // { dg-error "deleted" }
>>        |   ~^~~
>> gcc/testsuite/g++.dg/cpp0x/deleted16.C:22:4: error: use of deleted function ‘void g(int)’
>>     22 |   g(0); // { dg-error "deleted" }
>>        |   ~^~~
>> gcc/testsuite/g++.dg/cpp0x/deleted16.C:12:6: note: declared here
>>     12 | void g(int) = delete; // { dg-message "declared here" }
>>        |      ^
>> gcc/testsuite/g++.dg/cpp0x/deleted16.C:22:4: note: use ‘-fdiagnostics-all-candidates’ to display considered candidates
>>     22 |   g(0); // { dg-error "deleted" }
>>        |   ~^~~
>> gcc/testsuite/g++.dg/cpp0x/deleted16.C:23:4: error: use of deleted function ‘void h(T, T) [with T = int]’
>>     23 |   h(1, 1); // { dg-error "deleted" }
>>        |   ~^~~~~~
>> gcc/testsuite/g++.dg/cpp0x/deleted16.C:17:24: note: declared here
>>     17 | template<class T> void h(T, T) = delete; // { dg-message "declared here|candidate" }
>>        |                        ^
>> gcc/testsuite/g++.dg/cpp0x/deleted16.C:23:4: note: use ‘-fdiagnostics-all-candidates’ to display considered candidates
>>     23 |   h(1, 1); // { dg-error "deleted" }
>>        |   ~^~~~~~
>>
>> -- >8 --
>>
>>
>> Subject: [PATCH 3/3] c++: note other candidates when diagnosing deletedness
>>
>> 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
>>
>> These notes are controlled by a new command line flag
>> -fdiagnostics-all-candidates which also controls whether we note
>> ignored candidates more generally.
>>
>> gcc/ChangeLog:
>>
>> 	* doc/invoke.texi (C++ Dialect Options): Document
>> 	-fdiagnostics-all-candidates.
>>
>> gcc/c-family/ChangeLog:
>>
>> 	* c.opt: Add -fdiagnostics-all-candidates.
>>
>> gcc/cp/ChangeLog:
>>
>> 	* call.cc (print_z_candidates): Only print ignored candidates
>> 	when -fdiagnostics-all-candidates is set, otherwise suggest
>> 	the flag.
>> 	(build_over_call): When diagnosing deletedness, note
>> 	other candidates only if -fdiagnostics-all-candidates is
>> 	set, otherwise suggest the flag.
>>
>> gcc/testsuite/ChangeLog:
>>
>> 	* g++.dg/overload/error6.C: Pass -fdiagnostics-all-candidates.
>> 	* g++.dg/cpp0x/deleted16.C: New test.
>> 	* g++.dg/cpp0x/deleted16a.C: New test.
>> 	* g++.dg/overload/error6a.C: New test.
> 
> Ping.

OK.

Jason
diff mbox series

Patch

diff --git a/gcc/c-family/c.opt b/gcc/c-family/c.opt
index 44b9c862c14..a76f73cc661 100644
--- a/gcc/c-family/c.opt
+++ b/gcc/c-family/c.opt
@@ -2006,6 +2006,10 @@  fnil-receivers
 ObjC ObjC++ Var(flag_nil_receivers) Init(1)
 Assume that receivers of Objective-C messages may be nil.
 
+fnote-all-cands
+C++ ObjC++ Var(flag_note_all_cands)
+Note all candidates during overload resolution failure.
+
 flocal-ivars
 ObjC ObjC++ Var(flag_local_ivars) Init(1)
 Allow access to instance variables as if they were local declarations within instance method implementations.
diff --git a/gcc/cp/call.cc b/gcc/cp/call.cc
index 81cc029dddb..7ace0e65096 100644
--- a/gcc/cp/call.cc
+++ b/gcc/cp/call.cc
@@ -4090,6 +4090,8 @@  print_z_candidates (location_t loc, struct z_candidate *candidates,
     {
       if (only_viable_p.is_true () && candidates->viable != 1)
 	break;
+      if (ignored_candidate_p (candidates) && !flag_note_all_cands)
+	break;
       print_z_candidate (loc, N_("candidate:"), candidates);
     }
 }
@@ -9933,7 +9935,11 @@  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);
+	  if (cand->next && flag_note_all_cands)
+	    print_z_candidates (input_location, cand, /*only_viable_p=*/false);
+	}
       return error_mark_node;
     }
 
diff --git a/gcc/doc/invoke.texi b/gcc/doc/invoke.texi
index 5a9284d635c..ac82299416c 100644
--- a/gcc/doc/invoke.texi
+++ b/gcc/doc/invoke.texi
@@ -3479,6 +3479,11 @@  Disable built-in declarations of functions that are not mandated by
 ANSI/ISO C@.  These include @code{ffs}, @code{alloca}, @code{_exit},
 @code{index}, @code{bzero}, @code{conjf}, and other related functions.
 
+@opindex fnote-all-cands
+@item -fnote-all-cands
+Permit the C++ front end to note all candidates during overload resolution
+failure, including when a deleted function is selected.
+
 @opindex fnothrow-opt
 @item -fnothrow-opt
 Treat a @code{throw()} exception specification as if it were a
diff --git a/gcc/testsuite/g++.dg/cpp0x/deleted16.C b/gcc/testsuite/g++.dg/cpp0x/deleted16.C
new file mode 100644
index 00000000000..506caae76b6
--- /dev/null
+++ b/gcc/testsuite/g++.dg/cpp0x/deleted16.C
@@ -0,0 +1,25 @@ 
+// Verify -fnote-all-cands causes us to note other candidates when a deleted
+// function is selected by overload resolution.
+// { dg-do compile { target c++11 } }
+// { dg-additional-options "-fnote-all-cands" }
+
+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
+}
diff --git a/gcc/testsuite/g++.dg/overload/error6.C b/gcc/testsuite/g++.dg/overload/error6.C
index 86a12eaa8de..ac513824887 100644
--- a/gcc/testsuite/g++.dg/overload/error6.C
+++ b/gcc/testsuite/g++.dg/overload/error6.C
@@ -1,5 +1,6 @@ 
 // Verify we note even non-template candidates when diagnosing
 // overload resolution failure for a template-id.
+// { dg-additional-options "-fnote-all-cands" }
 
 template<class T> void f(T); // { dg-message "candidate" }
 void f(int); // { dg-message {candidate: 'void f\(int\)' \(ignored\)} }