Skip to content

Commit

Permalink
Point out the actual mismatch error
Browse files Browse the repository at this point in the history
  • Loading branch information
compiler-errors committed Sep 10, 2023
1 parent 203c93d commit 3c81efe
Show file tree
Hide file tree
Showing 21 changed files with 52 additions and 1 deletion.
15 changes: 14 additions & 1 deletion compiler/rustc_trait_selection/src/traits/error_reporting/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// ignore-tidy-filelength :(

mod ambiguity;
pub mod on_unimplemented;
pub mod suggestions;
Expand Down Expand Up @@ -1941,6 +1943,8 @@ impl<'tcx> InferCtxtPrivExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
other: bool,
param_env: ty::ParamEnv<'tcx>,
) -> bool {
// If we have a single implementation, try to unify it with the trait ref
// that failed. This should uncover a better hint for what *is* implemented.
if let [single] = &impl_candidates {
if self.probe(|_| {
let ocx = ObligationCtxt::new(self);
Expand Down Expand Up @@ -1970,7 +1974,7 @@ impl<'tcx> InferCtxtPrivExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
std::iter::zip(obligation_trait_ref.args, impl_trait_ref.args)
{
if let Err(terr) =
ocx.eq(&ObligationCause::dummy(), param_env, obligation_arg, impl_arg)
ocx.eq(&ObligationCause::dummy(), param_env, impl_arg, obligation_arg)
{
terrs.push(terr);
}
Expand Down Expand Up @@ -1998,6 +2002,15 @@ impl<'tcx> InferCtxtPrivExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
(cand.self_ty().to_string(), Style::Highlight),
("`".to_string(), Style::NoStyle),
]);

if let [TypeError::Sorts(exp_found)] = &terrs[..] {
let exp_found = self.resolve_vars_if_possible(*exp_found);
err.help(format!(
"... for that trait implementation, expected `{}`, found `{}`",
exp_found.expected, exp_found.found
));
}

true
}) {
return true;
Expand Down
2 changes: 2 additions & 0 deletions tests/ui/generic-const-items/unsatisfied-bounds.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ LL | let () = K::<()>;
| ^^ the trait `From<()>` is not implemented for `Infallible`
|
= help: the trait `From<!>` is implemented for `Infallible`
= help: ... for that trait implementation, expected `!`, found `()`
note: required by a bound in `K`
--> $DIR/unsatisfied-bounds.rs:12:17
|
Expand Down Expand Up @@ -48,6 +49,7 @@ LL | let _ = <() as Trait<&'static str>>::B::<()>;
| ^^ the trait `From<()>` is not implemented for `Infallible`
|
= help: the trait `From<!>` is implemented for `Infallible`
= help: ... for that trait implementation, expected `!`, found `()`
note: required by a bound in `Trait::B`
--> $DIR/unsatisfied-bounds.rs:21:21
|
Expand Down
1 change: 1 addition & 0 deletions tests/ui/impl-trait/issues/issue-62742.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ LL | WrongImpl::<()>::foo(0i32);
| ^^^^^^^^^^^^^^^ the trait `Raw<()>` is not implemented for `RawImpl<()>`
|
= help: the trait `Raw<[()]>` is implemented for `RawImpl<()>`
= help: ... for that trait implementation, expected `[()]`, found `()`
note: required by a bound in `SafeImpl`
--> $DIR/issue-62742.rs:26:35
|
Expand Down
1 change: 1 addition & 0 deletions tests/ui/indexing/index-help.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ LL | x[0i32];
|
= help: the trait `SliceIndex<[{integer}]>` is not implemented for `i32`
= help: the trait `SliceIndex<[{integer}]>` is implemented for `usize`
= help: ... for that trait implementation, expected `usize`, found `i32`
= note: required for `Vec<{integer}>` to implement `Index<i32>`

error: aborting due to previous error
Expand Down
1 change: 1 addition & 0 deletions tests/ui/indexing/indexing-requires-a-uint.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ LL | [0][0u8];
|
= help: the trait `SliceIndex<[{integer}]>` is not implemented for `u8`
= help: the trait `SliceIndex<[{integer}]>` is implemented for `usize`
= help: ... for that trait implementation, expected `usize`, found `u8`
= note: required for `[{integer}]` to implement `Index<u8>`

error[E0308]: mismatched types
Expand Down
8 changes: 8 additions & 0 deletions tests/ui/integral-indexing.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ LL | v[3u8];
|
= help: the trait `SliceIndex<[isize]>` is not implemented for `u8`
= help: the trait `SliceIndex<[isize]>` is implemented for `usize`
= help: ... for that trait implementation, expected `usize`, found `u8`
= note: required for `Vec<isize>` to implement `Index<u8>`

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

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

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

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

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

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

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

error: aborting due to 8 previous errors
Expand Down
1 change: 1 addition & 0 deletions tests/ui/issues/issue-34334.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ LL | let sr2: Vec<(u32, _, _)> = sr.iter().map(|(faction, th_sender, th_rece
|
= help: the trait `FromIterator<()>` is not implemented for `Vec<(u32, _, _)>`
= help: the trait `FromIterator<(u32, _, _)>` is implemented for `Vec<(u32, _, _)>`
= help: ... for that trait implementation, expected `(u32, _, _)`, found `()`
note: the method call chain might not have had the expected associated types
--> $DIR/issue-34334.rs:5:43
|
Expand Down
1 change: 1 addition & 0 deletions tests/ui/issues/issue-45801.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ LL | req.get_ref::<Params>();
| ^^^^^^^ the trait `Plugin<i32>` is not implemented for `Params`
|
= help: the trait `Plugin<Foo>` is implemented for `Params`
= help: ... for that trait implementation, expected `Foo`, found `i32`

error: aborting due to previous error

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ LL | let x2: Vec<f64> = x1.into_iter().collect();
|
= help: the trait `FromIterator<&f64>` is not implemented for `Vec<f64>`
= help: the trait `FromIterator<f64>` is implemented for `Vec<f64>`
= help: ... for that trait implementation, expected `f64`, found `&f64`
note: the method call chain might not have had the expected associated types
--> $DIR/issue-66923-show-error-for-correct-call.rs:8:27
|
Expand All @@ -26,6 +27,7 @@ LL | let x3 = x1.into_iter().collect::<Vec<f64>>();
|
= help: the trait `FromIterator<&f64>` is not implemented for `Vec<f64>`
= help: the trait `FromIterator<f64>` is implemented for `Vec<f64>`
= help: ... for that trait implementation, expected `f64`, found `&f64`
note: the method call chain might not have had the expected associated types
--> $DIR/issue-66923-show-error-for-correct-call.rs:12:17
|
Expand Down
2 changes: 2 additions & 0 deletions tests/ui/iterators/invalid-iterator-chain.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ LL | i.collect()
|
= help: the trait `FromIterator<&X>` is not implemented for `Vec<X>`
= help: the trait `FromIterator<X>` is implemented for `Vec<X>`
= help: ... for that trait implementation, expected `X`, found `&X`
note: the method call chain might not have had the expected associated types
--> $DIR/invalid-iterator-chain.rs:4:26
|
Expand Down Expand Up @@ -160,6 +161,7 @@ LL | let g: Vec<i32> = f.collect();
|
= help: the trait `FromIterator<()>` is not implemented for `Vec<i32>`
= help: the trait `FromIterator<i32>` is implemented for `Vec<i32>`
= help: ... for that trait implementation, expected `i32`, found `()`
note: the method call chain might not have had the expected associated types
--> $DIR/invalid-iterator-chain.rs:44:15
|
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ LL | <E as From<_>>::from(never);
| ^ the trait `From<()>` is not implemented for `E`
|
= help: the trait `From<!>` is implemented for `E`
= help: ... for that trait implementation, expected `!`, found `()`

error: aborting due to previous error

Expand Down
1 change: 1 addition & 0 deletions tests/ui/on-unimplemented/impl-substs.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ LL | Foo::<usize>::foo((1i32, 1i32, 1i32));
|
= help: the trait `Foo<usize>` is not implemented for `(i32, i32, i32)`
= help: the trait `Foo<i32>` is implemented for `(i32, i32, i32)`
= help: ... for that trait implementation, expected `i32`, found `usize`

error: aborting due to previous error

Expand Down
3 changes: 3 additions & 0 deletions tests/ui/on-unimplemented/on-impl.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ LL | Index::<u32>::index(&[1, 2, 3] as &[i32], 2u32);
|
= help: the trait `Index<u32>` is not implemented for `[i32]`
= help: the trait `Index<usize>` is implemented for `[i32]`
= help: ... for that trait implementation, expected `usize`, found `u32`

error[E0277]: the trait bound `[i32]: Index<u32>` is not satisfied
--> $DIR/on-impl.rs:22:5
Expand All @@ -17,6 +18,7 @@ LL | Index::<u32>::index(&[1, 2, 3] as &[i32], 2u32);
|
= help: the trait `Index<u32>` is not implemented for `[i32]`
= help: the trait `Index<usize>` is implemented for `[i32]`
= help: ... for that trait implementation, expected `usize`, found `u32`

error[E0277]: the trait bound `[i32]: Index<u32>` is not satisfied
--> $DIR/on-impl.rs:22:5
Expand All @@ -26,6 +28,7 @@ LL | Index::<u32>::index(&[1, 2, 3] as &[i32], 2u32);
|
= help: the trait `Index<u32>` is not implemented for `[i32]`
= help: the trait `Index<usize>` is implemented for `[i32]`
= help: ... for that trait implementation, expected `usize`, found `u32`

error: aborting due to 3 previous errors

Expand Down
1 change: 1 addition & 0 deletions tests/ui/on-unimplemented/slice-index.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ LL | x[1i32];
|
= help: the trait `SliceIndex<[i32]>` is not implemented for `i32`
= help: the trait `SliceIndex<[i32]>` is implemented for `usize`
= help: ... for that trait implementation, expected `usize`, found `i32`
= note: required for `[i32]` to implement `Index<i32>`

error[E0277]: the type `[i32]` cannot be indexed by `RangeTo<i32>`
Expand Down
3 changes: 3 additions & 0 deletions tests/ui/str/str-idx.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ LL | let _: u8 = s[4];
= note: you can use `.chars().nth()` or `.bytes().nth()`
for more information, see chapter 8 in The Book: <https://doc.rust-lang.org/book/ch08-02-strings.html#indexing-into-strings>
= help: the trait `SliceIndex<[_]>` is implemented for `usize`
= help: ... for that trait implementation, expected `[_]`, found `str`
= note: required for `str` to implement `Index<{integer}>`

error[E0277]: the type `str` cannot be indexed by `{integer}`
Expand All @@ -22,6 +23,7 @@ LL | let _ = s.get(4);
= note: you can use `.chars().nth()` or `.bytes().nth()`
for more information, see chapter 8 in The Book: <https://doc.rust-lang.org/book/ch08-02-strings.html#indexing-into-strings>
= help: the trait `SliceIndex<[_]>` is implemented for `usize`
= help: ... for that trait implementation, expected `[_]`, found `str`
note: required by a bound in `core::str::<impl str>::get`
--> $SRC_DIR/core/src/str/mod.rs:LL:COL

Expand All @@ -37,6 +39,7 @@ LL | let _ = s.get_unchecked(4);
= note: you can use `.chars().nth()` or `.bytes().nth()`
for more information, see chapter 8 in The Book: <https://doc.rust-lang.org/book/ch08-02-strings.html#indexing-into-strings>
= help: the trait `SliceIndex<[_]>` is implemented for `usize`
= help: ... for that trait implementation, expected `[_]`, found `str`
note: required by a bound in `core::str::<impl str>::get_unchecked`
--> $SRC_DIR/core/src/str/mod.rs:LL:COL

Expand Down
3 changes: 3 additions & 0 deletions tests/ui/str/str-mut-idx.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ LL | s[1usize] = bot();
|
= help: the trait `SliceIndex<str>` is not implemented for `usize`
= help: the trait `SliceIndex<[_]>` is implemented for `usize`
= help: ... for that trait implementation, expected `[_]`, found `str`
= note: required for `str` to implement `Index<usize>`

error[E0277]: the type `str` cannot be indexed by `{integer}`
Expand All @@ -46,6 +47,7 @@ LL | s.get_mut(1);
= note: you can use `.chars().nth()` or `.bytes().nth()`
for more information, see chapter 8 in The Book: <https://doc.rust-lang.org/book/ch08-02-strings.html#indexing-into-strings>
= help: the trait `SliceIndex<[_]>` is implemented for `usize`
= help: ... for that trait implementation, expected `[_]`, found `str`
note: required by a bound in `core::str::<impl str>::get_mut`
--> $SRC_DIR/core/src/str/mod.rs:LL:COL

Expand All @@ -61,6 +63,7 @@ LL | s.get_unchecked_mut(1);
= note: you can use `.chars().nth()` or `.bytes().nth()`
for more information, see chapter 8 in The Book: <https://doc.rust-lang.org/book/ch08-02-strings.html#indexing-into-strings>
= help: the trait `SliceIndex<[_]>` is implemented for `usize`
= help: ... for that trait implementation, expected `[_]`, found `str`
note: required by a bound in `core::str::<impl str>::get_unchecked_mut`
--> $SRC_DIR/core/src/str/mod.rs:LL:COL

Expand Down
1 change: 1 addition & 0 deletions tests/ui/suggestions/issue-101623.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ LL | Trait::do_stuff({ fun(&mut *inner) });
| required by a bound introduced by this call
|
= help: the trait `Trait<'_>` is implemented for `()`
= help: ... for that trait implementation, expected `()`, found `*mut ()`

error: aborting due to previous error

Expand Down
1 change: 1 addition & 0 deletions tests/ui/suggestions/suggest-dereferencing-index.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ LL | let one_item_please: i32 = [1, 2, 3][i];
|
= help: the trait `SliceIndex<[{integer}]>` is not implemented for `&usize`
= help: the trait `SliceIndex<[{integer}]>` is implemented for `usize`
= help: ... for that trait implementation, expected `usize`, found `&usize`
= note: required for `[{integer}]` to implement `Index<&usize>`
help: dereference this index
|
Expand Down
1 change: 1 addition & 0 deletions tests/ui/traits/coercion-generic-bad.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ LL | let s: Box<dyn Trait<isize>> = Box::new(Struct { person: "Fred" });
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `Trait<isize>` is not implemented for `Struct`
|
= help: the trait `Trait<&'static str>` is implemented for `Struct`
= help: ... for that trait implementation, expected `&'static str`, found `isize`
= note: required for the cast from `Box<Struct>` to `Box<dyn Trait<isize>>`

error: aborting due to previous error
Expand Down
1 change: 1 addition & 0 deletions tests/ui/try-block/try-block-bad-type.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ LL | Err("")?;
|
= note: the question mark operation (`?`) implicitly performs a conversion on the error value using the `From` trait
= help: the trait `From<Infallible>` is implemented for `TryFromSliceError`
= help: ... for that trait implementation, expected `Infallible`, found `&str`
= note: required for `Result<u32, TryFromSliceError>` to implement `FromResidual<Result<Infallible, &str>>`

error[E0271]: type mismatch resolving `<Result<i32, i32> as Try>::Output == &str`
Expand Down
3 changes: 3 additions & 0 deletions tests/ui/try-trait/bad-interconversion.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ LL | ControlFlow::Continue(Err("hello")?)
|
= help: the trait `FromResidual<Result<Infallible, &str>>` is not implemented for `ControlFlow<String>`
= help: the trait `FromResidual<ControlFlow<String, Infallible>>` is implemented for `ControlFlow<String>`
= help: ... for that trait implementation, expected `ControlFlow<String, Infallible>`, found `Result<Infallible, &str>`

error[E0277]: the `?` operator can only be used on `ControlFlow`s in a function that returns `ControlFlow`
--> $DIR/bad-interconversion.rs:37:12
Expand All @@ -85,6 +86,7 @@ LL | Some(3)?;
|
= help: the trait `FromResidual<Option<Infallible>>` is not implemented for `ControlFlow<u64>`
= help: the trait `FromResidual<ControlFlow<u64, Infallible>>` is implemented for `ControlFlow<u64>`
= help: ... for that trait implementation, expected `ControlFlow<u64, Infallible>`, found `Option<Infallible>`

error[E0277]: the `?` operator in a function that returns `ControlFlow<B, _>` can only be used on other `ControlFlow<B, _>`s (with the same Break type)
--> $DIR/bad-interconversion.rs:43:29
Expand All @@ -97,6 +99,7 @@ LL | ControlFlow::Break(4_u8)?;
= help: the trait `FromResidual<ControlFlow<u8, Infallible>>` is not implemented for `ControlFlow<i64>`
= note: unlike `Result`, there's no `From`-conversion performed for `ControlFlow`
= help: the trait `FromResidual<ControlFlow<i64, Infallible>>` is implemented for `ControlFlow<i64>`
= help: ... for that trait implementation, expected `i64`, found `u8`

error: aborting due to 8 previous errors

Expand Down

0 comments on commit 3c81efe

Please sign in to comment.