Rust: Associated types cause "expected" and "found" to be reversed in error messages

Created on 2 Aug 2019  路  6Comments  路  Source: rust-lang/rust

TL;DR: Iterator<Item=...> type mismatches are described wrong.

In some cases -- I've reproduced consistently with associated types, but haven't explored further -- type mismatches are reported incorrectly, with the expected type being reported as "found" and the actual type being reported as "expected".

For example, let _: &Iterator<Item=i8> = &vec![1u32].iter() produces an error where it "expected type &u32; found type i8". That's... not right, though, obviously, since the right-hand side matches Iterator<Item=u32>, and the left side requires Iterator<Item=i8> -- so it should be _expecting_ i8 and _finding_ u32. For a MCVE (playground):

trait F {
    type T;
}
struct Fi<A>(A);
impl<A> F for Fi<A> {
    type T = A;
}
fn main() {
    let _: &dyn F<T=i8> = &Fi(1u32);
}

This is a relatively small bug, but it makes debugging extremely difficult when you don't know what's happening, and just see that it's expecting _exactly_ the type you're providing it.

It _does not_ occur with generics, or plain wrong types -- e.g. let _: Fi<i8> = Fi(1u32); and let _: i8 = 1u32 both fail with correct error messages.

I think this might be the same as #57226, but I wanted to bump the issue (since that one hasn't had any attention and I think this is important) as well as provide a somewhat clearer, more minimal example.


Occurs with rustc 1.38.0-nightly as well as beta and stable.

A-diagnostics A-typesystem T-compiler

Most helpful comment

@gralpli @nic-hartley it's fixed by #65977!
Compiling with the latest nighty shows:

error[E0271]: type mismatch resolving `<Fi<u32> as F>::T == i8`
 --> src/main.rs:9:27
  |
9 |     let _: &dyn F<T=i8> = &Fi(1u32);
  |                           ^^^^^^^^^ expected i8, found u32
  |
  = note: expected type `i8`
             found type `u32`
  = note: required for the cast to the object type `dyn F<T = i8>`

btw @nic-hartley this example is different because the &dyn Trait is handled differently, but the test suit already had an example like this for something else which is why it's fixed 馃槃

All 6 comments

Same as #51767, as is #57226 ?!

@lukaslueg The first one is E0308. The second one I explicitly mentioned.

@gralpli @nic-hartley it's fixed by #65977!
Compiling with the latest nighty shows:

error[E0271]: type mismatch resolving `<Fi<u32> as F>::T == i8`
 --> src/main.rs:9:27
  |
9 |     let _: &dyn F<T=i8> = &Fi(1u32);
  |                           ^^^^^^^^^ expected i8, found u32
  |
  = note: expected type `i8`
             found type `u32`
  = note: required for the cast to the object type `dyn F<T = i8>`

btw @nic-hartley this example is different because the &dyn Trait is handled differently, but the test suit already had an example like this for something else which is why it's fixed 馃槃

@ohadravid @estebank Thanks a ton! Can't wait for 1.40.0 to come out in 6-8 years ;)

@nic-hartley 1.39 comes out in 2019-11-07 and 1.40 6 weeks after that ^_^

@estebank oh dang, that's cool. I actually had no idea. Thanks again!

Was this page helpful?
0 / 5 - 0 ratings