Skip to content

Commit 0785167

Browse files
Point out the actual mismatch error
1 parent 8be12f4 commit 0785167

21 files changed

+52
-1
lines changed

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

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// ignore-tidy-filelength :(
2+
13
mod ambiguity;
24
pub mod on_unimplemented;
35
pub mod suggestions;
@@ -1943,6 +1945,8 @@ impl<'tcx> InferCtxtPrivExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
19431945
other: bool,
19441946
param_env: ty::ParamEnv<'tcx>,
19451947
) -> bool {
1948+
// If we have a single implementation, try to unify it with the trait ref
1949+
// that failed. This should uncover a better hint for what *is* implemented.
19461950
if let [single] = &impl_candidates {
19471951
if self.probe(|_| {
19481952
let ocx = ObligationCtxt::new(self);
@@ -1972,7 +1976,7 @@ impl<'tcx> InferCtxtPrivExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
19721976
std::iter::zip(obligation_trait_ref.args, impl_trait_ref.args)
19731977
{
19741978
if let Err(terr) =
1975-
ocx.eq(&ObligationCause::dummy(), param_env, obligation_arg, impl_arg)
1979+
ocx.eq(&ObligationCause::dummy(), param_env, impl_arg, obligation_arg)
19761980
{
19771981
terrs.push(terr);
19781982
}
@@ -2000,6 +2004,15 @@ impl<'tcx> InferCtxtPrivExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
20002004
(cand.self_ty().to_string(), Style::Highlight),
20012005
("`".to_string(), Style::NoStyle),
20022006
]);
2007+
2008+
if let [TypeError::Sorts(exp_found)] = &terrs[..] {
2009+
let exp_found = self.resolve_vars_if_possible(*exp_found);
2010+
err.help(format!(
2011+
"for that trait implementation, expected `{}`, found `{}`",
2012+
exp_found.expected, exp_found.found
2013+
));
2014+
}
2015+
20032016
true
20042017
}) {
20052018
return true;

tests/ui/generic-const-items/unsatisfied-bounds.stderr

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ LL | let () = K::<()>;
1717
| ^^ the trait `From<()>` is not implemented for `Infallible`
1818
|
1919
= help: the trait `From<!>` is implemented for `Infallible`
20+
= help: for that trait implementation, expected `!`, found `()`
2021
note: required by a bound in `K`
2122
--> $DIR/unsatisfied-bounds.rs:12:17
2223
|
@@ -48,6 +49,7 @@ LL | let _ = <() as Trait<&'static str>>::B::<()>;
4849
| ^^ the trait `From<()>` is not implemented for `Infallible`
4950
|
5051
= help: the trait `From<!>` is implemented for `Infallible`
52+
= help: for that trait implementation, expected `!`, found `()`
5153
note: required by a bound in `Trait::B`
5254
--> $DIR/unsatisfied-bounds.rs:21:21
5355
|

tests/ui/impl-trait/issues/issue-62742.stderr

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ LL | WrongImpl::<()>::foo(0i32);
4343
| ^^^^^^^^^^^^^^^ the trait `Raw<()>` is not implemented for `RawImpl<()>`
4444
|
4545
= help: the trait `Raw<[()]>` is implemented for `RawImpl<()>`
46+
= help: for that trait implementation, expected `[()]`, found `()`
4647
note: required by a bound in `SafeImpl`
4748
--> $DIR/issue-62742.rs:26:35
4849
|

tests/ui/indexing/index-help.stderr

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ LL | x[0i32];
66
|
77
= help: the trait `SliceIndex<[{integer}]>` is not implemented for `i32`
88
= help: the trait `SliceIndex<[{integer}]>` is implemented for `usize`
9+
= help: for that trait implementation, expected `usize`, found `i32`
910
= note: required for `Vec<{integer}>` to implement `Index<i32>`
1011

1112
error: aborting due to previous error

tests/ui/indexing/indexing-requires-a-uint.stderr

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ LL | [0][0u8];
66
|
77
= help: the trait `SliceIndex<[{integer}]>` is not implemented for `u8`
88
= help: the trait `SliceIndex<[{integer}]>` is implemented for `usize`
9+
= help: for that trait implementation, expected `usize`, found `u8`
910
= note: required for `[{integer}]` to implement `Index<u8>`
1011

1112
error[E0308]: mismatched types

tests/ui/integral-indexing.stderr

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ LL | v[3u8];
66
|
77
= help: the trait `SliceIndex<[isize]>` is not implemented for `u8`
88
= help: the trait `SliceIndex<[isize]>` is implemented for `usize`
9+
= help: for that trait implementation, expected `usize`, found `u8`
910
= note: required for `Vec<isize>` to implement `Index<u8>`
1011

1112
error[E0277]: the type `[isize]` cannot be indexed by `i8`
@@ -16,6 +17,7 @@ LL | v[3i8];
1617
|
1718
= help: the trait `SliceIndex<[isize]>` is not implemented for `i8`
1819
= help: the trait `SliceIndex<[isize]>` is implemented for `usize`
20+
= help: for that trait implementation, expected `usize`, found `i8`
1921
= note: required for `Vec<isize>` to implement `Index<i8>`
2022

2123
error[E0277]: the type `[isize]` cannot be indexed by `u32`
@@ -26,6 +28,7 @@ LL | v[3u32];
2628
|
2729
= help: the trait `SliceIndex<[isize]>` is not implemented for `u32`
2830
= help: the trait `SliceIndex<[isize]>` is implemented for `usize`
31+
= help: for that trait implementation, expected `usize`, found `u32`
2932
= note: required for `Vec<isize>` to implement `Index<u32>`
3033

3134
error[E0277]: the type `[isize]` cannot be indexed by `i32`
@@ -36,6 +39,7 @@ LL | v[3i32];
3639
|
3740
= help: the trait `SliceIndex<[isize]>` is not implemented for `i32`
3841
= help: the trait `SliceIndex<[isize]>` is implemented for `usize`
42+
= help: for that trait implementation, expected `usize`, found `i32`
3943
= note: required for `Vec<isize>` to implement `Index<i32>`
4044

4145
error[E0277]: the type `[u8]` cannot be indexed by `u8`
@@ -46,6 +50,7 @@ LL | s.as_bytes()[3u8];
4650
|
4751
= help: the trait `SliceIndex<[u8]>` is not implemented for `u8`
4852
= help: the trait `SliceIndex<[u8]>` is implemented for `usize`
53+
= help: for that trait implementation, expected `usize`, found `u8`
4954
= note: required for `[u8]` to implement `Index<u8>`
5055

5156
error[E0277]: the type `[u8]` cannot be indexed by `i8`
@@ -56,6 +61,7 @@ LL | s.as_bytes()[3i8];
5661
|
5762
= help: the trait `SliceIndex<[u8]>` is not implemented for `i8`
5863
= help: the trait `SliceIndex<[u8]>` is implemented for `usize`
64+
= help: for that trait implementation, expected `usize`, found `i8`
5965
= note: required for `[u8]` to implement `Index<i8>`
6066

6167
error[E0277]: the type `[u8]` cannot be indexed by `u32`
@@ -66,6 +72,7 @@ LL | s.as_bytes()[3u32];
6672
|
6773
= help: the trait `SliceIndex<[u8]>` is not implemented for `u32`
6874
= help: the trait `SliceIndex<[u8]>` is implemented for `usize`
75+
= help: for that trait implementation, expected `usize`, found `u32`
6976
= note: required for `[u8]` to implement `Index<u32>`
7077

7178
error[E0277]: the type `[u8]` cannot be indexed by `i32`
@@ -76,6 +83,7 @@ LL | s.as_bytes()[3i32];
7683
|
7784
= help: the trait `SliceIndex<[u8]>` is not implemented for `i32`
7885
= help: the trait `SliceIndex<[u8]>` is implemented for `usize`
86+
= help: for that trait implementation, expected `usize`, found `i32`
7987
= note: required for `[u8]` to implement `Index<i32>`
8088

8189
error: aborting due to 8 previous errors

tests/ui/issues/issue-34334.stderr

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ LL | let sr2: Vec<(u32, _, _)> = sr.iter().map(|(faction, th_sender, th_rece
2020
|
2121
= help: the trait `FromIterator<()>` is not implemented for `Vec<(u32, _, _)>`
2222
= help: the trait `FromIterator<(u32, _, _)>` is implemented for `Vec<(u32, _, _)>`
23+
= help: for that trait implementation, expected `(u32, _, _)`, found `()`
2324
note: the method call chain might not have had the expected associated types
2425
--> $DIR/issue-34334.rs:5:43
2526
|

tests/ui/issues/issue-45801.stderr

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ LL | req.get_ref::<Params>();
55
| ^^^^^^^ the trait `Plugin<i32>` is not implemented for `Params`
66
|
77
= help: the trait `Plugin<Foo>` is implemented for `Params`
8+
= help: for that trait implementation, expected `Foo`, found `i32`
89

910
error: aborting due to previous error
1011

tests/ui/issues/issue-66923-show-error-for-correct-call.stderr

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ LL | let x2: Vec<f64> = x1.into_iter().collect();
66
|
77
= help: the trait `FromIterator<&f64>` is not implemented for `Vec<f64>`
88
= help: the trait `FromIterator<f64>` is implemented for `Vec<f64>`
9+
= help: for that trait implementation, expected `f64`, found `&f64`
910
note: the method call chain might not have had the expected associated types
1011
--> $DIR/issue-66923-show-error-for-correct-call.rs:8:27
1112
|
@@ -26,6 +27,7 @@ LL | let x3 = x1.into_iter().collect::<Vec<f64>>();
2627
|
2728
= help: the trait `FromIterator<&f64>` is not implemented for `Vec<f64>`
2829
= help: the trait `FromIterator<f64>` is implemented for `Vec<f64>`
30+
= help: for that trait implementation, expected `f64`, found `&f64`
2931
note: the method call chain might not have had the expected associated types
3032
--> $DIR/issue-66923-show-error-for-correct-call.rs:12:17
3133
|

tests/ui/iterators/invalid-iterator-chain.stderr

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ LL | i.collect()
66
|
77
= help: the trait `FromIterator<&X>` is not implemented for `Vec<X>`
88
= help: the trait `FromIterator<X>` is implemented for `Vec<X>`
9+
= help: for that trait implementation, expected `X`, found `&X`
910
note: the method call chain might not have had the expected associated types
1011
--> $DIR/invalid-iterator-chain.rs:4:26
1112
|
@@ -160,6 +161,7 @@ LL | let g: Vec<i32> = f.collect();
160161
|
161162
= help: the trait `FromIterator<()>` is not implemented for `Vec<i32>`
162163
= help: the trait `FromIterator<i32>` is implemented for `Vec<i32>`
164+
= help: for that trait implementation, expected `i32`, found `()`
163165
note: the method call chain might not have had the expected associated types
164166
--> $DIR/invalid-iterator-chain.rs:44:15
165167
|

0 commit comments

Comments
 (0)