resolve: Assign pub and pub(crate) visibilities to macro_rules items

This commit is contained in:
Vadim Petrochenkov 2018-12-09 21:49:21 +03:00
parent 1190f7cdf7
commit 099b3d86f9
3 changed files with 20 additions and 43 deletions

View File

@ -1073,7 +1073,12 @@ impl<'a> Resolver<'a> {
let ident = ident.modern(); let ident = ident.modern();
self.macro_names.insert(ident); self.macro_names.insert(ident);
let def = Def::Macro(def_id, MacroKind::Bang); 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); let binding = (def, vis, item.span, expansion).to_name_binding(self.arenas);
self.set_binding_parent_module(binding, self.current_module); self.set_binding_parent_module(binding, self.current_module);
let legacy_binding = self.arenas.alloc_legacy_binding(LegacyBinding { let legacy_binding = self.arenas.alloc_legacy_binding(LegacyBinding {
@ -1081,9 +1086,8 @@ impl<'a> Resolver<'a> {
}); });
*current_legacy_scope = LegacyScope::Binding(legacy_binding); *current_legacy_scope = LegacyScope::Binding(legacy_binding);
self.all_macros.insert(ident.name, def); 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 module = self.graph_root;
let vis = ty::Visibility::Public;
self.define(module, ident, MacroNS, self.define(module, ident, MacroNS,
(def, vis, item.span, expansion, IsMacroExport)); (def, vis, item.span, expansion, IsMacroExport));
} else { } else {

View File

@ -1,15 +1,14 @@
// edition:2018 // edition:2018
// For the time being `macro_rules` items are treated as *very* private...
#![feature(decl_macro, uniform_paths)] #![feature(decl_macro, uniform_paths)]
#![allow(non_camel_case_types)]
mod m1 { mod m1 {
// Non-exported legacy macros are treated as `pub(crate)`.
macro_rules! legacy_macro { () => () } macro_rules! legacy_macro { () => () }
// ... so they can't be imported by themselves, ... use legacy_macro as _; // OK
use legacy_macro as _; //~ ERROR `legacy_macro` is private, and cannot be re-exported pub(crate) use legacy_macro as _; // OK
pub use legacy_macro as _; //~ ERROR `legacy_macro` is private, and cannot be re-exported
} }
mod m2 { mod m2 {
@ -17,7 +16,7 @@ mod m2 {
type legacy_macro = u8; 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 use legacy_macro as _; // OK
} }
@ -27,19 +26,17 @@ mod m3 {
fn f() { fn f() {
macro_rules! legacy_macro { () => () } 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 use legacy_macro as _; //~ ERROR `legacy_macro` is ambiguous
//~| ERROR `legacy_macro` is private, and cannot be re-exported
} }
} }
mod exported { mod exported {
// Exported macros are treated as private as well, // Exported legacy macros are treated as `pub`.
// some better rules need to be figured out later.
#[macro_export] #[macro_export]
macro_rules! legacy_macro { () => () } 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() {} fn main() {}

View File

@ -1,39 +1,15 @@
error[E0364]: `legacy_macro` is private, and cannot be re-exported 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 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: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
| ^^^^^^^^^^^^^^^^^ | ^^^^^^^^^^^^^^^^^
| |
note: consider marking `legacy_macro` as `pub` in the imported module 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) error[E0659]: `legacy_macro` is ambiguous (name vs any other name during import resolution)
--> $DIR/macro-rules.rs:31:13 --> $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 = 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. Some errors occurred: E0364, E0659.
For more information about an error, try `rustc --explain E0364`. For more information about an error, try `rustc --explain E0364`.