Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

diagnostics: only show one suggestion for method -> assoc fn #104580

Merged
merged 1 commit into from
Nov 19, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 22 additions & 19 deletions compiler/rustc_hir_typeck/src/method/suggest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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,
);
}
}
}
}
Expand Down Expand Up @@ -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![];
Expand Down Expand Up @@ -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();
}

Expand Down
13 changes: 4 additions & 9 deletions src/test/ui/suggestions/issue-102354.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,17 @@ 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`
--> $DIR/issue-102354.rs:2:5
|
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

Expand Down