rust-lang / rust-analyzer

A Rust compiler front-end for IDEs
https://rust-analyzer.github.io/
Apache License 2.0
14.12k stars 1.58k forks source link

Inference failure since slice patterns does not serve as type expections #16609

Open oxalica opened 7 months ago

oxalica commented 7 months ago

Types of a and b in this code below cannot be inferred by rust-analyzer, but it compiles fine with rustc.

// edition = "2021"
fn main() {
    let [a, b] = vec![1u32, 2u32].try_into().unwrap();
    //   ^ {unknown}
}

rust-analyzer version: 2024-02-19

rustc version: rustc 1.76.0 (07dca489a 2024-02-04)

relevant settings: N/A

Veykril commented 7 months ago

Hmm, vec![1u32, 2u32].try_into() has type Result<[{unknown}], <Vec<u32, Global> as TryInto<[{unknown}]>>::Error> here, adding the type ascription let [a, b]: [u32; 2] = vec![1u32, 2u32].try_into().unwrap(); makes us resolve the try_into result correctly though

oxalica commented 7 months ago

adding the type ascription

Yes. But rustc can infer that without explicit type ascription.

Note: specifically, this feature is added since 1.73, in https://github.com/rust-lang/rust/pull/113199

Veykril commented 7 months ago

I am aware, I was just trying to reduce the root problem. Oh thanks for that link, would make sense that we are missing that if its such a recent inference feature.