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);
}
rustc --version --verbose
rustc 1.11.0 (9b21dcd6a 2016-08-15)
binary: rustc
commit-hash: 9b21dcd6a89f38e8ceccb2ede8c9027cb409f6e3
commit-date: 2016-08-15
host: x86_64-pc-windows-gnu
release: 1.11.0
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]>`
(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.
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
Hum, that might deserve another update once my current PR is merged.
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.)
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)
Most helpful comment
(incoming tangential comment)
Regarding:
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.