Skip to content
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

import #[macro_export] from crate root help broken by use trees #99695

Closed
CAD97 opened this issue Jul 24, 2022 · 2 comments · Fixed by #104347
Closed

import #[macro_export] from crate root help broken by use trees #99695

CAD97 opened this issue Jul 24, 2022 · 2 comments · Fixed by #104347
Labels
A-diagnostics Area: Messages for errors, warnings, and lints D-confusing Diagnostics: Confusing error or lint that should be reworked. D-incorrect Diagnostics: A diagnostic that is giving misleading or incorrect information. 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

@CAD97
Copy link
Contributor

CAD97 commented Jul 24, 2022

Given the following code: [playground]

mod m {
    #[macro_export]
    macro_rules! nu {
        {} => {};
    }

    #[allow(nonstandard_style)]
    pub struct other_item;

    pub use self::{nu, other_item as _};
}

The current output is:

error[[E0432]](https://doc.rust-lang.org/nightly/error-index.html#E0432): unresolved import `self::nu`
  --> src/lib.rs:10:20
   |
10 |     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
   |
10 -     pub use self::{nu, other_item as _};
10 +     pub use self::{nu, other_item as _};
   |
If the starting order is reversed, the suggestion puts the macro first in the tree
error[[E0432]](https://doc.rust-lang.org/nightly/error-index.html#E0432): unresolved import `self::nu`
  --> src/lib.rs:10:37
   |
10 |     pub use self::{other_item as _, nu};
   |                                     ^^ 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
   |
10 -     pub use self::{other_item as _, nu};
10 +     pub use self::{nu, other_item as _};
   |

This is suggesting to change the code to itself 🙃 It needs to move the import into a use crate:: tree; the easy way is to make a new use statement (and let rustfmt clean that up); the medium way is to find an existing use crate:: context; the hard way is when a use crate:: tree root doesn't already exist (or isn't immediately a tree at that point) determine what import granularity the code is using and match that.

Related: #99694

@rustbot label +D-confusing +D-incorrect

@CAD97 CAD97 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 Jul 24, 2022
@rustbot rustbot added D-confusing Diagnostics: Confusing error or lint that should be reworked. D-incorrect Diagnostics: A diagnostic that is giving misleading or incorrect information. labels Jul 24, 2022
@CAD97
Copy link
Contributor Author

CAD97 commented Jul 24, 2022

In this case, the ideal solution is actually to write just pub use nu; and get the import from legacy-lexical-macro-scope into the namespace. I originally hit this with just multiple macros in the use tree, and removing the self::{} was the correct fix. I "simplified" for the error report to use a second non-macro item here (to reduce the number of diagnostics).

So bonus challenge: handle this case properly and only emit a single help:

error[[E0432]](https://doc.rust-lang.org/nightly/error-index.html#E0432): unresolved imports `self::nu`, `self::nu_parameters`, `self::nu_parameter`
  --> src/lib.rs:19:20
   |
19 |     pub use self::{nu, nu_parameters, nu_parameter};
   |                    ^^  ^^^^^^^^^^^^^  ^^^^^^^^^^^^ no `nu_parameter` in `m`
   |                    |   |
   |                    |   no `nu_parameters` in `m`
   |                    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
   |
19 -     pub use self::{nu, nu_parameters, nu_parameter};
19 +     pub use self::{nu, nu_parameters, nu_parameter};
   |
help: a macro with this name exists at the root of the crate
   |
19 -     pub use self::{nu, nu_parameters, nu_parameter};
19 +     pub use self::{nu_parameters, nu, nu_parameter};
   |
help: a macro with this name exists at the root of the crate
   |
19 -     pub use self::{nu, nu_parameters, nu_parameter};
19 +     pub use self::{nu_parameter, nu, nu_parameters};
   |

@CAD97
Copy link
Contributor Author

CAD97 commented Jul 24, 2022

@rustbot label +D-invalid-suggestion

It's not producing invalid code, but it's surely suggesting a fix which will cause an error (the identical one).

@rustbot rustbot added the D-invalid-suggestion Diagnostics: A structured suggestion resulting in incorrect code. label Jul 24, 2022
compiler-errors added a commit to compiler-errors/rust that referenced this issue Jan 20, 2023
…rom-self-fixup, r=TaKO8Ki

diagnostics: suggest changing `s@self::{macro}@::macro` for exported

Fixes rust-lang#99695
compiler-errors added a commit to compiler-errors/rust that referenced this issue Jan 21, 2023
…rom-self-fixup, r=TaKO8Ki

diagnostics: suggest changing `s@self::{macro}@::macro` for exported

Fixes rust-lang#99695
@bors bors closed this as completed in e9d8d23 Jan 21, 2023
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 D-confusing Diagnostics: Confusing error or lint that should be reworked. D-incorrect Diagnostics: A diagnostic that is giving misleading or incorrect information. 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