Skip to content

Commit f541ad9

Browse files
authored
Rollup merge of #103550 - notriddle:notriddle/no-suggest-static-candidates, r=wesleywiser
diagnostics: do not suggest static candidates as traits to import If it's a static candidate, then it's already implemented. Do not suggest it a second time for implementing. Partial fix for #102354
2 parents 2f02cf8 + 8cf9ad6 commit f541ad9

File tree

3 files changed

+52
-6
lines changed

3 files changed

+52
-6
lines changed

compiler/rustc_hir_typeck/src/method/suggest.rs

+18-6
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
106106

107107
let report_candidates = |span: Span,
108108
err: &mut Diagnostic,
109-
mut sources: Vec<CandidateSource>,
109+
sources: &mut Vec<CandidateSource>,
110110
sugg_span: Span| {
111111
sources.sort();
112112
sources.dedup();
@@ -248,7 +248,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
248248

249249
match error {
250250
MethodError::NoMatch(NoMatchData {
251-
static_candidates: static_sources,
251+
static_candidates: mut static_sources,
252252
unsatisfied_predicates,
253253
out_of_scope_traits,
254254
lev_candidate,
@@ -422,9 +422,9 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
422422
err.help(&format!("try with `{}::{}`", ty_str, item_name,));
423423
}
424424

425-
report_candidates(span, &mut err, static_sources, sugg_span);
425+
report_candidates(span, &mut err, &mut static_sources, sugg_span);
426426
} else if static_sources.len() > 1 {
427-
report_candidates(span, &mut err, static_sources, sugg_span);
427+
report_candidates(span, &mut err, &mut static_sources, sugg_span);
428428
}
429429

430430
let mut bound_spans = vec![];
@@ -1007,6 +1007,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
10071007
source,
10081008
out_of_scope_traits,
10091009
&unsatisfied_predicates,
1010+
&static_sources,
10101011
unsatisfied_bounds,
10111012
);
10121013
}
@@ -1079,7 +1080,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
10791080
return Some(err);
10801081
}
10811082

1082-
MethodError::Ambiguity(sources) => {
1083+
MethodError::Ambiguity(mut sources) => {
10831084
let mut err = struct_span_err!(
10841085
self.sess(),
10851086
item_name.span,
@@ -1088,7 +1089,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
10881089
);
10891090
err.span_label(item_name.span, format!("multiple `{}` found", item_name));
10901091

1091-
report_candidates(span, &mut err, sources, sugg_span);
1092+
report_candidates(span, &mut err, &mut sources, sugg_span);
10921093
err.emit();
10931094
}
10941095

@@ -2015,6 +2016,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
20152016
Option<ty::Predicate<'tcx>>,
20162017
Option<ObligationCause<'tcx>>,
20172018
)],
2019+
static_candidates: &[CandidateSource],
20182020
unsatisfied_bounds: bool,
20192021
) {
20202022
let mut alt_rcvr_sugg = false;
@@ -2128,6 +2130,16 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
21282130
Some(attr) => attr.level.is_stable(),
21292131
None => true,
21302132
})
2133+
.filter(|info| {
2134+
// Static candidates are already implemented, and known not to work
2135+
// Do not suggest them again
2136+
static_candidates.iter().all(|sc| match *sc {
2137+
CandidateSource::Trait(def_id) => def_id != info.def_id,
2138+
CandidateSource::Impl(def_id) => {
2139+
self.tcx.trait_id_of_impl(def_id) != Some(info.def_id)
2140+
}
2141+
})
2142+
})
21312143
.filter(|info| {
21322144
// We approximate the coherence rules to only suggest
21332145
// traits that are legal to implement by requiring that
+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
trait Trait {
2+
fn func() {}
3+
}
4+
5+
impl Trait for i32 {}
6+
7+
fn main() {
8+
let x: i32 = 123;
9+
x.func(); //~ERROR no method
10+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
error[E0599]: no method named `func` found for type `i32` in the current scope
2+
--> $DIR/issue-102354.rs:9:7
3+
|
4+
LL | x.func();
5+
| ^^^^ this is an associated function, not a method
6+
|
7+
= note: found the following associated functions; to be used as methods, functions must have a `self` parameter
8+
note: the candidate is defined in the trait `Trait`
9+
--> $DIR/issue-102354.rs:2:5
10+
|
11+
LL | fn func() {}
12+
| ^^^^^^^^^
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+
| ~~~~~~~~~~~~~~~~~~~~~~~
21+
22+
error: aborting due to previous error
23+
24+
For more information about this error, try `rustc --explain E0599`.

0 commit comments

Comments
 (0)