about summary refs log tree commit diff
diff options
context:
space:
mode:
authorVadim Petrochenkov <vadim.petrochenkov@gmail.com>2018-12-09 21:49:21 +0300
committerVadim Petrochenkov <vadim.petrochenkov@gmail.com>2019-01-12 16:17:26 +0300
commit099b3d86f92166ca5974a103e6998fdbaf6a9872 (patch)
tree82b44f57c845509a1bbc9d495f8a506cf0832596
parent1190f7cdf7a62e25c9a8eaf58e0906849692bf2b (diff)
downloadrust-099b3d86f92166ca5974a103e6998fdbaf6a9872.tar.gz
rust-099b3d86f92166ca5974a103e6998fdbaf6a9872.zip
resolve: Assign `pub` and `pub(crate)` visibilities to `macro_rules` items
-rw-r--r--src/librustc_resolve/macros.rs10
-rw-r--r--src/test/ui/rust-2018/uniform-paths/macro-rules.rs19
-rw-r--r--src/test/ui/rust-2018/uniform-paths/macro-rules.stderr34
3 files changed, 20 insertions, 43 deletions
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 @@ impl<'a> Resolver<'a> {
             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 @@ impl<'a> Resolver<'a> {
             });
             *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 @@ mod m2 {
 
     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`.