Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Associated types cause "expected" and "found" to be reversed in error messages #63211

Closed
nic-hartley opened this issue Aug 2, 2019 · 6 comments
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-type-system Area: Type system T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@nic-hartley
Copy link

nic-hartley commented Aug 2, 2019

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.

@Centril Centril added A-diagnostics Area: Messages for errors, warnings, and lints A-type-system Area: Type system T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Aug 2, 2019
@lukaslueg
Copy link
Contributor

Same as #51767, as is #57226 ?!

@nic-hartley
Copy link
Author

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

@ohadravid
Copy link
Contributor

@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 😄

@estebank estebank closed this as completed Nov 2, 2019
@nic-hartley
Copy link
Author

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

@estebank
Copy link
Contributor

estebank commented Nov 3, 2019

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

@nic-hartley
Copy link
Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-type-system Area: Type system T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

5 participants