Skip to content

Use crate:: prefix for root macro suggestions #141132

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

Merged
merged 1 commit into from
May 18, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion compiler/rustc_resolve/src/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2493,7 +2493,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
let Res::Def(DefKind::Macro(MacroKind::Bang), _) = binding.res() else {
return None;
};
let module_name = crate_module.kind.name().unwrap_or(kw::Empty);
let module_name = crate_module.kind.name().unwrap_or(kw::Crate);
let import_snippet = match import.kind {
ImportKind::Single { source, target, .. } if source != target => {
format!("{source} as {target}")
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/imports/issue-99695-b.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ mod m {
pub struct other_item;
}

use ::nu;
use crate::nu;
pub use self::p::{other_item as _};
//~^ ERROR unresolved import `self::p::nu` [E0432]
//~| HELP a macro with this name exists at the root of the crate
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/imports/issue-99695-b.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ LL | pub use self::p::{nu, other_item as _};
= 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
|
LL ~ use ::nu;
LL ~ use crate::nu;
LL ~ pub use self::p::{other_item as _};
|

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
//@ run-rustfix
//@ revisions: edition_2015 edition_2018
//@ [edition_2015] edition: 2015
//@ [edition_2018] edition: 2018

#![allow(unused, nonstandard_style)]
mod m {
#[macro_export]
Expand All @@ -8,7 +12,7 @@ mod m {

pub struct other_item;

use ::nu;
use crate::nu;
pub use self::{other_item as _};
//~^ ERROR unresolved import `self::nu` [E0432]
//~| HELP a macro with this name exists at the root of the crate
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
error[E0432]: unresolved import `self::nu`
--> $DIR/issue-99695.rs:11:20
--> $DIR/issue-99695.rs:15:20
|
LL | 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
|
LL ~ use ::nu;
LL ~ use crate::nu;
LL ~ pub use self::{other_item as _};
|

Expand Down
21 changes: 21 additions & 0 deletions tests/ui/imports/issue-99695.edition_2018.fixed
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
//@ run-rustfix
//@ revisions: edition_2015 edition_2018
//@ [edition_2015] edition: 2015
//@ [edition_2018] edition: 2018

#![allow(unused, nonstandard_style)]
mod m {
#[macro_export]
macro_rules! nu {
{} => {};
}

pub struct other_item;

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

fn main() {}
16 changes: 16 additions & 0 deletions tests/ui/imports/issue-99695.edition_2018.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
error[E0432]: unresolved import `self::nu`
--> $DIR/issue-99695.rs:15:20
|
LL | 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
|
LL ~ use crate::nu;
LL ~ pub use self::{other_item as _};
|

error: aborting due to 1 previous error

For more information about this error, try `rustc --explain E0432`.
4 changes: 4 additions & 0 deletions tests/ui/imports/issue-99695.rs
Copy link
Member

@jieyouxu jieyouxu May 17, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggestion: while you're modifying this test, can you please rename it to sth more meaningful? We can still keep a backlink to the issue in a doc comment.

Actually nvm, this is in the queue already. I can do that as a follow-up.

Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
//@ run-rustfix
//@ revisions: edition_2015 edition_2018
//@ [edition_2015] edition: 2015
//@ [edition_2018] edition: 2018

#![allow(unused, nonstandard_style)]
mod m {
#[macro_export]
Expand Down
Loading