Description
While experimenting with the Rust By Example playground on the Inference section at https://doc.rust-lang.org/stable/rust-by-example/types/inference.html I came across what I believe is an error in the error produced by the compiler.
I do not expect this example to work but the error literal out of range for 'i8'
is reported against line 4 let e2 = 230;
which is a perfectly valid statement. The problem is lower down where two different types are pushed onto the same Vector but possibly due to the way the inference engine works this is being incorrectly reported against line 4. If I comment out the line vec.push(e2);
, the example works fine, apart from a warning: unused variable: 'e2'
.
Code
fn main() {
// Because of the annotation, the compiler knows that `elem` has type i8.
let elem = 6i8;
let e2 = 230;
// Create an empty vector (a growable array).
let mut vec = Vec::new();
// At this point the compiler doesn't know the exact type of `vec`, it
// just knows that it's a vector of something (`Vec<_>`).
// Insert `elem` in the vector.
vec.push(e2);
vec.push(elem);
// Aha! Now the compiler knows that `vec` is a vector of `u8`s (`Vec<u8>`)
// TODO ^ Try commenting out the `vec.push(elem)` line
println!("{:?}", vec);
}
Meta
rustc --version --verbose
: I am unable to determine the exact compiler version in the Rust By Example playground. I have run the same example on my Fedora 32 Workstation machine and with the same result. The rustc version details for this machine are as follows
rustc 1.48.0
binary: rustc
commit-hash: unknown
commit-date: unknown
host: x86_64-unknown-linux-gnu
release: 1.48.0
LLVM version: 10.0
Error output
Compiling playground v0.0.1 (/playground)
error: literal out of range for `i8`
--> src/main.rs:4:14
|
4 | let e2 = 230;
| ^^^
|
= note: `#[deny(overflowing_literals)]` on by default
= note: the literal `230` does not fit into the type `i8` whose range is `-128..=127`
error: aborting due to previous error
error: could not compile `playground`
To learn more, run the command again with --verbose.
Backtrace
Sorry I don't know exactly how to provide this.
<backtrace>