Skip to content

Commit a0e0a32

Browse files
committed
E0220: only suggests associated types if there's only one candidate
1 parent 96c9664 commit a0e0a32

File tree

7 files changed

+27
-49
lines changed

7 files changed

+27
-49
lines changed

compiler/rustc_hir_analysis/src/astconv/errors.rs

+19-25
Original file line numberDiff line numberDiff line change
@@ -201,33 +201,27 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
201201
}
202202
}
203203

204-
// If we still couldn't find any associated type, just list them all.
205-
206-
if all_candidate_names.is_empty() {
207-
err.help(format!(
208-
"`{ty_param_name}` has no associated type, try removing `{assoc_name}`"
209-
));
210-
return err.emit();
211-
}
212-
213-
let msg = if all_candidate_names.len() > 1 {
214-
format!("`{ty_param_name}` has the following associated types")
215-
} else {
216-
format!("`{ty_param_name}` has the following associated type")
217-
};
204+
// If we still couldn't find any associated type, and only one associated type exists,
205+
// suggests using it.
206+
207+
if all_candidate_names.len() == 1 {
208+
// this should still compile, except on `#![feature(associated_type_defaults)]`
209+
// where it could suggests `type A = Self::A`, thus recursing infinitely
210+
let applicability = if self.tcx().features().associated_type_defaults {
211+
Applicability::Unspecified
212+
} else {
213+
Applicability::MaybeIncorrect
214+
};
218215

219-
let applicability = if self.tcx().features().associated_type_defaults {
220-
Applicability::Unspecified // `type A = Self::B` would suggest `type A = Self::A`
216+
err.span_suggestion(
217+
assoc_name.span,
218+
format!("`{ty_param_name}` has the following associated type"),
219+
all_candidate_names.first().unwrap().to_string(),
220+
applicability,
221+
);
221222
} else {
222-
Applicability::MaybeIncorrect
223-
};
224-
225-
err.span_suggestions(
226-
assoc_name.span,
227-
msg,
228-
all_candidate_names.iter().map(|symbol| symbol.to_string()),
229-
applicability,
230-
);
223+
err.span_label(assoc_name.span, format!("associated type `{assoc_name}` not found"));
224+
}
231225

232226
err.emit()
233227
}

tests/ui/associated-consts/assoc-const-eq-missing.stderr

+3-9
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,19 @@ error[E0220]: associated type `Z` not found for `Foo`
22
--> $DIR/assoc-const-eq-missing.rs:15:16
33
|
44
LL | fn foo1<F: Foo<Z=3>>() {}
5-
| ^
6-
|
7-
= help: `Foo` has no associated type, try removing `Z`
5+
| ^ associated type `Z` not found
86

97
error[E0220]: associated type `Z` not found for `Foo`
108
--> $DIR/assoc-const-eq-missing.rs:17:16
119
|
1210
LL | fn foo2<F: Foo<Z=usize>>() {}
13-
| ^
14-
|
15-
= help: `Foo` has no associated type, try removing `Z`
11+
| ^ associated type `Z` not found
1612

1713
error[E0220]: associated type `Z` not found for `Foo`
1814
--> $DIR/assoc-const-eq-missing.rs:19:16
1915
|
2016
LL | fn foo3<F: Foo<Z=5>>() {}
21-
| ^
22-
|
23-
= help: `Foo` has no associated type, try removing `Z`
17+
| ^ associated type `Z` not found
2418

2519
error: aborting due to 3 previous errors
2620

tests/ui/associated-type-bounds/missing-trait-bound-for-assoc-fails.stderr

+1-3
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,7 @@ error[E0220]: associated type `Item` not found for `M`
88
--> $DIR/missing-trait-bound-for-assoc-fails.rs:4:8
99
|
1010
LL | M::Item: Temp,
11-
| ^^^^
12-
|
13-
= help: `M` has no associated type, try removing `Item`
11+
| ^^^^ associated type `Item` not found
1412

1513
error: aborting due to 2 previous errors
1614

tests/ui/associated-types/associated-types-path-1.stderr

+1-3
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,7 @@ error[E0220]: associated type `A` not found for `T`
22
--> $DIR/associated-types-path-1.rs:10:26
33
|
44
LL | pub fn f1<T>(a: T, x: T::A) {}
5-
| ^
6-
|
7-
= help: `T` has no associated type, try removing `A`
5+
| ^ associated type `A` not found
86

97
error[E0221]: ambiguous associated type `A` in bounds of `T`
108
--> $DIR/associated-types-path-1.rs:11:34

tests/ui/feature-gates/feature-gate-return_type_notation.cfg.stderr

+1-3
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,7 @@ error[E0220]: associated type `m` not found for `Trait`
1919
--> $DIR/feature-gate-return_type_notation.rs:14:17
2020
|
2121
LL | fn foo<T: Trait<m(): Send>>() {}
22-
| ^
23-
|
24-
= help: `Trait` has no associated type, try removing `m`
22+
| ^ associated type `m` not found
2523

2624
error: aborting due to 3 previous errors
2725

tests/ui/feature-gates/feature-gate-return_type_notation.cfg_current.stderr

+1-3
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,7 @@ error[E0220]: associated type `m` not found for `Trait`
1919
--> $DIR/feature-gate-return_type_notation.rs:17:17
2020
|
2121
LL | fn foo<T: Trait<m(): Send>>() {}
22-
| ^
23-
|
24-
= help: `Trait` has no associated type, try removing `m`
22+
| ^ associated type `m` not found
2523

2624
error: aborting due to 3 previous errors
2725

tests/ui/feature-gates/feature-gate-return_type_notation.cfg_next.stderr

+1-3
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,7 @@ error[E0220]: associated type `m` not found for `Trait`
1919
--> $DIR/feature-gate-return_type_notation.rs:17:17
2020
|
2121
LL | fn foo<T: Trait<m(): Send>>() {}
22-
| ^
23-
|
24-
= help: `Trait` has no associated type, try removing `m`
22+
| ^ associated type `m` not found
2523

2624
error: aborting due to 3 previous errors
2725

0 commit comments

Comments
 (0)