about summary refs log tree commit diff
diff options
context:
space:
mode:
authorMichael Howell <michael@notriddle.com>2022-11-12 22:08:07 -0700
committerMichael Howell <michael@notriddle.com>2023-01-20 14:53:33 -0700
commite237690a28f0d38ab478616fe4b247fb6eb8013f (patch)
tree6375efdefc65f144f8157d12b390b4f823c9dbe2
parente9d8d238ef76c7991b316bbfbd9f857d84bd39cf (diff)
downloadrust-e237690a28f0d38ab478616fe4b247fb6eb8013f.tar.gz
rust-e237690a28f0d38ab478616fe4b247fb6eb8013f.zip
diagnostics: add `};` only if `{` was added too
-rw-r--r--compiler/rustc_resolve/src/diagnostics.rs10
-rw-r--r--tests/ui/imports/issue-99695-b.fixed20
-rw-r--r--tests/ui/imports/issue-99695-b.rs19
-rw-r--r--tests/ui/imports/issue-99695-b.stderr16
4 files changed, 60 insertions, 5 deletions
diff --git a/compiler/rustc_resolve/src/diagnostics.rs b/compiler/rustc_resolve/src/diagnostics.rs
index 2a5cc288380..af4d7a8eaff 100644
--- a/compiler/rustc_resolve/src/diagnostics.rs
+++ b/compiler/rustc_resolve/src/diagnostics.rs
@@ -2161,6 +2161,11 @@ 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()));
+                    }
                 } else {
                     // If the root import is module-relative, add the import separately
                     corrections.push((
@@ -2168,11 +2173,6 @@ impl<'a, 'b> ImportResolver<'a, 'b> {
                         format!("use {module_name}::{import_snippet};\n"),
                     ));
                 }
-
-                // 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()));
-                }
             }
 
             let suggestion = Some((
diff --git a/tests/ui/imports/issue-99695-b.fixed b/tests/ui/imports/issue-99695-b.fixed
new file mode 100644
index 00000000000..0e60c73b67a
--- /dev/null
+++ b/tests/ui/imports/issue-99695-b.fixed
@@ -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() {}
diff --git a/tests/ui/imports/issue-99695-b.rs b/tests/ui/imports/issue-99695-b.rs
new file mode 100644
index 00000000000..031443a1f5d
--- /dev/null
+++ b/tests/ui/imports/issue-99695-b.rs
@@ -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() {}
diff --git a/tests/ui/imports/issue-99695-b.stderr b/tests/ui/imports/issue-99695-b.stderr
new file mode 100644
index 00000000000..b6f5c726a5c
--- /dev/null
+++ b/tests/ui/imports/issue-99695-b.stderr
@@ -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`.