Skip to content

Commit bf75f81

Browse files
Rollup merge of #104347 - notriddle:notriddle/import-macro-from-self-fixup, r=TaKO8Ki
diagnostics: suggest changing `s@self::{macro}@::macro` for exported Fixes #99695
2 parents e640069 + c07a722 commit bf75f81

File tree

7 files changed

+121
-5
lines changed

7 files changed

+121
-5
lines changed

compiler/rustc_resolve/src/diagnostics.rs

+17-5
Original file line numberDiff line numberDiff line change
@@ -2125,9 +2125,15 @@ impl<'a, 'b> ImportResolver<'a, 'b> {
21252125

21262126
let source_map = self.r.session.source_map();
21272127

2128+
// Make sure this is actually crate-relative.
2129+
let is_definitely_crate = import
2130+
.module_path
2131+
.first()
2132+
.map_or(false, |f| f.ident.name != kw::SelfLower && f.ident.name != kw::Super);
2133+
21282134
// Add the import to the start, with a `{` if required.
21292135
let start_point = source_map.start_point(after_crate_name);
2130-
if let Ok(start_snippet) = source_map.span_to_snippet(start_point) {
2136+
if is_definitely_crate && let Ok(start_snippet) = source_map.span_to_snippet(start_point) {
21312137
corrections.push((
21322138
start_point,
21332139
if has_nested {
@@ -2139,11 +2145,17 @@ impl<'a, 'b> ImportResolver<'a, 'b> {
21392145
format!("{{{}, {}", import_snippet, start_snippet)
21402146
},
21412147
));
2142-
}
21432148

2144-
// Add a `};` to the end if nested, matching the `{` added at the start.
2145-
if !has_nested {
2146-
corrections.push((source_map.end_point(after_crate_name), "};".to_string()));
2149+
// Add a `};` to the end if nested, matching the `{` added at the start.
2150+
if !has_nested {
2151+
corrections.push((source_map.end_point(after_crate_name), "};".to_string()));
2152+
}
2153+
} else {
2154+
// If the root import is module-relative, add the import separately
2155+
corrections.push((
2156+
import.use_span.shrink_to_lo(),
2157+
format!("use {module_name}::{import_snippet};\n"),
2158+
));
21472159
}
21482160
}
21492161

tests/ui/imports/issue-99695-b.fixed

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// run-rustfix
2+
#![allow(unused, nonstandard_style)]
3+
mod m {
4+
5+
mod p {
6+
#[macro_export]
7+
macro_rules! nu {
8+
{} => {};
9+
}
10+
11+
pub struct other_item;
12+
}
13+
14+
use ::nu;
15+
pub use self::p::{other_item as _};
16+
//~^ ERROR unresolved import `self::p::nu` [E0432]
17+
//~| HELP a macro with this name exists at the root of the crate
18+
}
19+
20+
fn main() {}

tests/ui/imports/issue-99695-b.rs

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// run-rustfix
2+
#![allow(unused, nonstandard_style)]
3+
mod m {
4+
5+
mod p {
6+
#[macro_export]
7+
macro_rules! nu {
8+
{} => {};
9+
}
10+
11+
pub struct other_item;
12+
}
13+
14+
pub use self::p::{nu, other_item as _};
15+
//~^ ERROR unresolved import `self::p::nu` [E0432]
16+
//~| HELP a macro with this name exists at the root of the crate
17+
}
18+
19+
fn main() {}

tests/ui/imports/issue-99695-b.stderr

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
error[E0432]: unresolved import `self::p::nu`
2+
--> $DIR/issue-99695-b.rs:14:23
3+
|
4+
LL | pub use self::p::{nu, other_item as _};
5+
| ^^ no `nu` in `m::p`
6+
|
7+
= 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
8+
help: a macro with this name exists at the root of the crate
9+
|
10+
LL ~ use ::nu;
11+
LL ~ pub use self::p::{other_item as _};
12+
|
13+
14+
error: aborting due to previous error
15+
16+
For more information about this error, try `rustc --explain E0432`.

tests/ui/imports/issue-99695.fixed

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// run-rustfix
2+
#![allow(unused, nonstandard_style)]
3+
mod m {
4+
#[macro_export]
5+
macro_rules! nu {
6+
{} => {};
7+
}
8+
9+
pub struct other_item;
10+
11+
use ::nu;
12+
pub use self::{other_item as _};
13+
//~^ ERROR unresolved import `self::nu` [E0432]
14+
//~| HELP a macro with this name exists at the root of the crate
15+
}
16+
17+
fn main() {}

tests/ui/imports/issue-99695.rs

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// run-rustfix
2+
#![allow(unused, nonstandard_style)]
3+
mod m {
4+
#[macro_export]
5+
macro_rules! nu {
6+
{} => {};
7+
}
8+
9+
pub struct other_item;
10+
11+
pub use self::{nu, other_item as _};
12+
//~^ ERROR unresolved import `self::nu` [E0432]
13+
//~| HELP a macro with this name exists at the root of the crate
14+
}
15+
16+
fn main() {}

tests/ui/imports/issue-99695.stderr

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
error[E0432]: unresolved import `self::nu`
2+
--> $DIR/issue-99695.rs:11:20
3+
|
4+
LL | pub use self::{nu, other_item as _};
5+
| ^^ no `nu` in `m`
6+
|
7+
= 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
8+
help: a macro with this name exists at the root of the crate
9+
|
10+
LL ~ use ::nu;
11+
LL ~ pub use self::{other_item as _};
12+
|
13+
14+
error: aborting due to previous error
15+
16+
For more information about this error, try `rustc --explain E0432`.

0 commit comments

Comments
 (0)