Skip to content

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

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
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
22 changes: 17 additions & 5 deletions compiler/rustc_resolve/src/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2125,9 +2125,15 @@ impl<'a, 'b> ImportResolver<'a, 'b> {

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

// Make sure this is actually crate-relative.
let is_definitely_crate = import
.module_path
.first()
.map_or(false, |f| f.ident.name != kw::SelfLower && f.ident.name != kw::Super);

// Add the import to the start, with a `{` if required.
let start_point = source_map.start_point(after_crate_name);
if let Ok(start_snippet) = source_map.span_to_snippet(start_point) {
if is_definitely_crate && let Ok(start_snippet) = source_map.span_to_snippet(start_point) {
corrections.push((
start_point,
if has_nested {
Expand All @@ -2139,11 +2145,17 @@ impl<'a, 'b> ImportResolver<'a, 'b> {
format!("{{{}, {}", import_snippet, start_snippet)
},
));
}

// Add a `};` to the end if nested, matching the `{` added at the start.
if !has_nested {
corrections.push((source_map.end_point(after_crate_name), "};".to_string()));
// Add a `};` to the end if nested, matching the `{` added at the start.
if !has_nested {
corrections.push((source_map.end_point(after_crate_name), "};".to_string()));
}
} else {
// If the root import is module-relative, add the import separately
corrections.push((
import.use_span.shrink_to_lo(),
format!("use {module_name}::{import_snippet};\n"),
));
}
}

Expand Down
20 changes: 20 additions & 0 deletions tests/ui/imports/issue-99695-b.fixed
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// run-rustfix
#![allow(unused, nonstandard_style)]
mod m {

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

pub struct other_item;
}

use ::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
}

fn main() {}
19 changes: 19 additions & 0 deletions tests/ui/imports/issue-99695-b.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// run-rustfix
#![allow(unused, nonstandard_style)]
mod m {

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

pub struct other_item;
}

pub use self::p::{nu, other_item as _};
//~^ ERROR unresolved import `self::p::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-b.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
error[E0432]: unresolved import `self::p::nu`
--> $DIR/issue-99695-b.rs:14:23
|
LL | pub use self::p::{nu, other_item as _};
| ^^ no `nu` in `m::p`
|
= 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 ~ pub use self::p::{other_item as _};
|

error: aborting due to previous error

For more information about this error, try `rustc --explain E0432`.
17 changes: 17 additions & 0 deletions tests/ui/imports/issue-99695.fixed
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// run-rustfix
#![allow(unused, nonstandard_style)]
mod m {
#[macro_export]
macro_rules! nu {
{} => {};
}

pub struct other_item;

use ::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.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// 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() {}
16 changes: 16 additions & 0 deletions tests/ui/imports/issue-99695.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:11: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 ~ pub use self::{other_item as _};
|

error: aborting due to previous error

For more information about this error, try `rustc --explain E0432`.