rust-analyzer: Rust detecting most types as "unknown" or outright getting types wrong all of the sudden

rust-analyzer version: rust-analyzer version: 0.0.0 (5342f47f4 2022-07-09)

rustc version: rustc 1.61.0 (fe5b13d68 2022-05-18)

relevant settings: (eg. client settings, or environment variables like CARGO, RUSTUP_HOME or CARGO_HOME): No relevant environment variables, but I do have Rust running inside a Nix environment.

I have issues recently that appeared out of nowhere with Rust-Analyzer suddenly being unable to infer many types, often setting them to unknown or just outright getting them wrong, even in very simple cases. For example, I have a struct that’s being parsed with clap, and originally the type inference worked when I did:

let data = example_struct::Parse()

originally it would correctly infer the type correctly as :example_struct but now it is {unknown}

When creating a new instance of a struct via an asynchronous function in one file it is able to infer the type: let result = result_struct::new().await and will successfully label the type as result_struct but then in a similar file that uses that same identical line of code, it will simply say {unknown}

Or, when calling a random function I made called restore rust-analyzer was originally able to type it successfully as an anyhow result type of Result<()> but now rust-analyzer feels it is of ?Sized impl and even weirder the rust-analyzer will often triple list the types, so rather than it just listing the types once it will repeat it 3 times so it shows as ?Sized impl ?Sized impl ?Sized impl

I thought perhaps this (even though it was initially working) out of nowhere loss in functionality was brought about by me using rust-analyzer with the local installation of Rust rather than the Nix one, but I tried launching VSCode from within the Nix repo, and even tried the nix environment switcher plugin and got the same strange analyzer results. I even tried running cargo clean and that also achieved nothing. Does this issue sound familiar to anyone?

About this issue

  • Original URL
  • State: open
  • Created 2 years ago
  • Comments: 16 (8 by maintainers)

Most upvoted comments

If you’re using the bundled binary (from the Code extension), we already do some patchelf magic to make it work on Nix. But I assume that doesn’t help with the location of rust-src. Maybe you need to install that too somehow.

Anyway, I don’t understand Nix. matklad is using it so it should work given the right setup.

I just added an entry defining rust-src from the rustPkg and that seemed to sort it out. Not sure what initially caused it to freakout after it started working, but it seems its good to go now.