Skip to content

Commit 099b3d8

Browse files
committed
resolve: Assign pub and pub(crate) visibilities to macro_rules items
1 parent 1190f7c commit 099b3d8

File tree

3 files changed

+20
-43
lines changed

3 files changed

+20
-43
lines changed

src/librustc_resolve/macros.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1073,17 +1073,21 @@ impl<'a> Resolver<'a> {
10731073
let ident = ident.modern();
10741074
self.macro_names.insert(ident);
10751075
let def = Def::Macro(def_id, MacroKind::Bang);
1076-
let vis = ty::Visibility::Invisible; // Doesn't matter for legacy bindings
1076+
let is_macro_export = attr::contains_name(&item.attrs, "macro_export");
1077+
let vis = if is_macro_export {
1078+
ty::Visibility::Public
1079+
} else {
1080+
ty::Visibility::Restricted(DefId::local(CRATE_DEF_INDEX))
1081+
};
10771082
let binding = (def, vis, item.span, expansion).to_name_binding(self.arenas);
10781083
self.set_binding_parent_module(binding, self.current_module);
10791084
let legacy_binding = self.arenas.alloc_legacy_binding(LegacyBinding {
10801085
parent_legacy_scope: *current_legacy_scope, binding, ident
10811086
});
10821087
*current_legacy_scope = LegacyScope::Binding(legacy_binding);
10831088
self.all_macros.insert(ident.name, def);
1084-
if attr::contains_name(&item.attrs, "macro_export") {
1089+
if is_macro_export {
10851090
let module = self.graph_root;
1086-
let vis = ty::Visibility::Public;
10871091
self.define(module, ident, MacroNS,
10881092
(def, vis, item.span, expansion, IsMacroExport));
10891093
} else {
Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,22 @@
11
// edition:2018
22

3-
// For the time being `macro_rules` items are treated as *very* private...
4-
53
#![feature(decl_macro, uniform_paths)]
6-
#![allow(non_camel_case_types)]
74

85
mod m1 {
6+
// Non-exported legacy macros are treated as `pub(crate)`.
97
macro_rules! legacy_macro { () => () }
108

11-
// ... so they can't be imported by themselves, ...
12-
use legacy_macro as _; //~ ERROR `legacy_macro` is private, and cannot be re-exported
9+
use legacy_macro as _; // OK
10+
pub(crate) use legacy_macro as _; // OK
11+
pub use legacy_macro as _; //~ ERROR `legacy_macro` is private, and cannot be re-exported
1312
}
1413

1514
mod m2 {
1615
macro_rules! legacy_macro { () => () }
1716

1817
type legacy_macro = u8;
1918

20-
// ... but don't prevent names from other namespaces from being imported, ...
19+
// Legacy macro imports don't prevent names from other namespaces from being imported.
2120
use legacy_macro as _; // OK
2221
}
2322

@@ -27,19 +26,17 @@ mod m3 {
2726
fn f() {
2827
macro_rules! legacy_macro { () => () }
2928

30-
// ... but still create ambiguities with other names in the same namespace.
29+
// Legacy macro imports create ambiguities with other names in the same namespace.
3130
use legacy_macro as _; //~ ERROR `legacy_macro` is ambiguous
32-
//~| ERROR `legacy_macro` is private, and cannot be re-exported
3331
}
3432
}
3533

3634
mod exported {
37-
// Exported macros are treated as private as well,
38-
// some better rules need to be figured out later.
35+
// Exported legacy macros are treated as `pub`.
3936
#[macro_export]
4037
macro_rules! legacy_macro { () => () }
4138

42-
use legacy_macro as _; //~ ERROR `legacy_macro` is private, and cannot be re-exported
39+
pub use legacy_macro as _; // OK
4340
}
4441

4542
fn main() {}
Lines changed: 5 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,15 @@
11
error[E0364]: `legacy_macro` is private, and cannot be re-exported
2-
--> $DIR/macro-rules.rs:12:9
2+
--> $DIR/macro-rules.rs:11:13
33
|
4-
LL | use legacy_macro as _; //~ ERROR `legacy_macro` is private, and cannot be re-exported
5-
| ^^^^^^^^^^^^^^^^^
6-
|
7-
note: consider marking `legacy_macro` as `pub` in the imported module
8-
--> $DIR/macro-rules.rs:12:9
9-
|
10-
LL | use legacy_macro as _; //~ ERROR `legacy_macro` is private, and cannot be re-exported
11-
| ^^^^^^^^^^^^^^^^^
12-
13-
error[E0364]: `legacy_macro` is private, and cannot be re-exported
14-
--> $DIR/macro-rules.rs:31:13
15-
|
16-
LL | use legacy_macro as _; //~ ERROR `legacy_macro` is ambiguous
4+
LL | pub use legacy_macro as _; //~ ERROR `legacy_macro` is private, and cannot be re-exported
175
| ^^^^^^^^^^^^^^^^^
186
|
197
note: consider marking `legacy_macro` as `pub` in the imported module
20-
--> $DIR/macro-rules.rs:31:13
8+
--> $DIR/macro-rules.rs:11:13
219
|
22-
LL | use legacy_macro as _; //~ ERROR `legacy_macro` is ambiguous
10+
LL | pub use legacy_macro as _; //~ ERROR `legacy_macro` is private, and cannot be re-exported
2311
| ^^^^^^^^^^^^^^^^^
2412

25-
error[E0364]: `legacy_macro` is private, and cannot be re-exported
26-
--> $DIR/macro-rules.rs:42:9
27-
|
28-
LL | use legacy_macro as _; //~ ERROR `legacy_macro` is private, and cannot be re-exported
29-
| ^^^^^^^^^^^^^^^^^
30-
|
31-
note: consider marking `legacy_macro` as `pub` in the imported module
32-
--> $DIR/macro-rules.rs:42:9
33-
|
34-
LL | use legacy_macro as _; //~ ERROR `legacy_macro` is private, and cannot be re-exported
35-
| ^^^^^^^^^^^^^^^^^
36-
3713
error[E0659]: `legacy_macro` is ambiguous (name vs any other name during import resolution)
3814
--> $DIR/macro-rules.rs:31:13
3915
|
@@ -52,7 +28,7 @@ LL | macro legacy_macro() {}
5228
| ^^^^^^^^^^^^^^^^^^^^^^^
5329
= help: use `self::legacy_macro` to refer to this macro unambiguously
5430

55-
error: aborting due to 4 previous errors
31+
error: aborting due to 2 previous errors
5632

5733
Some errors occurred: E0364, E0659.
5834
For more information about an error, try `rustc --explain E0364`.

0 commit comments

Comments
 (0)