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

Two-level type inference throws unhelpful E0308 #37632

Closed
friendzis opened this issue Nov 7, 2016 · 6 comments
Closed

Two-level type inference throws unhelpful E0308 #37632

friendzis opened this issue Nov 7, 2016 · 6 comments
Labels
A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@friendzis
Copy link

I have tried two level type inference from vector literal and got totally unhelpful E0308. While I would expect this to work, I file this report due to unhelpfulness of the error.
I expected vec2 alocation to succeed or complain about inability to to coerce types.
Actual error:

src\main.rs:14:25: 14:26 error: mismatched types [E0308]
src\main.rs:14     let vec2 = Baz(vec![1, 2, 3]);
                                       ^
src\main.rs:14:20: 14:33 note: in this expansion of vec! (defined in <std macros>)
src\main.rs:14:25: 14:26 help: run `rustc --explain E0308` to see a detailed explanation
src\main.rs:14:25: 14:26 note: expected type `_`
src\main.rs:14:25: 14:26 note:    found type `_`
<std macros>:3:25: 3:46 error: mismatched types [E0308]
<std macros>:3 < [ _ ] > :: into_vec ( box [ $ ( $ x ) , * ] ) ) ; ( $ ( $ x : expr , ) * )
                                       ^~~~~~~~~~~~~~~~~~~~~
src\main.rs:14:20: 14:33 note: in this expansion of vec! (defined in <std macros>)
<std macros>:3:25: 3:46 help: run `rustc --explain E0308` to see a detailed explanation
<std macros>:3:25: 3:46 note: expected type `Box<[Foo]>`
<std macros>:3:25: 3:46 note:    found type `Box<[_; 3]>`
error: aborting due to 2 previous errors
error: Could not compile `s1c1`.

To learn more, run the command again with --verbose.

"cargo build" completed with code 101
It took approximately 0.379 seconds

Code sample

#[derive(Debug)]
struct Foo(u8);

#[derive(Debug)]
struct Bar(Vec<u8>);

#[derive(Debug)]
struct Baz(Vec<Foo>);

fn main() {
    let vec1 = Bar(vec!(1, 2, 3));
    println!("{:?}", vec1);

    let vec2 = Baz(vec!(1, 2, 3));  // Offending line
    println!("{:?}", vec2);

    let vec3 = Baz(vec!(Foo(1), Foo(2), Foo(3)));
    println!("{:?}", vec3);
}

Meta

rustc --version --verbose
rustc 1.11.0 (9b21dcd 2016-08-15)
binary: rustc
commit-hash: 9b21dcd
commit-date: 2016-08-15
host: x86_64-pc-windows-gnu
release: 1.11.0

@friendzis friendzis added the A-diagnostics Area: Messages for errors, warnings, and lints label Nov 7, 2016
@steveklabnik
Copy link
Member

To me, the

src\main.rs:14:25: 14:26 note: expected type `_`
src\main.rs:14:25: 14:26 note:    found type `_`

line is the worst part, though at least it does give the real error here:

<std macros>:3:25: 3:46 note: expected type `Box<[Foo]>`
<std macros>:3:25: 3:46 note:    found type `Box<[_; 3]>`

@frewsxcv
Copy link
Member

frewsxcv commented Nov 7, 2016

(incoming tangential comment)

Regarding:

src\main.rs:14:25: 14:26 note: expected type `_`
src\main.rs:14:25: 14:26 note:    found type `_`

Maybe we should add a debug assertion that both of the types in this error message are not equal. I don't see a valid scenario where we should display the same type on both lines.

@killercup
Copy link
Member

killercup commented Nov 8, 2016

cc @GuillaumeGomez because of #37388

FYI, error with recent nightly is (see playground):

rustc 1.14.0-nightly (cae6ab1c4 2016-11-05)
error[E0308]: mismatched types
  --> <anon>:14:25
   |
14 |     let vec2 = Baz(vec!(1, 2, 3));  // Offending line
   |                         ^ expected struct `Foo`, found integral variable
   |
   = note: expected type `_`
   = note:    found type `{integer}`

error[E0308]: mismatched types
  --> <anon>:14:20
   |
14 |     let vec2 = Baz(vec!(1, 2, 3));  // Offending line
   |                    ^^^^^^^^^^^^^ expected slice, found array of 3 elements
   |
   = note: expected type `Box<[Foo]>`
   = note:    found type `Box<[{integer}; 3]>`
   = note: this error originates in a macro outside of the current crate

error: aborting due to 2 previous errors

@GuillaumeGomez
Copy link
Member

Hum, that might deserve another update once my current PR is merged.

@killercup
Copy link
Member

14 |     let vec2 = Baz(vec!(1, 2, 3));  // Offending line
   |                         ^ expected struct `Foo`, found integral variable

Maybe change the "variable" here to "value" (or "literal") if it's, you know, a literal value? (Should probably be a new issue if it doesn't exist yet.)

@steveklabnik steveklabnik added the T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. label Mar 9, 2017
@Mark-Simulacrum
Copy link
Member

Closing as fixed, I think the current error represents the problem well:

error[E0308]: mismatched types
  --> test.rs:14:25
   |
14 |     let vec2 = Baz(vec!(1, 2, 3));  // Offending line
   |                         ^ expected struct `Foo`, found integral variable
   |
   = note: expected type `Foo`
              found type `{integer}`

error: aborting due to previous error(s)

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

6 participants