Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
japaric opened this issue May 16, 2025 · 0 comments · May be fixed by #141132
Open

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

japaric opened this issue May 16, 2025 · 0 comments · May be fixed by #141132
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-resolve Area: Name/path resolution done by `rustc_resolve` specifically D-invalid-suggestion Diagnostics: A structured suggestion resulting in incorrect code. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@japaric
Copy link
Member

japaric commented May 16, 2025

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",
@japaric japaric added A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels May 16, 2025
@fmease fmease added A-resolve Area: Name/path resolution done by `rustc_resolve` specifically D-invalid-suggestion Diagnostics: A structured suggestion resulting in incorrect code. labels May 16, 2025
@bvanjoi bvanjoi linked a pull request May 17, 2025 that will close this issue
fmease added a commit to fmease/rust that referenced this issue May 18, 2025
Use `crate::` prefix for root macro suggestions

Fixes rust-lang#141082

I've changed the prefix to `crate` directly, since it's compatible across all editions.
fmease added a commit to fmease/rust that referenced this issue May 18, 2025
Use `crate::` prefix for root macro suggestions

Fixes rust-lang#141082

I've changed the prefix to `crate` directly, since it's compatible across all editions.
fmease added a commit to fmease/rust that referenced this issue May 18, 2025
Use `crate::` prefix for root macro suggestions

Fixes rust-lang#141082

I've changed the prefix to `crate` directly, since it's compatible across all editions.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-resolve Area: Name/path resolution done by `rustc_resolve` specifically D-invalid-suggestion Diagnostics: A structured suggestion resulting in incorrect code. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants