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

“expected” and “found” interchanged in error message E0308 #57226

Closed
gralpli opened this issue Dec 31, 2018 · 1 comment · Fixed by #65977
Closed

“expected” and “found” interchanged in error message E0308 #57226

gralpli opened this issue Dec 31, 2018 · 1 comment · Fixed by #65977
Labels
A-diagnostics Area: Messages for errors, warnings, and lints D-incorrect Diagnostics: A diagnostic that is giving misleading or incorrect information. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@gralpli
Copy link
Contributor

gralpli commented Dec 31, 2018

I’m using rustc 1.33.0-nightly (a2b0f247b 2018-12-30).

trait MyTrait {
    type Item;
}

struct MyStruct<T> {
    item: T
}

impl<T> MyTrait for MyStruct<T> {
    type Item = T;
}

fn func<T: MyTrait<Item=i32>>(t: T) { }

fn main() {
    func(MyStruct { item: "str" });
}

func expects an MyTrait<Item=i32>, so the error message should say: “expected type i32, found type &str.” Instead, they’re interchanged and it says:

error[E0271]: type mismatch resolving `<MyStruct<&str> as MyTrait>::Item == i32`
  --> src/main.rs:17:5
   |
17 |     func(my_struct);
   |     ^^^^ expected &str, found i32
   |
   = note: expected type `&str`
              found type `i32`
note: required by `func`
  --> src/main.rs:13:1
   |
13 | fn func<T: MyTrait<Item=i32>>(t: T) { }
   | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@estebank
Copy link
Contributor

Current output:

error[E0271]: type mismatch resolving `<MyStruct<&str> as MyTrait>::Item == i32`
  --> src/main.rs:16:5
   |
13 | fn func<T: MyTrait<Item=i32>>(t: T) { }
   |    ----            -------- required by this bound in `func`
...
16 |     func(MyStruct { item: "str" });
   |     ^^^^ expected &str, found i32
   |
   = note: expected type `&str`
              found type `i32`

Still same reversal.

@estebank estebank added the T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. label Oct 10, 2019
@estebank estebank added the D-incorrect Diagnostics: A diagnostic that is giving misleading or incorrect information. label Oct 18, 2019
tmandry added a commit to tmandry/rust that referenced this issue Oct 31, 2019
…-with-an-associated-type, r=estebank

Fix incorrect diagnostics for expected type in E0271 with an associated type

With code like the following code:

```rust
#[derive(Debug)]
struct Data {}

fn do_stuff<'a>(iterator: impl Iterator<Item = &'a Data>) {
    for item in iterator {
        println!("{:?}", item)
    }
}

fn main() {
    let v = vec![Data {}];

    do_stuff(v.into_iter());
}
```

the diagnostic (in nightly & stable) wrongly complains about the expected type:

```
error[E0271]: type mismatch resolving `<std::vec::IntoIter<Data> as std::iter::Iterator>::Item == &Data`
  --> src/main.rs:15:5
   |
5  | fn do_stuff<'a>(iterator: impl Iterator<Item = &'a Data>) {
   |    --------                             --------------- required by this bound in `do_stuff`
...
15 |     do_stuff(v.into_iter());
   |     ^^^^^^^^ expected struct `Data`, found &Data
   |
   = note: expected type `Data`
              found type `&Data`
```

This PR fixes this issue by flipping the expected/actual values where appropriate, so it looks like this:

```
error[E0271]: type mismatch resolving `<std::vec::IntoIter<Data> as std::iter::Iterator>::Item == &Data`
  --> main.rs:15:5
   |
5  | fn do_stuff<'a>(iterator: impl Iterator<Item = &'a Data>) {
   |    --------                             --------------- required by this bound in `do_stuff`
...
15 |     do_stuff(v.into_iter());
   |     ^^^^^^^^ expected &Data, found struct `Data`
   |
   = note: expected type `&Data`
              found type `Data`
```

This improves the output of a lot of existing tests (check out `associated-types-binding-to-type-defined-in-supertrait`!).

The only change which I wasn't too sure about is in the test `associated-types-overridden-binding-2`, but I think it's an improvement and the underlying problem is with handling of `trait_alias`.

Fix rust-lang#57226, fix rust-lang#64760, fix rust-lang#58092.
@bors bors closed this as completed in 959a563 Nov 1, 2019
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 D-incorrect Diagnostics: A diagnostic that is giving misleading or incorrect information. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants