"Use of undeclared type or module" doesn't detect accidental use of variable

2420aef
Opened by Oli Scherer at 2025-03-03 20:18:16
trait Foo {
    type Bar;
    fn bar(&self) -> Self::Bar;
}

impl Foo for i32 {
    type Bar = i32;
    fn bar(&self) -> i32 {
        *self
    }
}

fn main() {
    let x = 5i32;
    let y = x::Bar;
}

reports

error[E0433]: failed to resolve. Use of undeclared type or module `x`
  --> src/main.rs:15:13
   |
15 |     let y = x::Bar;
   |             ^ Use of undeclared type or module `x`

Which is correct, but not helpful.

Imo this should at minimum tell the programmer that you cannot statically access elements of the variable x. At best it it suggests to call a method that yields a type that's reachable via typeof(x)::Bar.

  1. Current output:

    error[E0433]: failed to resolve: use of unresolved module or unlinked crate `x`
      --> src/main.rs:15:13
       |
    15 |     let y = x::Bar;
       |             ^ use of unresolved module or unlinked crate `x`
       |
       = help: if you wanted to use a crate named `x`, use `cargo add x` to add it to your `Cargo.toml`
    

    It should add "a binding named x was declared here, but bindings can't be part of type paths" or something like that (that doesn't muddle the waters with const params).

    Esteban Kuber at 2025-03-03 20:18:16