Skip to content

Rust analyzer false positive error #18133

Open
@benluelo

Description

@benluelo
$ 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.

Metadata

Metadata

Assignees

Labels

A-chalkchalk related issueA-diagnosticsdiagnostics / error reportingA-tytype system / type inference / traits / method resolutionC-bugCategory: bug

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions