From: Vadim Petrochenkov Date: Sun, 9 Dec 2018 18:49:21 +0000 (+0300) Subject: resolve: Assign `pub` and `pub(crate)` visibilities to `macro_rules` items X-Git-Url: https://git.lizzy.rs/?a=commitdiff_plain;h=099b3d86f92166ca5974a103e6998fdbaf6a9872;p=rust.git resolve: Assign `pub` and `pub(crate)` visibilities to `macro_rules` items --- diff --git a/src/librustc_resolve/macros.rs b/src/librustc_resolve/macros.rs index e5e6c7a994b..7856661741d 100644 --- a/src/librustc_resolve/macros.rs +++ b/src/librustc_resolve/macros.rs @@ -1073,7 +1073,12 @@ pub fn define_macro(&mut self, let ident = ident.modern(); self.macro_names.insert(ident); let def = Def::Macro(def_id, MacroKind::Bang); - let vis = ty::Visibility::Invisible; // Doesn't matter for legacy bindings + let is_macro_export = attr::contains_name(&item.attrs, "macro_export"); + let vis = if is_macro_export { + ty::Visibility::Public + } else { + ty::Visibility::Restricted(DefId::local(CRATE_DEF_INDEX)) + }; let binding = (def, vis, item.span, expansion).to_name_binding(self.arenas); self.set_binding_parent_module(binding, self.current_module); let legacy_binding = self.arenas.alloc_legacy_binding(LegacyBinding { @@ -1081,9 +1086,8 @@ pub fn define_macro(&mut self, }); *current_legacy_scope = LegacyScope::Binding(legacy_binding); self.all_macros.insert(ident.name, def); - if attr::contains_name(&item.attrs, "macro_export") { + if is_macro_export { let module = self.graph_root; - let vis = ty::Visibility::Public; self.define(module, ident, MacroNS, (def, vis, item.span, expansion, IsMacroExport)); } else { diff --git a/src/test/ui/rust-2018/uniform-paths/macro-rules.rs b/src/test/ui/rust-2018/uniform-paths/macro-rules.rs index 69f6a8668ad..e3c1b4e8ab3 100644 --- a/src/test/ui/rust-2018/uniform-paths/macro-rules.rs +++ b/src/test/ui/rust-2018/uniform-paths/macro-rules.rs @@ -1,15 +1,14 @@ // edition:2018 -// For the time being `macro_rules` items are treated as *very* private... - #![feature(decl_macro, uniform_paths)] -#![allow(non_camel_case_types)] mod m1 { + // Non-exported legacy macros are treated as `pub(crate)`. macro_rules! legacy_macro { () => () } - // ... so they can't be imported by themselves, ... - use legacy_macro as _; //~ ERROR `legacy_macro` is private, and cannot be re-exported + use legacy_macro as _; // OK + pub(crate) use legacy_macro as _; // OK + pub use legacy_macro as _; //~ ERROR `legacy_macro` is private, and cannot be re-exported } mod m2 { @@ -17,7 +16,7 @@ macro_rules! legacy_macro { () => () } type legacy_macro = u8; - // ... but don't prevent names from other namespaces from being imported, ... + // Legacy macro imports don't prevent names from other namespaces from being imported. use legacy_macro as _; // OK } @@ -27,19 +26,17 @@ mod m3 { fn f() { macro_rules! legacy_macro { () => () } - // ... but still create ambiguities with other names in the same namespace. + // Legacy macro imports create ambiguities with other names in the same namespace. use legacy_macro as _; //~ ERROR `legacy_macro` is ambiguous - //~| ERROR `legacy_macro` is private, and cannot be re-exported } } mod exported { - // Exported macros are treated as private as well, - // some better rules need to be figured out later. + // Exported legacy macros are treated as `pub`. #[macro_export] macro_rules! legacy_macro { () => () } - use legacy_macro as _; //~ ERROR `legacy_macro` is private, and cannot be re-exported + pub use legacy_macro as _; // OK } fn main() {} diff --git a/src/test/ui/rust-2018/uniform-paths/macro-rules.stderr b/src/test/ui/rust-2018/uniform-paths/macro-rules.stderr index be7bbd314df..684f5fceac1 100644 --- a/src/test/ui/rust-2018/uniform-paths/macro-rules.stderr +++ b/src/test/ui/rust-2018/uniform-paths/macro-rules.stderr @@ -1,39 +1,15 @@ error[E0364]: `legacy_macro` is private, and cannot be re-exported - --> $DIR/macro-rules.rs:12:9 + --> $DIR/macro-rules.rs:11:13 | -LL | use legacy_macro as _; //~ ERROR `legacy_macro` is private, and cannot be re-exported - | ^^^^^^^^^^^^^^^^^ - | -note: consider marking `legacy_macro` as `pub` in the imported module - --> $DIR/macro-rules.rs:12:9 - | -LL | use legacy_macro as _; //~ ERROR `legacy_macro` is private, and cannot be re-exported - | ^^^^^^^^^^^^^^^^^ - -error[E0364]: `legacy_macro` is private, and cannot be re-exported - --> $DIR/macro-rules.rs:31:13 - | -LL | use legacy_macro as _; //~ ERROR `legacy_macro` is ambiguous +LL | pub use legacy_macro as _; //~ ERROR `legacy_macro` is private, and cannot be re-exported | ^^^^^^^^^^^^^^^^^ | note: consider marking `legacy_macro` as `pub` in the imported module - --> $DIR/macro-rules.rs:31:13 + --> $DIR/macro-rules.rs:11:13 | -LL | use legacy_macro as _; //~ ERROR `legacy_macro` is ambiguous +LL | pub use legacy_macro as _; //~ ERROR `legacy_macro` is private, and cannot be re-exported | ^^^^^^^^^^^^^^^^^ -error[E0364]: `legacy_macro` is private, and cannot be re-exported - --> $DIR/macro-rules.rs:42:9 - | -LL | use legacy_macro as _; //~ ERROR `legacy_macro` is private, and cannot be re-exported - | ^^^^^^^^^^^^^^^^^ - | -note: consider marking `legacy_macro` as `pub` in the imported module - --> $DIR/macro-rules.rs:42:9 - | -LL | use legacy_macro as _; //~ ERROR `legacy_macro` is private, and cannot be re-exported - | ^^^^^^^^^^^^^^^^^ - error[E0659]: `legacy_macro` is ambiguous (name vs any other name during import resolution) --> $DIR/macro-rules.rs:31:13 | @@ -52,7 +28,7 @@ LL | macro legacy_macro() {} | ^^^^^^^^^^^^^^^^^^^^^^^ = help: use `self::legacy_macro` to refer to this macro unambiguously -error: aborting due to 4 previous errors +error: aborting due to 2 previous errors Some errors occurred: E0364, E0659. For more information about an error, try `rustc --explain E0364`.