Skip to content

Commit

Permalink
E0220: only suggests associated types if there's only one candidate
Browse files Browse the repository at this point in the history
  • Loading branch information
ShE3py committed Sep 8, 2023
1 parent 96c9664 commit a0e0a32
Show file tree
Hide file tree
Showing 7 changed files with 27 additions and 49 deletions.
44 changes: 19 additions & 25 deletions compiler/rustc_hir_analysis/src/astconv/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -201,33 +201,27 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
}
}

// If we still couldn't find any associated type, just list them all.

if all_candidate_names.is_empty() {
err.help(format!(
"`{ty_param_name}` has no associated type, try removing `{assoc_name}`"
));
return err.emit();
}

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")
};
// If we still couldn't find any associated type, and only one associated type exists,
// suggests using it.

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
};

let applicability = if self.tcx().features().associated_type_defaults {
Applicability::Unspecified // `type A = Self::B` would suggest `type A = Self::A`
err.span_suggestion(
assoc_name.span,
format!("`{ty_param_name}` has the following associated type"),
all_candidate_names.first().unwrap().to_string(),
applicability,
);
} else {
Applicability::MaybeIncorrect
};

err.span_suggestions(
assoc_name.span,
msg,
all_candidate_names.iter().map(|symbol| symbol.to_string()),
applicability,
);
err.span_label(assoc_name.span, format!("associated type `{assoc_name}` not found"));
}

err.emit()
}
Expand Down
12 changes: 3 additions & 9 deletions tests/ui/associated-consts/assoc-const-eq-missing.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
4 changes: 1 addition & 3 deletions tests/ui/associated-types/associated-types-path-1.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit a0e0a32

Please sign in to comment.