Skip to content

Commit 4f8e2e3

Browse files
committed
Auto merge of #96940 - TaKO8Ki:stop-suggesting-wrong-fully-qualified-path, r=estebank
Stop suggesting non-existing fully qualified paths This patch fixes a part of #96295. r? `@estebank`
2 parents 3a08bd7 + daeec7e commit 4f8e2e3

File tree

3 files changed

+75
-11
lines changed

3 files changed

+75
-11
lines changed

compiler/rustc_infer/src/infer/error_reporting/need_type_info.rs

+16-11
Original file line numberDiff line numberDiff line change
@@ -739,7 +739,7 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
739739
{
740740
let mut eraser = TypeParamEraser(self.tcx);
741741
let candidate_len = impl_candidates.len();
742-
let mut suggestions: Vec<_> = impl_candidates.iter().map(|candidate| {
742+
let mut suggestions: Vec<_> = impl_candidates.iter().filter_map(|candidate| {
743743
let trait_item = self.tcx
744744
.associated_items(candidate.def_id)
745745
.find_by_name_and_kind(
@@ -748,6 +748,9 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
748748
ty::AssocKind::Fn,
749749
candidate.def_id
750750
);
751+
if trait_item.is_none() {
752+
return None;
753+
}
751754
let prefix = if let Some(trait_item) = trait_item
752755
&& let Some(trait_m) = trait_item.def_id.as_local()
753756
&& let hir::TraitItemKind::Fn(fn_, _) = &self.tcx.hir().trait_item(hir::TraitItemId { def_id: trait_m }).kind
@@ -761,24 +764,26 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
761764
""
762765
};
763766
let candidate = candidate.super_fold_with(&mut eraser);
764-
vec![
767+
Some(vec![
765768
(expr.span.shrink_to_lo(), format!("{}::{}({}", candidate, segment.ident, prefix)),
766769
if exprs.len() == 1 {
767770
(expr.span.shrink_to_hi().with_hi(e.span.hi()), ")".to_string())
768771
} else {
769772
(expr.span.shrink_to_hi().with_hi(exprs[1].span.lo()), ", ".to_string())
770773
},
771-
]
774+
])
772775
}).collect();
773776
suggestions.sort_by(|a, b| a[0].1.cmp(&b[0].1));
774-
err.multipart_suggestions(
775-
&format!(
776-
"use the fully qualified path for the potential candidate{}",
777-
pluralize!(candidate_len),
778-
),
779-
suggestions.into_iter(),
780-
Applicability::MaybeIncorrect,
781-
);
777+
if !suggestions.is_empty() {
778+
err.multipart_suggestions(
779+
&format!(
780+
"use the fully qualified path for the potential candidate{}",
781+
pluralize!(candidate_len),
782+
),
783+
suggestions.into_iter(),
784+
Applicability::MaybeIncorrect,
785+
);
786+
}
782787
}
783788
// Suggest specifying type params or point out the return type of the call:
784789
//
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
struct A<T>(T);
2+
struct B;
3+
4+
trait I<T> {}
5+
impl I<i32> for B {}
6+
impl I<u32> for B {}
7+
8+
trait V<U> {
9+
fn method(self) -> U;
10+
}
11+
12+
impl<T, U> V<U> for A<T>
13+
where
14+
T: I<U>,
15+
{
16+
fn method(self) -> U { unimplemented!() }
17+
}
18+
19+
fn main() {
20+
let a = A(B);
21+
a.method();
22+
//~^ ERROR type annotations needed
23+
//~| ERROR type annotations needed
24+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
error[E0282]: type annotations needed
2+
--> $DIR/not-suggest-non-existing-fully-qualified-path.rs:21:7
3+
|
4+
LL | a.method();
5+
| --^^^^^^--
6+
| | |
7+
| | cannot infer type for type parameter `U` declared on the trait `V`
8+
| this method call resolves to `U`
9+
10+
error[E0283]: type annotations needed
11+
--> $DIR/not-suggest-non-existing-fully-qualified-path.rs:21:7
12+
|
13+
LL | a.method();
14+
| --^^^^^^--
15+
| | |
16+
| | cannot infer type for type parameter `U`
17+
| this method call resolves to `U`
18+
|
19+
note: multiple `impl`s satisfying `B: I<_>` found
20+
--> $DIR/not-suggest-non-existing-fully-qualified-path.rs:5:1
21+
|
22+
LL | impl I<i32> for B {}
23+
| ^^^^^^^^^^^^^^^^^
24+
LL | impl I<u32> for B {}
25+
| ^^^^^^^^^^^^^^^^^
26+
note: required because of the requirements on the impl of `V<_>` for `A<B>`
27+
--> $DIR/not-suggest-non-existing-fully-qualified-path.rs:12:12
28+
|
29+
LL | impl<T, U> V<U> for A<T>
30+
| ^^^^ ^^^^
31+
32+
error: aborting due to 2 previous errors
33+
34+
Some errors have detailed explanations: E0282, E0283.
35+
For more information about an error, try `rustc --explain E0282`.

0 commit comments

Comments
 (0)