Skip to content

Commit 972dc15

Browse files
committed
resolve: Rename some fields related to legacy macro scopes
1 parent ad0afa9 commit 972dc15

File tree

2 files changed

+60
-51
lines changed

2 files changed

+60
-51
lines changed

src/librustc_resolve/build_reduced_graph.rs

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -933,7 +933,7 @@ impl<'a, 'cl> Resolver<'a, 'cl> {
933933

934934
pub struct BuildReducedGraphVisitor<'a, 'b: 'a, 'c: 'b> {
935935
pub resolver: &'a mut Resolver<'b, 'c>,
936-
pub legacy_scope: LegacyScope<'b>,
936+
pub current_legacy_scope: LegacyScope<'b>,
937937
pub expansion: Mark,
938938
}
939939

@@ -943,7 +943,7 @@ impl<'a, 'b, 'cl> BuildReducedGraphVisitor<'a, 'b, 'cl> {
943943
self.resolver.current_module.unresolved_invocations.borrow_mut().insert(mark);
944944
let invocation = self.resolver.invocations[&mark];
945945
invocation.module.set(self.resolver.current_module);
946-
invocation.legacy_scope.set(self.legacy_scope);
946+
invocation.parent_legacy_scope.set(self.current_legacy_scope);
947947
invocation
948948
}
949949
}
@@ -969,29 +969,30 @@ impl<'a, 'b, 'cl> Visitor<'a> for BuildReducedGraphVisitor<'a, 'b, 'cl> {
969969
fn visit_item(&mut self, item: &'a Item) {
970970
let macro_use = match item.node {
971971
ItemKind::MacroDef(..) => {
972-
self.resolver.define_macro(item, self.expansion, &mut self.legacy_scope);
972+
self.resolver.define_macro(item, self.expansion, &mut self.current_legacy_scope);
973973
return
974974
}
975975
ItemKind::Mac(..) => {
976-
self.legacy_scope = LegacyScope::Expansion(self.visit_invoc(item.id));
976+
self.current_legacy_scope = LegacyScope::Expansion(self.visit_invoc(item.id));
977977
return
978978
}
979979
ItemKind::Mod(..) => self.resolver.contains_macro_use(&item.attrs),
980980
_ => false,
981981
};
982982

983-
let (parent, legacy_scope) = (self.resolver.current_module, self.legacy_scope);
983+
let orig_current_module = self.resolver.current_module;
984+
let orig_current_legacy_scope = self.current_legacy_scope;
984985
self.resolver.build_reduced_graph_for_item(item, self.expansion);
985986
visit::walk_item(self, item);
986-
self.resolver.current_module = parent;
987+
self.resolver.current_module = orig_current_module;
987988
if !macro_use {
988-
self.legacy_scope = legacy_scope;
989+
self.current_legacy_scope = orig_current_legacy_scope;
989990
}
990991
}
991992

992993
fn visit_stmt(&mut self, stmt: &'a ast::Stmt) {
993994
if let ast::StmtKind::Mac(..) = stmt.node {
994-
self.legacy_scope = LegacyScope::Expansion(self.visit_invoc(stmt.id));
995+
self.current_legacy_scope = LegacyScope::Expansion(self.visit_invoc(stmt.id));
995996
} else {
996997
visit::walk_stmt(self, stmt);
997998
}
@@ -1008,11 +1009,12 @@ impl<'a, 'b, 'cl> Visitor<'a> for BuildReducedGraphVisitor<'a, 'b, 'cl> {
10081009
}
10091010

10101011
fn visit_block(&mut self, block: &'a Block) {
1011-
let (parent, legacy_scope) = (self.resolver.current_module, self.legacy_scope);
1012+
let orig_current_module = self.resolver.current_module;
1013+
let orig_current_legacy_scope = self.current_legacy_scope;
10121014
self.resolver.build_reduced_graph_for_block(block, self.expansion);
10131015
visit::walk_block(self, block);
1014-
self.resolver.current_module = parent;
1015-
self.legacy_scope = legacy_scope;
1016+
self.resolver.current_module = orig_current_module;
1017+
self.current_legacy_scope = orig_current_legacy_scope;
10161018
}
10171019

10181020
fn visit_trait_item(&mut self, item: &'a TraitItem) {

src/librustc_resolve/macros.rs

Lines changed: 47 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -50,26 +50,37 @@ crate struct FromPrelude(bool);
5050

5151
#[derive(Clone)]
5252
pub struct InvocationData<'a> {
53-
pub module: Cell<Module<'a>>,
54-
pub def_index: DefIndex,
55-
// The scope in which the invocation path is resolved.
56-
pub legacy_scope: Cell<LegacyScope<'a>>,
57-
// The smallest scope that includes this invocation's expansion,
58-
// or `Empty` if this invocation has not been expanded yet.
59-
pub expansion: Cell<LegacyScope<'a>>,
53+
crate module: Cell<Module<'a>>,
54+
def_index: DefIndex,
55+
// Legacy scope in which the macro was invoked.
56+
// The invocation path is resolved in this scope.
57+
crate parent_legacy_scope: Cell<LegacyScope<'a>>,
58+
// Legacy scope *produced* by expanding this macro invocation,
59+
// includes all the macro_rules items, other invocations, etc generated by it.
60+
// `Empty` is used if for invocations that has not been expanded yet.
61+
output_legacy_scope: Cell<LegacyScope<'a>>,
6062
}
6163

6264
impl<'a> InvocationData<'a> {
6365
pub fn root(graph_root: Module<'a>) -> Self {
6466
InvocationData {
6567
module: Cell::new(graph_root),
6668
def_index: CRATE_DEF_INDEX,
67-
legacy_scope: Cell::new(LegacyScope::Empty),
68-
expansion: Cell::new(LegacyScope::Empty),
69+
parent_legacy_scope: Cell::new(LegacyScope::Empty),
70+
output_legacy_scope: Cell::new(LegacyScope::Empty),
6971
}
7072
}
7173
}
7274

75+
// Binding produced by a `macro_rules` item.
76+
// Not modularized, can shadow previous legacy bindings, etc.
77+
pub struct LegacyBinding<'a> {
78+
binding: &'a NameBinding<'a>,
79+
// Legacy scope into which the `macro_rules` item was planted.
80+
parent_legacy_scope: Cell<LegacyScope<'a>>,
81+
ident: Ident,
82+
}
83+
7384
#[derive(Copy, Clone)]
7485
pub enum LegacyScope<'a> {
7586
Empty,
@@ -78,14 +89,6 @@ pub enum LegacyScope<'a> {
7889
Binding(&'a LegacyBinding<'a>),
7990
}
8091

81-
// Binding produced by a `macro_rules` item.
82-
// Not modularized, can shadow previous legacy bindings, etc.
83-
pub struct LegacyBinding<'a> {
84-
binding: &'a NameBinding<'a>,
85-
parent: Cell<LegacyScope<'a>>,
86-
ident: Ident,
87-
}
88-
8992
pub struct ProcMacError {
9093
crate_name: Symbol,
9194
name: Symbol,
@@ -105,8 +108,8 @@ impl<'a, 'crateloader: 'a> base::Resolver for Resolver<'a, 'crateloader> {
105108
self.invocations.insert(mark, self.arenas.alloc_invocation_data(InvocationData {
106109
module: Cell::new(module),
107110
def_index: module.def_id().unwrap().index,
108-
legacy_scope: Cell::new(LegacyScope::Empty),
109-
expansion: Cell::new(LegacyScope::Empty),
111+
parent_legacy_scope: Cell::new(LegacyScope::Empty),
112+
output_legacy_scope: Cell::new(LegacyScope::Empty),
110113
}));
111114
mark
112115
}
@@ -178,11 +181,11 @@ impl<'a, 'crateloader: 'a> base::Resolver for Resolver<'a, 'crateloader> {
178181
}
179182
let mut visitor = BuildReducedGraphVisitor {
180183
resolver: self,
181-
legacy_scope: LegacyScope::Invocation(invocation),
184+
current_legacy_scope: LegacyScope::Invocation(invocation),
182185
expansion: mark,
183186
};
184187
fragment.visit_with(&mut visitor);
185-
invocation.expansion.set(visitor.legacy_scope);
188+
invocation.output_legacy_scope.set(visitor.current_legacy_scope);
186189
}
187190

188191
fn add_builtin(&mut self, ident: ast::Ident, ext: Lrc<SyntaxExtension>) {
@@ -458,7 +461,7 @@ impl<'a, 'cl> Resolver<'a, 'cl> {
458461
}
459462

460463
let legacy_resolution =
461-
self.resolve_legacy_scope(path[0], invoc_id, &invocation.legacy_scope, false);
464+
self.resolve_legacy_scope(path[0], invoc_id, &invocation.parent_legacy_scope, false);
462465
let result = if let Some(legacy_binding) = legacy_resolution {
463466
Ok(legacy_binding.def())
464467
} else {
@@ -791,18 +794,20 @@ impl<'a, 'cl> Resolver<'a, 'cl> {
791794

792795
macro_rules! continue_search { () => {
793796
where_to_resolve = match where_to_resolve.get() {
794-
LegacyScope::Binding(binding) => &binding.parent,
795-
LegacyScope::Invocation(invocation) => &invocation.legacy_scope,
796-
LegacyScope::Expansion(invocation) => match invocation.expansion.get() {
797-
LegacyScope::Empty => &invocation.legacy_scope,
798-
LegacyScope::Binding(..) |
799-
LegacyScope::Expansion(..) => &invocation.expansion,
800-
LegacyScope::Invocation(..) => {
801-
where_to_resolve.set(invocation.legacy_scope.get());
802-
where_to_resolve
797+
LegacyScope::Empty => break, // nowhere else to search
798+
LegacyScope::Binding(binding) => &binding.parent_legacy_scope,
799+
LegacyScope::Invocation(invocation) => &invocation.parent_legacy_scope,
800+
LegacyScope::Expansion(invocation) => {
801+
match invocation.output_legacy_scope.get() {
802+
LegacyScope::Empty => &invocation.parent_legacy_scope,
803+
LegacyScope::Binding(..) |
804+
LegacyScope::Expansion(..) => &invocation.output_legacy_scope,
805+
LegacyScope::Invocation(..) => {
806+
where_to_resolve.set(invocation.parent_legacy_scope.get());
807+
where_to_resolve
808+
}
803809
}
804810
}
805-
LegacyScope::Empty => break, // nowhere else to search
806811
};
807812

808813
continue;
@@ -857,8 +862,9 @@ impl<'a, 'cl> Resolver<'a, 'cl> {
857862

858863
for &(invoc_id, ident, kind, def) in module.legacy_macro_resolutions.borrow().iter() {
859864
let span = ident.span;
860-
let legacy_scope = &self.invocations[&invoc_id].legacy_scope;
861-
let legacy_resolution = self.resolve_legacy_scope(ident, invoc_id, legacy_scope, true);
865+
let invoc_parent_legacy_scope = &self.invocations[&invoc_id].parent_legacy_scope;
866+
let legacy_resolution =
867+
self.resolve_legacy_scope(ident, invoc_id, invoc_parent_legacy_scope, true);
862868
let resolution = self.resolve_lexical_macro_path_segment(
863869
ident, MacroNS, invoc_id, true, true, kind == MacroKind::Attr, span
864870
);
@@ -984,8 +990,8 @@ impl<'a, 'cl> Resolver<'a, 'cl> {
984990
arenas.alloc_invocation_data(InvocationData {
985991
def_index: invoc.def_index,
986992
module: Cell::new(graph_root),
987-
expansion: Cell::new(LegacyScope::Empty),
988-
legacy_scope: Cell::new(LegacyScope::Empty),
993+
parent_legacy_scope: Cell::new(LegacyScope::Empty),
994+
output_legacy_scope: Cell::new(LegacyScope::Empty),
989995
})
990996
});
991997
};
@@ -1000,7 +1006,7 @@ impl<'a, 'cl> Resolver<'a, 'cl> {
10001006
pub fn define_macro(&mut self,
10011007
item: &ast::Item,
10021008
expansion: Mark,
1003-
legacy_scope: &mut LegacyScope<'a>) {
1009+
current_legacy_scope: &mut LegacyScope<'a>) {
10041010
self.local_macro_def_scopes.insert(item.id, self.current_module);
10051011
let ident = item.ident;
10061012
if ident.name == "macro_rules" {
@@ -1020,9 +1026,10 @@ impl<'a, 'cl> Resolver<'a, 'cl> {
10201026
let def = Def::Macro(def_id, MacroKind::Bang);
10211027
let vis = ty::Visibility::Invisible; // Doesn't matter for legacy bindings
10221028
let binding = (def, vis, item.span, expansion).to_name_binding(self.arenas);
1023-
*legacy_scope = LegacyScope::Binding(self.arenas.alloc_legacy_binding(
1024-
LegacyBinding { parent: Cell::new(*legacy_scope), binding, ident }
1025-
));
1029+
let legacy_binding = self.arenas.alloc_legacy_binding(LegacyBinding {
1030+
parent_legacy_scope: Cell::new(*current_legacy_scope), binding, ident
1031+
});
1032+
*current_legacy_scope = LegacyScope::Binding(legacy_binding);
10261033
self.all_macros.insert(ident.name, def);
10271034
if attr::contains_name(&item.attrs, "macro_export") {
10281035
let module = self.graph_root;

0 commit comments

Comments
 (0)