]> git.lizzy.rs Git - rust.git/commitdiff
resolve: Assign `pub` and `pub(crate)` visibilities to `macro_rules` items
authorVadim Petrochenkov <vadim.petrochenkov@gmail.com>
Sun, 9 Dec 2018 18:49:21 +0000 (21:49 +0300)
committerVadim Petrochenkov <vadim.petrochenkov@gmail.com>
Sat, 12 Jan 2019 13:17:26 +0000 (16:17 +0300)
src/librustc_resolve/macros.rs
src/test/ui/rust-2018/uniform-paths/macro-rules.rs
src/test/ui/rust-2018/uniform-paths/macro-rules.stderr

index e5e6c7a994b7a02ad6dddaae4b2af30ab9ede69a..7856661741da7abeb7a9224deb2bec4a566e1fcb 100644 (file)
@@ -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 {
index 69f6a8668add39a7231bbc29b79d3eb93f10bdaf..e3c1b4e8ab33afa4ca00351371869fa634e1340d 100644 (file)
@@ -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() {}
index be7bbd314df316027358d84d6e44872a217ab306..684f5fceac15fc6f191d99bc6e07031553c99d3d 100644 (file)
@@ -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`.