Skip to content

Commit 62956b5

Browse files
committed
resolve: Assign pub and pub(crate) visibilities to macro_rules items
1 parent 98e435e commit 62956b5

File tree

3 files changed

+20
-42
lines changed

3 files changed

+20
-42
lines changed

src/librustc_resolve/macros.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1189,17 +1189,21 @@ impl<'a, 'cl> Resolver<'a, 'cl> {
11891189
let ident = ident.modern();
11901190
self.macro_names.insert(ident);
11911191
let def = Def::Macro(def_id, MacroKind::Bang);
1192-
let vis = ty::Visibility::Invisible; // Doesn't matter for legacy bindings
1192+
let is_macro_export = attr::contains_name(&item.attrs, "macro_export");
1193+
let vis = if is_macro_export {
1194+
ty::Visibility::Public
1195+
} else {
1196+
ty::Visibility::Restricted(DefId::local(CRATE_DEF_INDEX))
1197+
};
11931198
let binding = (def, vis, item.span, expansion).to_name_binding(self.arenas);
11941199
self.set_binding_parent_module(binding, self.current_module);
11951200
let legacy_binding = self.arenas.alloc_legacy_binding(LegacyBinding {
11961201
parent_legacy_scope: *current_legacy_scope, binding, ident
11971202
});
11981203
*current_legacy_scope = LegacyScope::Binding(legacy_binding);
11991204
self.all_macros.insert(ident.name, def);
1200-
if attr::contains_name(&item.attrs, "macro_export") {
1205+
if is_macro_export {
12011206
let module = self.graph_root;
1202-
let vis = ty::Visibility::Public;
12031207
self.define(module, ident, MacroNS,
12041208
(def, vis, item.span, expansion, IsMacroExport));
12051209
} else {
Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,22 @@
11
// edition:2018
22

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

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

10-
// ... so they can't be imported by themselves, ...
11-
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
1212
}
1313

1414
mod m2 {
1515
macro_rules! legacy_macro { () => () }
1616

1717
type legacy_macro = u8;
1818

19-
// ... 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.
2020
use legacy_macro as _; // OK
2121
}
2222

@@ -26,19 +26,17 @@ mod m3 {
2626
fn f() {
2727
macro_rules! legacy_macro { () => () }
2828

29-
// ... but still create ambiguities with other names in the same namespace.
29+
// Legacy macro imports create ambiguities with other names in the same namespace.
3030
use legacy_macro as _; //~ ERROR `legacy_macro` is ambiguous
31-
//~| ERROR `legacy_macro` is private, and cannot be re-exported
3231
}
3332
}
3433

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

41-
use legacy_macro as _; //~ ERROR `legacy_macro` is private, and cannot be re-exported
39+
pub use legacy_macro as _; // OK
4240
}
4341

4442
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:11: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:11: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:30: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:30: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:41: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:41: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:30: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)