Skip to content

Commit 3cf3a65

Browse files
authored
Rollup merge of #104580 - notriddle:notriddle/issue-102354-hide-sugg, r=compiler-errors
diagnostics: only show one suggestion for method -> assoc fn Fixes #102354
2 parents 686c170 + df7ecbc commit 3cf3a65

File tree

2 files changed

+26
-28
lines changed

2 files changed

+26
-28
lines changed

compiler/rustc_hir_typeck/src/method/suggest.rs

+22-19
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
114114
let report_candidates = |span: Span,
115115
err: &mut Diagnostic,
116116
sources: &mut Vec<CandidateSource>,
117-
sugg_span: Span| {
117+
sugg_span: Option<Span>| {
118118
sources.sort();
119119
sources.dedup();
120120
// Dynamic limit to avoid hiding just one candidate, which is silly.
@@ -175,7 +175,8 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
175175
} else {
176176
err.note(&note_str);
177177
}
178-
if let Some(trait_ref) = self.tcx.impl_trait_ref(impl_did) {
178+
if let Some(sugg_span) = sugg_span
179+
&& let Some(trait_ref) = self.tcx.impl_trait_ref(impl_did) {
179180
let path = self.tcx.def_path_str(trait_ref.def_id);
180181

181182
let ty = match item.kind {
@@ -224,20 +225,22 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
224225
err.span_note(item_span, msg);
225226
None
226227
};
227-
let path = self.tcx.def_path_str(trait_did);
228-
print_disambiguation_help(
229-
item_name,
230-
args,
231-
err,
232-
path,
233-
rcvr_ty,
234-
item.kind,
235-
item.def_id,
236-
sugg_span,
237-
idx,
238-
self.tcx.sess.source_map(),
239-
item.fn_has_self_parameter,
240-
);
228+
if let Some(sugg_span) = sugg_span {
229+
let path = self.tcx.def_path_str(trait_did);
230+
print_disambiguation_help(
231+
item_name,
232+
args,
233+
err,
234+
path,
235+
rcvr_ty,
236+
item.kind,
237+
item.def_id,
238+
sugg_span,
239+
idx,
240+
self.tcx.sess.source_map(),
241+
item.fn_has_self_parameter,
242+
);
243+
}
241244
}
242245
}
243246
}
@@ -407,9 +410,9 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
407410
sugg_span,
408411
);
409412

410-
report_candidates(span, &mut err, &mut static_candidates, sugg_span);
413+
report_candidates(span, &mut err, &mut static_candidates, None);
411414
} else if static_candidates.len() > 1 {
412-
report_candidates(span, &mut err, &mut static_candidates, sugg_span);
415+
report_candidates(span, &mut err, &mut static_candidates, Some(sugg_span));
413416
}
414417

415418
let mut bound_spans = vec![];
@@ -1015,7 +1018,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
10151018
);
10161019
err.span_label(item_name.span, format!("multiple `{}` found", item_name));
10171020

1018-
report_candidates(span, &mut err, &mut sources, sugg_span);
1021+
report_candidates(span, &mut err, &mut sources, Some(sugg_span));
10191022
err.emit();
10201023
}
10211024

src/test/ui/suggestions/issue-102354.stderr

+4-9
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,17 @@ error[E0599]: no method named `func` found for type `i32` in the current scope
22
--> $DIR/issue-102354.rs:9:7
33
|
44
LL | x.func();
5-
| ^^^^ this is an associated function, not a method
5+
| --^^^^--
6+
| | |
7+
| | this is an associated function, not a method
8+
| help: use associated function syntax instead: `i32::func()`
69
|
710
= note: found the following associated functions; to be used as methods, functions must have a `self` parameter
811
note: the candidate is defined in the trait `Trait`
912
--> $DIR/issue-102354.rs:2:5
1013
|
1114
LL | fn func() {}
1215
| ^^^^^^^^^
13-
help: use associated function syntax instead
14-
|
15-
LL | i32::func();
16-
| ~~~~~~~~~~~
17-
help: disambiguate the associated function for the candidate
18-
|
19-
LL | <i32 as Trait>::func(x);
20-
| ~~~~~~~~~~~~~~~~~~~~~~~
2116

2217
error: aborting due to previous error
2318

0 commit comments

Comments
 (0)