about summary refs log tree commit diff
diff options
context:
space:
mode:
authorDylan DPC <99973273+Dylan-DPC@users.noreply.github.com>2022-11-19 11:54:47 +0530
committerGitHub <noreply@github.com>2022-11-19 11:54:47 +0530
commit3cf3a65a719e29bc5aac8a3a3be8a21f3162acf2 (patch)
treeb3c1cb60370e9195576083b367a68464deebf57d
parent686c170751abe59ee81eb1f0f913a572f10c9a9c (diff)
parentdf7ecbcb1116915cb4557dccc5a4839a13fbf5b0 (diff)
downloadrust-3cf3a65a719e29bc5aac8a3a3be8a21f3162acf2.tar.gz
rust-3cf3a65a719e29bc5aac8a3a3be8a21f3162acf2.zip
Rollup merge of #104580 - notriddle:notriddle/issue-102354-hide-sugg, r=compiler-errors
diagnostics: only show one suggestion for method -> assoc fn

Fixes #102354
-rw-r--r--compiler/rustc_hir_typeck/src/method/suggest.rs41
-rw-r--r--src/test/ui/suggestions/issue-102354.stderr13
2 files changed, 26 insertions, 28 deletions
diff --git a/compiler/rustc_hir_typeck/src/method/suggest.rs b/compiler/rustc_hir_typeck/src/method/suggest.rs
index e2c5edd0e88..4ac4914bd44 100644
--- a/compiler/rustc_hir_typeck/src/method/suggest.rs
+++ b/compiler/rustc_hir_typeck/src/method/suggest.rs
@@ -114,7 +114,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
         let report_candidates = |span: Span,
                                  err: &mut Diagnostic,
                                  sources: &mut Vec<CandidateSource>,
-                                 sugg_span: Span| {
+                                 sugg_span: Option<Span>| {
             sources.sort();
             sources.dedup();
             // Dynamic limit to avoid hiding just one candidate, which is silly.
@@ -175,7 +175,8 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
                         } else {
                             err.note(&note_str);
                         }
-                        if let Some(trait_ref) = self.tcx.impl_trait_ref(impl_did) {
+                        if let Some(sugg_span) = sugg_span
+                            && let Some(trait_ref) = self.tcx.impl_trait_ref(impl_did) {
                             let path = self.tcx.def_path_str(trait_ref.def_id);
 
                             let ty = match item.kind {
@@ -224,20 +225,22 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
                             err.span_note(item_span, msg);
                             None
                         };
-                        let path = self.tcx.def_path_str(trait_did);
-                        print_disambiguation_help(
-                            item_name,
-                            args,
-                            err,
-                            path,
-                            rcvr_ty,
-                            item.kind,
-                            item.def_id,
-                            sugg_span,
-                            idx,
-                            self.tcx.sess.source_map(),
-                            item.fn_has_self_parameter,
-                        );
+                        if let Some(sugg_span) = sugg_span {
+                            let path = self.tcx.def_path_str(trait_did);
+                            print_disambiguation_help(
+                                item_name,
+                                args,
+                                err,
+                                path,
+                                rcvr_ty,
+                                item.kind,
+                                item.def_id,
+                                sugg_span,
+                                idx,
+                                self.tcx.sess.source_map(),
+                                item.fn_has_self_parameter,
+                            );
+                        }
                     }
                 }
             }
@@ -407,9 +410,9 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
                         sugg_span,
                     );
 
-                    report_candidates(span, &mut err, &mut static_candidates, sugg_span);
+                    report_candidates(span, &mut err, &mut static_candidates, None);
                 } else if static_candidates.len() > 1 {
-                    report_candidates(span, &mut err, &mut static_candidates, sugg_span);
+                    report_candidates(span, &mut err, &mut static_candidates, Some(sugg_span));
                 }
 
                 let mut bound_spans = vec![];
@@ -1015,7 +1018,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
                 );
                 err.span_label(item_name.span, format!("multiple `{}` found", item_name));
 
-                report_candidates(span, &mut err, &mut sources, sugg_span);
+                report_candidates(span, &mut err, &mut sources, Some(sugg_span));
                 err.emit();
             }
 
diff --git a/src/test/ui/suggestions/issue-102354.stderr b/src/test/ui/suggestions/issue-102354.stderr
index b17c4dc5dfb..08d4b995590 100644
--- a/src/test/ui/suggestions/issue-102354.stderr
+++ b/src/test/ui/suggestions/issue-102354.stderr
@@ -2,7 +2,10 @@ error[E0599]: no method named `func` found for type `i32` in the current scope
   --> $DIR/issue-102354.rs:9:7
    |
 LL |     x.func();
-   |       ^^^^ this is an associated function, not a method
+   |     --^^^^--
+   |     | |
+   |     | this is an associated function, not a method
+   |     help: use associated function syntax instead: `i32::func()`
    |
    = note: found the following associated functions; to be used as methods, functions must have a `self` parameter
 note: the candidate is defined in the trait `Trait`
@@ -10,14 +13,6 @@ note: the candidate is defined in the trait `Trait`
    |
 LL |     fn func() {}
    |     ^^^^^^^^^
-help: use associated function syntax instead
-   |
-LL |     i32::func();
-   |     ~~~~~~~~~~~
-help: disambiguate the associated function for the candidate
-   |
-LL |     <i32 as Trait>::func(x);
-   |     ~~~~~~~~~~~~~~~~~~~~~~~
 
 error: aborting due to previous error