I have a strange problem and I'm not sure what exactly is wrong here. The compiler gives an error at 2_u8 as _ saying that type annotations are needed.
fn main() {
// Delete the following line to get rid of the error
let a: String = serde_yaml::from_str("").unwrap();
if 1_u16 == 2_u8 as _ { // Error: type annotations needed (E0282)
println!("???");
}
}
--> src/main.rs:5:25
|
5 | if 1_u16 == 2_u8 as _ { // Error: type annotations needed (E0282)
| ^ cannot infer type
The strange thing is that if I remove the first line, the error disappears. But I can't see how they are related. This looks like a compiler bug to me but still, I'm not sure. What should be done here?
[package]
name = "project"
authors = ["x"]
version = "0.1.0"
edition = "2021"
[dependencies]
# At least using version 0.9.2 eliminates the error
serde_yaml = "0.8.26"
That is pretty weird. My guess is that mentioning serde_yaml somehow makes it register that crates' PartialEq implementations which then somehow trip up inference (even though that shouldn't effect casting). E.g. maybe one part of the compiler asks another to figure out the type of the RHS of == before considering that the RHS is a cast (which would limit the viable types).