]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
c++: note other candidates when diagnosing deletedness
authorPatrick Palka <ppalka@redhat.com>
Wed, 13 Dec 2023 21:46:04 +0000 (16:46 -0500)
committerPatrick Palka <ppalka@redhat.com>
Wed, 13 Dec 2023 21:46:04 +0000 (16:46 -0500)
With the previous two patches in place, we can now extend our
deletedness diagnostic to note the other considered candidates, e.g.:

  deleted.C: In function 'int main()':
  deleted.C:10:4: error: use of deleted function 'void f(int)'
     10 |   f(0);
        |   ~^~~
  deleted.C:5:6: note: declared here
      5 | void f(int) = delete;
        |      ^
  deleted.C:5:6: note: candidate: 'void f(int)' (deleted)
  deleted.C:6:6: note: candidate: 'void f(...)'
      6 | void f(...);
        |      ^
  deleted.C:7:6: note: candidate: 'void f(int, int)'
      7 | void f(int, int);
        |      ^
  deleted.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
gcc/cp/call.cc
gcc/doc/invoke.texi
gcc/testsuite/g++.dg/cpp0x/deleted16.C [new file with mode: 0644]
gcc/testsuite/g++.dg/cpp0x/deleted16a.C [new file with mode: 0644]
gcc/testsuite/g++.dg/overload/error6.C
gcc/testsuite/g++.dg/overload/error6a.C [new file with mode: 0644]

index 3706505f8bf8230fc4fb735b9aafa18bdc6dad1b..03b64d536fa3d4e797313c0b6f67e26f1814bfe4 100644 (file)
@@ -1805,6 +1805,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.
index aa4111dda5c7e868066e5f1039fe7e855dfc6b01..6ac87a298b29f7439f2906a1ffc0827e52a67636 100644 (file)
@@ -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);
     }
 }
@@ -9967,7 +9973,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;
     }
 
index 8f885b8c6d69b512bd3be676bdc6bc737c7dc88f..f89f926a572bff628b10af4c7d174f7e87528e05 100644 (file)
@@ -3328,6 +3328,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 (file)
index 0000000..d434794
--- /dev/null
@@ -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 (file)
index 0000000..e62306f
--- /dev/null
@@ -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 }
index 86a12eaa8de0e8f36d49fbcfdc11ccbe41ddad4d..3186a297bfc9fd008517232816d9d990ecbdbe2b 100644 (file)
@@ -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 (file)
index 0000000..e86ab51
--- /dev/null
@@ -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 }