Skip to content

suggestion/fix (use ::item) is incorrect for editions 2018+ #141082

Closed
@japaric

Description

@japaric

Code

// copied from: https://github.com/rust-lang/rust/blob/1b9efcd18f1cb95348c3ffadd4db47bfa15292e5/tests/ui/imports/issue-99695.rs
//@ run-rustfix
#![allow(unused, nonstandard_style)]
mod m {
    #[macro_export]
    macro_rules! nu {
        {} => {};
    }

    pub struct other_item;

    pub use self::{nu, other_item as _};
    //~^ ERROR unresolved import `self::nu` [E0432]
    //~| HELP a macro with this name exists at the root of the crate
}

fn main() {}

Current output

$ rustc --edition=2018 src/main.rs # all editions report the same suggestion
error[E0432]: unresolved import `self::nu`
  --> src/main.rs:12:20
   |
12 |     pub use self::{nu, other_item as _};
   |                    ^^ no `nu` in `m`
   |
   = note: this could be because a macro annotated with `#[macro_export]` will be exported at the root of the crate instead of the module where it is defined
help: a macro with this name exists at the root of the crate
   |
12 ~     use ::nu;
13 ~ pub use self::{other_item as _};
   |

error: aborting due to 1 previous error

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

Desired output

help: a macro with this name exists at the root of the crate
   |
12 ~     use crate::nu;
13 ~ pub use self::{other_item as _};

Rationale and extra context

use ::nu only works in the 2015 edition. use crate::nu works in the 2015 and newer editions

Other cases

Rust Version

$ rustc --version --verbose
rustc 1.89.0-nightly (d97326eab 2025-05-15)
binary: rustc
commit-hash: d97326eabfc3b2c33abcb08d6bc117aefa697cb7
commit-date: 2025-05-15
host: x86_64-unknown-linux-gnu
release: 1.89.0-nightly
LLVM version: 20.1.4

Anything else?

Found this issue while running a compiler UI test on the 2018 edition.

$ ./x test --stage 1 --force-rerun --test-args '--edition 2018' tests/ui/imports/issue-99695.rs
running 1 tests

[ui] tests/ui/imports/issue-99695.rs ... F


failures:

---- [ui] tests/ui/imports/issue-99695.rs stdout ----

error: failed to compile fixed code

As the UI test has the run-rustfix directive, it will use the rustfix crate to apply the suggestions in the compiler output. As the suggestion is wrong for edition 2018+, the output of rustfix does not compile as indicate the output of the x command.

What I found curious there is that the JSON version of the diagnostic indicates that the suggestion "may be incorrect", yet rustfix (as invoked by the compiletest tool) still applies the "maybe incorrect" suggestion.

$ rustc --error-format=json --edition=2018 tests/ui/imports/issue-99695.rs 2>&1 | jq | rg -B2 suggestion
      "label": "no `nu` in `m`",
      "suggested_replacement": null,
      "suggestion_applicability": null,
--
          "label": null,
          "suggested_replacement": "",
          "suggestion_applicability": "MaybeIncorrect",
--
          "label": null,
          "suggested_replacement": "use ::nu;\n",
          "suggestion_applicability": "MaybeIncorrect",

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsA-resolveArea: Name/path resolution done by `rustc_resolve` specificallyD-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