1
Fork 0

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 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
} }
} }
// 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() { if all_candidate_names.len() == 1 {
err.help(format!( // this should still compile, except on `#![feature(associated_type_defaults)]`
"`{ty_param_name}` has no associated type, try removing `{assoc_name}`" // where it could suggests `type A = Self::A`, thus recursing infinitely
)); let applicability = if self.tcx().features().associated_type_defaults {
return err.emit(); 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() 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 --> $DIR/assoc-const-eq-missing.rs:15:16
| |
LL | fn foo1<F: Foo<Z=3>>() {} LL | fn foo1<F: Foo<Z=3>>() {}
| ^ | ^ associated type `Z` not found
|
= help: `Foo` has no associated type, try removing `Z`
error[E0220]: associated type `Z` not found for `Foo` error[E0220]: associated type `Z` not found for `Foo`
--> $DIR/assoc-const-eq-missing.rs:17:16 --> $DIR/assoc-const-eq-missing.rs:17:16
| |
LL | fn foo2<F: Foo<Z=usize>>() {} LL | fn foo2<F: Foo<Z=usize>>() {}
| ^ | ^ associated type `Z` not found
|
= help: `Foo` has no associated type, try removing `Z`
error[E0220]: associated type `Z` not found for `Foo` error[E0220]: associated type `Z` not found for `Foo`
--> $DIR/assoc-const-eq-missing.rs:19:16 --> $DIR/assoc-const-eq-missing.rs:19:16
| |
LL | fn foo3<F: Foo<Z=5>>() {} LL | fn foo3<F: Foo<Z=5>>() {}
| ^ | ^ associated type `Z` not found
|
= help: `Foo` has no associated type, try removing `Z`
error: aborting due to 3 previous errors 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 --> $DIR/missing-trait-bound-for-assoc-fails.rs:4:8
| |
LL | M::Item: Temp, LL | M::Item: Temp,
| ^^^^ | ^^^^ associated type `Item` not found
|
= help: `M` has no associated type, try removing `Item`
error: aborting due to 2 previous errors 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 --> $DIR/associated-types-path-1.rs:10:26
| |
LL | pub fn f1<T>(a: T, x: T::A) {} LL | pub fn f1<T>(a: T, x: T::A) {}
| ^ | ^ associated type `A` not found
|
= help: `T` has no associated type, try removing `A`
error[E0221]: ambiguous associated type `A` in bounds of `T` error[E0221]: ambiguous associated type `A` in bounds of `T`
--> $DIR/associated-types-path-1.rs:11:34 --> $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 --> $DIR/feature-gate-return_type_notation.rs:14:17
| |
LL | fn foo<T: Trait<m(): Send>>() {} LL | fn foo<T: Trait<m(): Send>>() {}
| ^ | ^ associated type `m` not found
|
= help: `Trait` has no associated type, try removing `m`
error: aborting due to 3 previous errors 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 --> $DIR/feature-gate-return_type_notation.rs:17:17
| |
LL | fn foo<T: Trait<m(): Send>>() {} LL | fn foo<T: Trait<m(): Send>>() {}
| ^ | ^ associated type `m` not found
|
= help: `Trait` has no associated type, try removing `m`
error: aborting due to 3 previous errors 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 --> $DIR/feature-gate-return_type_notation.rs:17:17
| |
LL | fn foo<T: Trait<m(): Send>>() {} LL | fn foo<T: Trait<m(): Send>>() {}
| ^ | ^ associated type `m` not found
|
= help: `Trait` has no associated type, try removing `m`
error: aborting due to 3 previous errors error: aborting due to 3 previous errors