From 6b62275bc9e60315e41018ea0d6a70982e9f6008 Mon Sep 17 00:00:00 2001 From: bohan Date: Thu, 28 Dec 2023 15:18:49 +0800 Subject: [PATCH] exclude unexported macro bindings from extern crate --- .../rustc_resolve/src/build_reduced_graph.rs | 3 ++- .../extern/auxiliary/issue-80074-macro-2.rs | 3 +++ .../ui/extern/auxiliary/issue-80074-macro.rs | 2 ++ tests/ui/extern/issue-80074.rs | 11 +++++++- tests/ui/extern/issue-80074.stderr | 27 +++++++++++++++++++ 5 files changed, 44 insertions(+), 2 deletions(-) create mode 100644 tests/ui/extern/auxiliary/issue-80074-macro-2.rs create mode 100644 tests/ui/extern/issue-80074.stderr diff --git a/compiler/rustc_resolve/src/build_reduced_graph.rs b/compiler/rustc_resolve/src/build_reduced_graph.rs index 4e1fda5479c88..11c951072988e 100644 --- a/compiler/rustc_resolve/src/build_reduced_graph.rs +++ b/compiler/rustc_resolve/src/build_reduced_graph.rs @@ -1071,7 +1071,8 @@ impl<'a, 'b, 'tcx> BuildReducedGraphVisitor<'a, 'b, 'tcx> { let import = macro_use_import(self, span); self.r.potentially_unused_imports.push(import); module.for_each_child(self, |this, ident, ns, binding| { - if ns == MacroNS { + if ns == MacroNS && this.r.is_accessible_from(binding.vis, this.parent_scope.module) + { let imported_binding = this.r.import(binding, import); this.add_macro_use_binding(ident.name, imported_binding, span, allow_shadowing); } diff --git a/tests/ui/extern/auxiliary/issue-80074-macro-2.rs b/tests/ui/extern/auxiliary/issue-80074-macro-2.rs new file mode 100644 index 0000000000000..ea47a420738f9 --- /dev/null +++ b/tests/ui/extern/auxiliary/issue-80074-macro-2.rs @@ -0,0 +1,3 @@ +// edition:2018 + +macro_rules! boo { () => {}; } diff --git a/tests/ui/extern/auxiliary/issue-80074-macro.rs b/tests/ui/extern/auxiliary/issue-80074-macro.rs index 30e0f19ab8d84..3e912d977159a 100644 --- a/tests/ui/extern/auxiliary/issue-80074-macro.rs +++ b/tests/ui/extern/auxiliary/issue-80074-macro.rs @@ -2,3 +2,5 @@ macro_rules! foo_ { () => {}; } use foo_ as foo; + +macro_rules! bar { () => {}; } diff --git a/tests/ui/extern/issue-80074.rs b/tests/ui/extern/issue-80074.rs index f83027d4abfd2..5b25829781409 100644 --- a/tests/ui/extern/issue-80074.rs +++ b/tests/ui/extern/issue-80074.rs @@ -1,10 +1,19 @@ // edition:2018 -// build-pass // aux-crate:issue_80074=issue-80074-macro.rs +// aux-crate:issue_80074_2=issue-80074-macro-2.rs #[macro_use] extern crate issue_80074; +#[macro_use(boo)] +extern crate issue_80074_2; +//~^^ ERROR: imported macro not found + fn main() { foo!(); + //~^ ERROR: cannot find macro `foo` in this scope + bar!(); + //~^ ERROR: cannot find macro `bar` in this scope + boo!(); + //~^ ERROR: cannot find macro `boo` in this scope } diff --git a/tests/ui/extern/issue-80074.stderr b/tests/ui/extern/issue-80074.stderr new file mode 100644 index 0000000000000..1320c401c90ee --- /dev/null +++ b/tests/ui/extern/issue-80074.stderr @@ -0,0 +1,27 @@ +error[E0469]: imported macro not found + --> $DIR/issue-80074.rs:8:13 + | +LL | #[macro_use(boo)] + | ^^^ + +error: cannot find macro `boo` in this scope + --> $DIR/issue-80074.rs:17:5 + | +LL | boo!(); + | ^^^ + +error: cannot find macro `bar` in this scope + --> $DIR/issue-80074.rs:15:5 + | +LL | bar!(); + | ^^^ + +error: cannot find macro `foo` in this scope + --> $DIR/issue-80074.rs:13:5 + | +LL | foo!(); + | ^^^ + +error: aborting due to 4 previous errors + +For more information about this error, try `rustc --explain E0469`.