rust-analyzer icon indicating copy to clipboard operation
rust-analyzer copied to clipboard

Misleading diagnostic for E0308 when short name `Error` shadows `std::io::Error` with imported `std::error::Error` trait

Open HinataDeveloper opened this issue 2 months ago • 1 comments

Summary

When an unresolved identifier Error is auto-imported/resolved as std::error::Error (a trait) in a type annotation expecting a concrete struct like std::io::Error, the resulting E0308 type mismatch diagnostic prints identical short names (Error), omitting fully-qualified path disambiguation. This causes a misleading diagnostic suggesting a trait object lifetime/indirection mismatch rather than a concrete type mismatch.

Minimal Reproducible Example (MRE)

use std::error::Error; // Imported trait instead of std::io::Error
use std::fs::{self, DirEntry};

fn main() {
let read_dir = fs::read_dir(".").unwrap();
let file_coll: Vec<_> = read_dir.collect();

// Type annotation using bare trait `Error` instead of `std::io::Error`
let one: &Result<DirEntry, Error> = &file_coll[0];
}

Current Behavior / Diagnostic Output
The error message emitted is:
expected `&Result<DirEntry, dyn Error + 'static>`, found `&Result<DirEntry, Error>` (E0308)

Because both types are stripped to the short name Error, the diagnostic strongly implies that the found type is an instance of the trait itself (missing dynamic dispatch/indirection), rather than making it obvious that:

    The expected type is dyn std::error::Error + 'static (bare trait treated as trait object)
    The found type is the concrete struct std::io::Error

Expected Behavior
The diagnostic should disambiguate identical or shadowing type names using fully-qualified paths:

expected `&Result<DirEntry, dyn std::error::Error + 'static>`
   found `&Result<DirEntry, std::io::Error>`

Environment

    rustc version: rustc 1.100.0-nightly (a69a63265 2026-09-03)
    cargo version: cargo 1.100.0-nightly (b2e9d5f9d 2026-09-02)
    LLVM version: 23.1.1
    OS: Fedora 44 (64-bit) / Linux Kernel 7.1.13-200.fc44.x86_64
    Host: x86_64-unknown-linux-gnu

HinataDeveloper avatar Sep 05 '26 02:09 HinataDeveloper

The dyn is enough of a hint for an experienced Rustacean, although I agree the diagnostic could be improved. There's also an inlay hint for dyn.

We do fully-qualify when the names are identical but this is based on textual equality, which misses here because of the dyn.

ChayimFriedman2 avatar Sep 05 '26 17:09 ChayimFriedman2