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

Type inference goes in the wrong direction #4618

Closed
Vlad-Shcherbina opened this issue May 26, 2020 · 2 comments
Closed

Type inference goes in the wrong direction #4618

Vlad-Shcherbina opened this issue May 26, 2020 · 2 comments

Comments

@Vlad-Shcherbina
Copy link
Contributor

Vlad-Shcherbina commented May 26, 2020

To reproduce

  1. Open the following program in VSCode:
    // [dependencies]
    // rand = "0.7"
    
    fn main() {
        use rand::seq::SliceRandom;
        let (x, y) = [(1, 2)].choose(&mut rand::thread_rng()).unwrap();
        let x: i32 = x;
    }
  2. Mouse hover over x in let (x, y) = ....

Expected result

The inferred type should be &i32.
Alternatively, if we accept Chalk limitations or whatever this is, {unknown}.

Actual result

The inferred type is i32.
This makes the error in the next line confusing.

By the way, the inferred type of y is {unknown}.
Removing the next line turns the inferred type of x into {unknown} as well.

image

rust-analyzer version: 2020-05-25 (fbb8b88)
@lnicola
Copy link
Member

lnicola commented May 26, 2020

Duplicate of #1165.

image

Inferring x as i32 is normal, the compiler does the same thing every time you write Vec::new().

@lnicola lnicola closed this as completed May 26, 2020
@flodiebold
Copy link
Member

Type inference after type errors is best-effort; we infer x as i32 from the subsequent usage to still give you useful completions.

So yeah, I'd also consider this a duplicate.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants