E0220: only suggests associated types if there's only one candidate

This commit is contained in:
Lieselotte 2023-09-08 08:44:23 +02:00
parent 96c96645c7
commit a0e0a3261e
No known key found for this signature in database
GPG key ID: 43A6A32F83A6F9B1
7 changed files with 27 additions and 49 deletions

View file

@ -201,34 +201,28 @@ pub(crate) fn complain_about_assoc_type_not_found<I>(
}
}
// If we still couldn't find any associated type, just list them all.
// If we still couldn't find any associated type, and only one associated type exists,
// suggests using it.
if all_candidate_names.is_empty() {
err.help(format!(
"`{ty_param_name}` has no associated type, try removing `{assoc_name}`"
));
return err.emit();
if all_candidate_names.len() == 1 {
// this should still compile, except on `#![feature(associated_type_defaults)]`
// where it could suggests `type A = Self::A`, thus recursing infinitely
let applicability = if self.tcx().features().associated_type_defaults {
Applicability::Unspecified
} else {
Applicability::MaybeIncorrect
};
err.span_suggestion(
assoc_name.span,
format!("`{ty_param_name}` has the following associated type"),
all_candidate_names.first().unwrap().to_string(),
applicability,
);
} else {
err.span_label(assoc_name.span, format!("associated type `{assoc_name}` not found"));
}
let msg = if all_candidate_names.len() > 1 {
format!("`{ty_param_name}` has the following associated types")
} else {
format!("`{ty_param_name}` has the following associated type")
};
let applicability = if self.tcx().features().associated_type_defaults {
Applicability::Unspecified // `type A = Self::B` would suggest `type A = Self::A`
} else {
Applicability::MaybeIncorrect
};
err.span_suggestions(
assoc_name.span,
msg,
all_candidate_names.iter().map(|symbol| symbol.to_string()),
applicability,
);
err.emit()
}

View file

@ -2,25 +2,19 @@ error[E0220]: associated type `Z` not found for `Foo`
--> $DIR/assoc-const-eq-missing.rs:15:16
|
LL | fn foo1<F: Foo<Z=3>>() {}
| ^
|
= help: `Foo` has no associated type, try removing `Z`
| ^ associated type `Z` not found
error[E0220]: associated type `Z` not found for `Foo`
--> $DIR/assoc-const-eq-missing.rs:17:16
|
LL | fn foo2<F: Foo<Z=usize>>() {}
| ^
|
= help: `Foo` has no associated type, try removing `Z`
| ^ associated type `Z` not found
error[E0220]: associated type `Z` not found for `Foo`
--> $DIR/assoc-const-eq-missing.rs:19:16
|
LL | fn foo3<F: Foo<Z=5>>() {}
| ^
|
= help: `Foo` has no associated type, try removing `Z`
| ^ associated type `Z` not found
error: aborting due to 3 previous errors

View file

@ -8,9 +8,7 @@ error[E0220]: associated type `Item` not found for `M`
--> $DIR/missing-trait-bound-for-assoc-fails.rs:4:8
|
LL | M::Item: Temp,
| ^^^^
|
= help: `M` has no associated type, try removing `Item`
| ^^^^ associated type `Item` not found
error: aborting due to 2 previous errors

View file

@ -2,9 +2,7 @@ error[E0220]: associated type `A` not found for `T`
--> $DIR/associated-types-path-1.rs:10:26
|
LL | pub fn f1<T>(a: T, x: T::A) {}
| ^
|
= help: `T` has no associated type, try removing `A`
| ^ associated type `A` not found
error[E0221]: ambiguous associated type `A` in bounds of `T`
--> $DIR/associated-types-path-1.rs:11:34

View file

@ -19,9 +19,7 @@ error[E0220]: associated type `m` not found for `Trait`
--> $DIR/feature-gate-return_type_notation.rs:14:17
|
LL | fn foo<T: Trait<m(): Send>>() {}
| ^
|
= help: `Trait` has no associated type, try removing `m`
| ^ associated type `m` not found
error: aborting due to 3 previous errors

View file

@ -19,9 +19,7 @@ error[E0220]: associated type `m` not found for `Trait`
--> $DIR/feature-gate-return_type_notation.rs:17:17
|
LL | fn foo<T: Trait<m(): Send>>() {}
| ^
|
= help: `Trait` has no associated type, try removing `m`
| ^ associated type `m` not found
error: aborting due to 3 previous errors

View file

@ -19,9 +19,7 @@ error[E0220]: associated type `m` not found for `Trait`
--> $DIR/feature-gate-return_type_notation.rs:17:17
|
LL | fn foo<T: Trait<m(): Send>>() {}
| ^
|
= help: `Trait` has no associated type, try removing `m`
| ^ associated type `m` not found
error: aborting due to 3 previous errors