rust-lang/rust

Unusable path suggestion for item nested in function

Opened this issue · 1 comments

Code

fn main() {
    struct Error; // name must be non-unique in project
    const ERROR = Error;
}

Current output

error: missing type for `const` item
 --> test.rs:3:16
  |
3 |     const ERROR = Error;
  |                ^ help: provide a type for the constant: `: main::Error`

error: aborting due to 1 previous error

Desired output

error: missing type for `const` item
 --> test.rs:3:16
  |
3 |     const ERROR = Error;
  |                ^ help: provide a type for the constant: `: Error`

error: aborting due to 1 previous error

Rationale and extra context

If the suggestion is applied, it doesn't work, because main::Error isn't a real path.

error[E0433]: failed to resolve: function `main` is not a crate or module
 --> test.rs:3:18
  |
3 |     const ERROR: main::Error = Error;
  |                  ^^^^ function `main` is not a crate or module

error: aborting due to 1 previous error

For more information about this error, try `rustc --explain E0433`.

Other cases

I figured out how to reproduce this problem with plain rustc while investigating rust-lang/rust-clippy#15221

Rust Version

rustc 1.89.0-nightly (be19eda0d 2025-06-22)
binary: rustc
commit-hash: be19eda0dc4c22c5cf5f1b48fd163acf9bd4b0a6
commit-date: 2025-06-22
host: x86_64-unknown-linux-gnu
release: 1.89.0-nightly
LLVM version: 20.1.7

Anything else?

Since this error and rust-lang/rust-clippy#15221 both produce similar problems, it seems like this should be fixed in the path-printer, not just the individual diagnostic found here.

I thought Ty::is_suggestable would help here, but apparently it doesn't catch such meaningless paths