Skip to content

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

Closed
@CAD97

Description

@CAD97

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsD-confusingDiagnostics: Confusing error or lint that should be reworked.D-incorrectDiagnostics: A diagnostic that is giving misleading or incorrect information.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