Skip to content

Unusable path suggestion for item nested in function #146786

@notriddle

Description

@notriddle

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsA-suggestion-diagnosticsArea: Suggestions generated by the compiler applied by `cargo fix`D-diagnostic-infraDiagnostics: Issues that affect all diagnostics, or relate to the diagnostic machinery itself.D-invalid-suggestionDiagnostics: A structured suggestion resulting in incorrect code.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions