Skip to content

Commit 1310d9b

Browse files
author
Yuki Okushi
authored
Rollup merge of #105338 - estebank:other-impls, r=compiler-errors
Tweak "the following other types implement trait" When *any* of the suggested impls is an exact match, *only* show the exact matches. This is particularly relevant for integer types. r? `@compiler-errors`
2 parents e09c71e + e1649c4 commit 1310d9b

23 files changed

+138
-395
lines changed

compiler/rustc_trait_selection/src/traits/error_reporting/mod.rs

+10-2
Original file line numberDiff line numberDiff line change
@@ -1810,7 +1810,8 @@ impl<'tcx> InferCtxtPrivExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
18101810
&self,
18111811
trait_pred: ty::PolyTraitPredicate<'tcx>,
18121812
) -> Vec<ImplCandidate<'tcx>> {
1813-
self.tcx
1813+
let mut candidates: Vec<_> = self
1814+
.tcx
18141815
.all_impls(trait_pred.def_id())
18151816
.filter_map(|def_id| {
18161817
if self.tcx.impl_polarity(def_id) == ty::ImplPolarity::Negative
@@ -1826,7 +1827,14 @@ impl<'tcx> InferCtxtPrivExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
18261827
self.fuzzy_match_tys(trait_pred.skip_binder().self_ty(), imp.self_ty(), false)
18271828
.map(|similarity| ImplCandidate { trait_ref: imp, similarity })
18281829
})
1829-
.collect()
1830+
.collect();
1831+
if candidates.iter().any(|c| matches!(c.similarity, CandidateSimilarity::Exact { .. })) {
1832+
// If any of the candidates is a perfect match, we don't want to show all of them.
1833+
// This is particularly relevant for the case of numeric types (as they all have the
1834+
// same cathegory).
1835+
candidates.retain(|c| matches!(c.similarity, CandidateSimilarity::Exact { .. }));
1836+
}
1837+
candidates
18301838
}
18311839

18321840
fn report_similar_impl_candidates(

src/test/ui/binop/binop-mul-i32-f32.stderr

+3-8
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,10 @@ LL | x * y
66
|
77
= help: the trait `Mul<f32>` is not implemented for `i32`
88
= help: the following other types implement trait `Mul<Rhs>`:
9-
<&'a f32 as Mul<f32>>
10-
<&'a f64 as Mul<f64>>
11-
<&'a i128 as Mul<i128>>
12-
<&'a i16 as Mul<i16>>
139
<&'a i32 as Mul<i32>>
14-
<&'a i64 as Mul<i64>>
15-
<&'a i8 as Mul<i8>>
16-
<&'a isize as Mul<isize>>
17-
and 49 others
10+
<&i32 as Mul<&i32>>
11+
<i32 as Mul<&i32>>
12+
<i32 as Mul>
1813

1914
error: aborting due to previous error
2015

src/test/ui/const-generics/defaults/rp_impl_trait_fail.stderr

+2-6
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,7 @@ LL |
1818
LL | 1_u32
1919
| ----- return type was inferred to be `u32` here
2020
|
21-
= help: the following other types implement trait `Traitor<N, M>`:
22-
<u32 as Traitor<N, 2>>
23-
<u64 as Traitor<1, 2>>
21+
= help: the trait `Traitor<N, 2>` is implemented for `u32`
2422

2523
error[E0277]: the trait bound `u64: Traitor` is not satisfied
2624
--> $DIR/rp_impl_trait_fail.rs:21:13
@@ -31,9 +29,7 @@ LL |
3129
LL | 1_u64
3230
| ----- return type was inferred to be `u64` here
3331
|
34-
= help: the following other types implement trait `Traitor<N, M>`:
35-
<u32 as Traitor<N, 2>>
36-
<u64 as Traitor<1, 2>>
32+
= help: the trait `Traitor<1, 2>` is implemented for `u64`
3733

3834
error: aborting due to 3 previous errors
3935

src/test/ui/consts/const-eval/const-eval-overflow-3b.stderr

+3-8
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,10 @@ LL | = [0; (i8::MAX + 1u8) as usize];
1212
|
1313
= help: the trait `~const Add<u8>` is not implemented for `i8`
1414
= help: the following other types implement trait `Add<Rhs>`:
15-
<&'a f32 as Add<f32>>
16-
<&'a f64 as Add<f64>>
17-
<&'a i128 as Add<i128>>
18-
<&'a i16 as Add<i16>>
19-
<&'a i32 as Add<i32>>
20-
<&'a i64 as Add<i64>>
2115
<&'a i8 as Add<i8>>
22-
<&'a isize as Add<isize>>
23-
and 48 others
16+
<&i8 as Add<&i8>>
17+
<i8 as Add<&i8>>
18+
<i8 as Add>
2419

2520
error: aborting due to 2 previous errors
2621

src/test/ui/consts/const-eval/const-eval-overflow-4b.stderr

+3-8
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,10 @@ LL | : [u32; (i8::MAX as i8 + 1u8) as usize]
1212
|
1313
= help: the trait `~const Add<u8>` is not implemented for `i8`
1414
= help: the following other types implement trait `Add<Rhs>`:
15-
<&'a f32 as Add<f32>>
16-
<&'a f64 as Add<f64>>
17-
<&'a i128 as Add<i128>>
18-
<&'a i16 as Add<i16>>
19-
<&'a i32 as Add<i32>>
20-
<&'a i64 as Add<i64>>
2115
<&'a i8 as Add<i8>>
22-
<&'a isize as Add<isize>>
23-
and 48 others
16+
<&i8 as Add<&i8>>
17+
<i8 as Add<&i8>>
18+
<i8 as Add>
2419

2520
error[E0604]: only `u8` can be cast as `char`, not `i8`
2621
--> $DIR/const-eval-overflow-4b.rs:22:13

src/test/ui/did_you_mean/issue-39802-show-5-trait-impls.stderr

-9
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,6 @@ LL | Foo::<i32>::bar(&1i8);
1212
<i8 as Foo<u32>>
1313
<i8 as Foo<u64>>
1414
<i8 as Foo<u8>>
15-
<u8 as Foo<bool>>
16-
<u8 as Foo<u16>>
17-
<u8 as Foo<u32>>
18-
<u8 as Foo<u64>>
1915

2016
error[E0277]: the trait bound `u8: Foo<i32>` is not satisfied
2117
--> $DIR/issue-39802-show-5-trait-impls.rs:25:21
@@ -26,11 +22,6 @@ LL | Foo::<i32>::bar(&1u8);
2622
| required by a bound introduced by this call
2723
|
2824
= help: the following other types implement trait `Foo<B>`:
29-
<i8 as Foo<bool>>
30-
<i8 as Foo<u16>>
31-
<i8 as Foo<u32>>
32-
<i8 as Foo<u64>>
33-
<i8 as Foo<u8>>
3425
<u8 as Foo<bool>>
3526
<u8 as Foo<u16>>
3627
<u8 as Foo<u32>>

src/test/ui/impl-trait/equality.stderr

+4-9
Original file line numberDiff line numberDiff line change
@@ -30,15 +30,10 @@ LL | n + sum_to(n - 1)
3030
|
3131
= help: the trait `Add<impl Foo>` is not implemented for `u32`
3232
= help: the following other types implement trait `Add<Rhs>`:
33-
<&'a f32 as Add<f32>>
34-
<&'a f64 as Add<f64>>
35-
<&'a i128 as Add<i128>>
36-
<&'a i16 as Add<i16>>
37-
<&'a i32 as Add<i32>>
38-
<&'a i64 as Add<i64>>
39-
<&'a i8 as Add<i8>>
40-
<&'a isize as Add<isize>>
41-
and 48 others
33+
<&'a u32 as Add<u32>>
34+
<&u32 as Add<&u32>>
35+
<u32 as Add<&u32>>
36+
<u32 as Add>
4237

4338
error: aborting due to 2 previous errors; 1 warning emitted
4439

src/test/ui/issues/issue-24352.stderr

+3-8
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,10 @@ LL | 1.0f64 - 1
66
|
77
= help: the trait `Sub<{integer}>` is not implemented for `f64`
88
= help: the following other types implement trait `Sub<Rhs>`:
9-
<&'a f32 as Sub<f32>>
109
<&'a f64 as Sub<f64>>
11-
<&'a i128 as Sub<i128>>
12-
<&'a i16 as Sub<i16>>
13-
<&'a i32 as Sub<i32>>
14-
<&'a i64 as Sub<i64>>
15-
<&'a i8 as Sub<i8>>
16-
<&'a isize as Sub<isize>>
17-
and 48 others
10+
<&f64 as Sub<&f64>>
11+
<f64 as Sub<&f64>>
12+
<f64 as Sub>
1813
help: consider using a floating-point literal by writing it with `.0`
1914
|
2015
LL | 1.0f64 - 1.0

src/test/ui/kindck/kindck-copy.stderr

+2-20
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,7 @@ error[E0277]: the trait bound `&'static mut isize: Copy` is not satisfied
44
LL | assert_copy::<&'static mut isize>();
55
| ^^^^^^^^^^^^^^^^^^ the trait `Copy` is not implemented for `&'static mut isize`
66
|
7-
= help: the following other types implement trait `Copy`:
8-
f32
9-
f64
10-
i128
11-
i16
12-
i32
13-
i64
14-
i8
15-
isize
16-
and 6 others
7+
= help: the trait `Copy` is implemented for `isize`
178
note: required by a bound in `assert_copy`
189
--> $DIR/kindck-copy.rs:5:18
1910
|
@@ -26,16 +17,7 @@ error[E0277]: the trait bound `&'a mut isize: Copy` is not satisfied
2617
LL | assert_copy::<&'a mut isize>();
2718
| ^^^^^^^^^^^^^ the trait `Copy` is not implemented for `&'a mut isize`
2819
|
29-
= help: the following other types implement trait `Copy`:
30-
f32
31-
f64
32-
i128
33-
i16
34-
i32
35-
i64
36-
i8
37-
isize
38-
and 6 others
20+
= help: the trait `Copy` is implemented for `isize`
3921
note: required by a bound in `assert_copy`
4022
--> $DIR/kindck-copy.rs:5:18
4123
|

src/test/ui/lexer/lex-bad-char-literals-6.stderr

+8-10
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,11 @@ LL | if x == y {}
4242
<&'a str as PartialEq<OsString>>
4343
<&'a str as PartialEq<String>>
4444
<&'b str as PartialEq<Cow<'a, str>>>
45-
<String as PartialEq<&'a str>>
46-
<String as PartialEq<Cow<'a, str>>>
47-
<String as PartialEq<str>>
48-
<String as PartialEq>
4945
<str as PartialEq<Cow<'a, str>>>
50-
and 4 others
46+
<str as PartialEq<OsStr>>
47+
<str as PartialEq<OsString>>
48+
<str as PartialEq<String>>
49+
<str as PartialEq>
5150

5251
error[E0308]: mismatched types
5352
--> $DIR/lex-bad-char-literals-6.rs:15:20
@@ -68,12 +67,11 @@ LL | if x == z {}
6867
<&'a str as PartialEq<OsString>>
6968
<&'a str as PartialEq<String>>
7069
<&'b str as PartialEq<Cow<'a, str>>>
71-
<String as PartialEq<&'a str>>
72-
<String as PartialEq<Cow<'a, str>>>
73-
<String as PartialEq<str>>
74-
<String as PartialEq>
7570
<str as PartialEq<Cow<'a, str>>>
76-
and 4 others
71+
<str as PartialEq<OsStr>>
72+
<str as PartialEq<OsString>>
73+
<str as PartialEq<String>>
74+
<str as PartialEq>
7775

7876
error: aborting due to 6 previous errors
7977

src/test/ui/mismatched_types/binops.stderr

+4-9
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,10 @@ LL | 2 as usize - Some(1);
2424
|
2525
= help: the trait `Sub<Option<{integer}>>` is not implemented for `usize`
2626
= help: the following other types implement trait `Sub<Rhs>`:
27-
<&'a f32 as Sub<f32>>
28-
<&'a f64 as Sub<f64>>
29-
<&'a i128 as Sub<i128>>
30-
<&'a i16 as Sub<i16>>
31-
<&'a i32 as Sub<i32>>
32-
<&'a i64 as Sub<i64>>
33-
<&'a i8 as Sub<i8>>
34-
<&'a isize as Sub<isize>>
35-
and 48 others
27+
<&'a usize as Sub<usize>>
28+
<&usize as Sub<&usize>>
29+
<usize as Sub<&usize>>
30+
<usize as Sub>
3631

3732
error[E0277]: cannot multiply `{integer}` by `()`
3833
--> $DIR/binops.rs:4:7

src/test/ui/never_type/issue-13352.stderr

+4-9
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,10 @@ LL | 2_usize + (loop {});
66
|
77
= help: the trait `Add<()>` is not implemented for `usize`
88
= help: the following other types implement trait `Add<Rhs>`:
9-
<&'a f32 as Add<f32>>
10-
<&'a f64 as Add<f64>>
11-
<&'a i128 as Add<i128>>
12-
<&'a i16 as Add<i16>>
13-
<&'a i32 as Add<i32>>
14-
<&'a i64 as Add<i64>>
15-
<&'a i8 as Add<i8>>
16-
<&'a isize as Add<isize>>
17-
and 48 others
9+
<&'a usize as Add<usize>>
10+
<&usize as Add<&usize>>
11+
<usize as Add<&usize>>
12+
<usize as Add>
1813

1914
error: aborting due to previous error
2015

0 commit comments

Comments
 (0)