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

Rust analyzer false positive error #18133

Open
benluelo opened this issue Sep 17, 2024 · 10 comments
Open

Rust analyzer false positive error #18133

benluelo opened this issue Sep 17, 2024 · 10 comments
Assignees
Labels
A-chalk chalk related issue A-diagnostics diagnostics / error reporting A-ty type system / type inference / traits / method resolution C-bug Category: bug

Comments

@benluelo
Copy link

$ rust-analyzer --version
rust-analyzer 1.83.0-nightly (c52c23b6 2024-09-16)

$ rustc -V --verbose
rustc 1.83.0-nightly (c52c23b6f 2024-09-16)
binary: rustc
commit-hash: c52c23b6f44cd19718721a5e3b2eeb169e9c96ff
commit-date: 2024-09-16
host: aarch64-unknown-linux-gnu
release: 1.83.0-nightly
LLVM version: 19.1.0
pub enum Enum<T: Foo> {
    String(String),
    ImplsBar(T::UsedInEnum),
}

impl<T: Foo> Enum<T> {
    pub fn f(&self) {
        match *self {
            Enum::String(ref string) => dyn_bar(&string),
            Enum::ImplsBar(ref a1) => dyn_bar(&a1),
        };
    }
}

pub trait Foo {
    type UsedInEnum: Bar;
    type Unused1: Quux;
    type Unused2: Quux;
}

pub trait Quux {
    type Unused3: Bar;
}

pub trait Bar {}

impl Bar for String {}

impl<T: Bar> Bar for &'_ T {}

pub fn dyn_bar(_: &dyn Bar) {}

cargo check works fine, however rust-analyzer errors:

$ rust-analyzer diagnostics .
processing crate: playground, module: /redacted/src/main.rs
Error RustcHardError("E0308") from LineCol { line: 10, col: 48 } to LineCol { line: 10, col: 55 }: expected &dyn Bar, found &&String
Error RustcHardError("E0308") from LineCol { line: 11, col: 46 } to LineCol { line: 11, col: 49 }: expected &dyn Bar, found &&<T as Foo>::UsedInEnum

diagnostic scan complete

Error: diagnostic error detected

Stack backtrace:
   0: std::backtrace_rs::backtrace::libunwind::trace
             at /rustc/c52c23b6f44cd19718721a5e3b2eeb169e9c96ff/library/std/src/../../backtrace/src/backtrace/libunwind.rs:116:5
   1: std::backtrace_rs::backtrace::trace_unsynchronized
             at /rustc/c52c23b6f44cd19718721a5e3b2eeb169e9c96ff/library/std/src/../../backtrace/src/backtrace/mod.rs:66:5
   2: std::backtrace::Backtrace::create
             at /rustc/c52c23b6f44cd19718721a5e3b2eeb169e9c96ff/library/std/src/backtrace.rs:331:13
   3: <anyhow::Error>::msg::<&str>
   4: std::sys::backtrace::__rust_begin_short_backtrace::<<stdx::thread::Builder>::spawn<<rust_analyzer::cli::flags::Diagnostics>::run::{closure#0}, core::result::Result<(), anyhow::Error>>::{closure#0}, core::result::Result<(), anyhow::Error>>
   5: <<std::thread::Builder>::spawn_unchecked_<<stdx::thread::Builder>::spawn<<rust_analyzer::cli::flags::Diagnostics>::run::{closure#0}, core::result::Result<(), anyhow::Error>>::{closure#0}, core::result::Result<(), anyhow::Error>>::{closure#1} as core::ops::function::FnOnce<()>>::call_once::{shim:vtable#0}
   6: <alloc::boxed::Box<F,A> as core::ops::function::FnOnce<Args>>::call_once
             at /rustc/c52c23b6f44cd19718721a5e3b2eeb169e9c96ff/library/alloc/src/boxed.rs:2453:9
   7: <alloc::boxed::Box<F,A> as core::ops::function::FnOnce<Args>>::call_once
             at /rustc/c52c23b6f44cd19718721a5e3b2eeb169e9c96ff/library/alloc/src/boxed.rs:2453:9
   8: std::sys::pal::unix::thread::Thread::new::thread_start
             at /rustc/c52c23b6f44cd19718721a5e3b2eeb169e9c96ff/library/std/src/sys/pal/unix/thread.rs:105:17
   9: start_thread
  10: thread_start

Notably both String and T::UsedInEnum error. However! If any of the Unused* associated types are commented out, it compiles.

@benluelo benluelo added the C-bug Category: bug label Sep 17, 2024
@ShoyuVanilla ShoyuVanilla added A-ty type system / type inference / traits / method resolution A-diagnostics diagnostics / error reporting labels Sep 18, 2024
@ShoyuVanilla
Copy link
Member

I've tried a bit but this seems to be a chalk issue 😢

@ShoyuVanilla ShoyuVanilla added the A-chalk chalk related issue label Sep 18, 2024
@benluelo
Copy link
Author

I've bisected this to:

rust-analyzer 1.82.0-nightly (0d634185 2024-08-29) -> works
rust-analyzer 1.82.0-nightly (a7399ba6 2024-08-31) -> doesn't work

@ShoyuVanilla
Copy link
Member

I've bisected this to:

rust-analyzer 1.82.0-nightly (0d634185 2024-08-29) -> works rust-analyzer 1.82.0-nightly (a7399ba6 2024-08-31) -> doesn't work

Oh, that's cool. This might not be a chalk issue then. I'll look into this more. Thanks 😄

@ShoyuVanilla
Copy link
Member

My bisection result is a bit different

rust-analyzer version: 0.4.2075-standalone (0765978 2024-08-16) -> Makes those false positives
rust-analyzer version: 0.4.2074-standalone (91aa3f4 2024-08-15) -> No false positives

@benluelo
Copy link
Author

I use https://github.com/oxalica/rust-overlay - I'm unable to get those same commits as you seemingly:

rust-analyzer 1.82.0-nightly (2c93fabd 2024-08-15)
rust-analyzer 1.82.0-nightly (506052d4 2024-08-16)

(both of these work for me also)

benluelo added a commit to unionlabs/union that referenced this issue Sep 18, 2024
Bump rust version to latest nightly.

- error in core stabilized
- min exhaustive patterns stabilized
- some new clippy lints
- rust-overlay internal changes (we were using some unstable attributes
previously)

NOTE: Slight change to our custom Debug derive macro due to
rust-lang/rust-analyzer#18133
@ShoyuVanilla
Copy link
Member

Thanks!
I've found the most suspicious single commit.
Will work on this tommorrow but not sure if this could be fixed soon 😅

@ShoyuVanilla
Copy link
Member

It seems that nix rust overlay prints versions from its own commit graphs btw

@ShoyuVanilla
Copy link
Member

ShoyuVanilla commented Sep 19, 2024

Bisected to 8a4261a

I guess that maybe the chalk is not so happy with the partialy implemented lifetimes in the above commit.

For instance, chalk solves this full explicit lifetime modification of the original code well and emits no false-positive diagnostics

pub enum Enum<T: Foo> {
    String(String),
    ImplsBar(T::UsedInEnum),
}

impl<T: Foo> Enum<T> {
    pub fn f<'a>(self: &'a Self) {
        match *self {
            Enum::String(ref string) => dyn_bar(&string),
            Enum::ImplsBar(ref a1) => dyn_bar(&a1),
        };
    }
}

pub trait Foo {
    type UsedInEnum: Bar;
    type Unused1: Quux;
    type Unused2: Quux;
}

pub trait Quux {
    type Unused3: Bar;
}

pub trait Bar {}

impl Bar for String {}

impl<'a, T: Bar> Bar for &'a T {}

pub fn dyn_bar<'a>(_: &'a dyn Bar) {}

Maybe I should implement lifetime elision inference, which r-a currently doesn't have.

@ShoyuVanilla
Copy link
Member

As @dfireBird is working on lifetime elision, this seems to be resolved once his work is done. If this isn't fixed then, I'll look into this more.

@Veykril
Copy link
Member

Veykril commented Sep 24, 2024

Likely the same issue as #18064

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-chalk chalk related issue A-diagnostics diagnostics / error reporting A-ty type system / type inference / traits / method resolution C-bug Category: bug
Projects
None yet
Development

No branches or pull requests

3 participants