rust-lang / rust-analyzer

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

spurious type-mismatches #14924

Open rbtcollins opened 1 year ago

rbtcollins commented 1 year ago

rust-analyzer version: rust-analyzer version: 0.3.1506-standalone (833d5301d 2023-05-07)

rustc version: rustc 1.70.0-beta.6 (2687f47c4 2023-05-26)

relevant settings:

The following code is returning type mismatches, yet cargo build and cargo clippy are happy.

 let host_triple = if let Some(trip) = opts.default_host_triple.as_ref() {
        trip.to_owned()
    } else {
        TargetTriple::from_host_or_build().to_string()
    };

image

The reported error is expected &String, found String

You can see from the screenshot that it thinks host_triple is an &String, but it is actually a String. If I explicitly make it a string: let host_triple:String = ... then the error goes away.

I'm also getting another spurious type-mismatch here:

    pub fn fg(&mut self, color: Color) -> io::Result<()> {
        match self.inner.lock().unwrap().deref_mut() {
            TerminalInner::StandardStream(s, spec) => {
                spec.set_fg(Some(color));
                s.set_color(spec) <--- mismatch here
            }
            #[cfg(feature = "test")]
            TerminalInner::TestWriter(_) => Ok(()),
        }
    }

image

You can see from the embeds that RA thinks spec is a ColorSpec, but actually because of the deref_mut(), it is an &ColorSpec, and thus the error message expected &ColorSpec, found ColorSpec is nonsense.

lowr commented 1 year ago

Would you share the whole code base that this issue is occurring, or if it's not possible, can you try creating an MRE? It's pretty hard to track down the root cause of type mismatches without knowing all the types/impls/traits in scope, etc. Thanks!

lnicola commented 1 year ago

First one is from rustup, but I can't reproduce because it's gated for Windows. Second looks like a feature branch, but I couldn't spot it.

rbtcollins commented 1 year ago

Second one is from my branch https://github.com/rbtcollins/rustup.rs/tree/termcolor-tweaked