Skip to content

Commit d26ae20

Browse files
committed
resolve: Rename some fields related to legacy macro scopes
1 parent c057d57 commit d26ae20

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
@@ -950,7 +950,7 @@ impl<'a, 'cl> Resolver<'a, 'cl> {
950950

951951
pub struct BuildReducedGraphVisitor<'a, 'b: 'a, 'c: 'b> {
952952
pub resolver: &'a mut Resolver<'b, 'c>,
953-
pub legacy_scope: LegacyScope<'b>,
953+
pub current_legacy_scope: LegacyScope<'b>,
954954
pub expansion: Mark,
955955
}
956956

@@ -960,7 +960,7 @@ impl<'a, 'b, 'cl> BuildReducedGraphVisitor<'a, 'b, 'cl> {
960960
self.resolver.current_module.unresolved_invocations.borrow_mut().insert(mark);
961961
let invocation = self.resolver.invocations[&mark];
962962
invocation.module.set(self.resolver.current_module);
963-
invocation.legacy_scope.set(self.legacy_scope);
963+
invocation.parent_legacy_scope.set(self.current_legacy_scope);
964964
invocation
965965
}
966966
}
@@ -986,29 +986,30 @@ impl<'a, 'b, 'cl> Visitor<'a> for BuildReducedGraphVisitor<'a, 'b, 'cl> {
986986
fn visit_item(&mut self, item: &'a Item) {
987987
let macro_use = match item.node {
988988
ItemKind::MacroDef(..) => {
989-
self.resolver.define_macro(item, self.expansion, &mut self.legacy_scope);
989+
self.resolver.define_macro(item, self.expansion, &mut self.current_legacy_scope);
990990
return
991991
}
992992
ItemKind::Mac(..) => {
993-
self.legacy_scope = LegacyScope::Expansion(self.visit_invoc(item.id));
993+
self.current_legacy_scope = LegacyScope::Expansion(self.visit_invoc(item.id));
994994
return
995995
}
996996
ItemKind::Mod(..) => self.resolver.contains_macro_use(&item.attrs),
997997
_ => false,
998998
};
999999

1000-
let (parent, legacy_scope) = (self.resolver.current_module, self.legacy_scope);
1000+
let orig_current_module = self.resolver.current_module;
1001+
let orig_current_legacy_scope = self.current_legacy_scope;
10011002
self.resolver.build_reduced_graph_for_item(item, self.expansion);
10021003
visit::walk_item(self, item);
1003-
self.resolver.current_module = parent;
1004+
self.resolver.current_module = orig_current_module;
10041005
if !macro_use {
1005-
self.legacy_scope = legacy_scope;
1006+
self.current_legacy_scope = orig_current_legacy_scope;
10061007
}
10071008
}
10081009

10091010
fn visit_stmt(&mut self, stmt: &'a ast::Stmt) {
10101011
if let ast::StmtKind::Mac(..) = stmt.node {
1011-
self.legacy_scope = LegacyScope::Expansion(self.visit_invoc(stmt.id));
1012+
self.current_legacy_scope = LegacyScope::Expansion(self.visit_invoc(stmt.id));
10121013
} else {
10131014
visit::walk_stmt(self, stmt);
10141015
}
@@ -1025,11 +1026,12 @@ impl<'a, 'b, 'cl> Visitor<'a> for BuildReducedGraphVisitor<'a, 'b, 'cl> {
10251026
}
10261027

10271028
fn visit_block(&mut self, block: &'a Block) {
1028-
let (parent, legacy_scope) = (self.resolver.current_module, self.legacy_scope);
1029+
let orig_current_module = self.resolver.current_module;
1030+
let orig_current_legacy_scope = self.current_legacy_scope;
10291031
self.resolver.build_reduced_graph_for_block(block, self.expansion);
10301032
visit::walk_block(self, block);
1031-
self.resolver.current_module = parent;
1032-
self.legacy_scope = legacy_scope;
1033+
self.resolver.current_module = orig_current_module;
1034+
self.current_legacy_scope = orig_current_legacy_scope;
10331035
}
10341036

10351037
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>) {
@@ -481,7 +484,7 @@ impl<'a, 'cl> Resolver<'a, 'cl> {
481484
}
482485

483486
let legacy_resolution =
484-
self.resolve_legacy_scope(path[0], invoc_id, &invocation.legacy_scope, false);
487+
self.resolve_legacy_scope(path[0], invoc_id, &invocation.parent_legacy_scope, false);
485488
let result = if let Some(legacy_binding) = legacy_resolution {
486489
Ok(legacy_binding.def())
487490
} else {
@@ -814,18 +817,20 @@ impl<'a, 'cl> Resolver<'a, 'cl> {
814817

815818
macro_rules! continue_search { () => {
816819
where_to_resolve = match where_to_resolve.get() {
817-
LegacyScope::Binding(binding) => &binding.parent,
818-
LegacyScope::Invocation(invocation) => &invocation.legacy_scope,
819-
LegacyScope::Expansion(invocation) => match invocation.expansion.get() {
820-
LegacyScope::Empty => &invocation.legacy_scope,
821-
LegacyScope::Binding(..) |
822-
LegacyScope::Expansion(..) => &invocation.expansion,
823-
LegacyScope::Invocation(..) => {
824-
where_to_resolve.set(invocation.legacy_scope.get());
825-
where_to_resolve
820+
LegacyScope::Empty => break, // nowhere else to search
821+
LegacyScope::Binding(binding) => &binding.parent_legacy_scope,
822+
LegacyScope::Invocation(invocation) => &invocation.parent_legacy_scope,
823+
LegacyScope::Expansion(invocation) => {
824+
match invocation.output_legacy_scope.get() {
825+
LegacyScope::Empty => &invocation.parent_legacy_scope,
826+
LegacyScope::Binding(..) |
827+
LegacyScope::Expansion(..) => &invocation.output_legacy_scope,
828+
LegacyScope::Invocation(..) => {
829+
where_to_resolve.set(invocation.parent_legacy_scope.get());
830+
where_to_resolve
831+
}
826832
}
827833
}
828-
LegacyScope::Empty => break, // nowhere else to search
829834
};
830835

831836
continue;
@@ -880,8 +885,9 @@ impl<'a, 'cl> Resolver<'a, 'cl> {
880885

881886
for &(invoc_id, ident, kind, def) in module.legacy_macro_resolutions.borrow().iter() {
882887
let span = ident.span;
883-
let legacy_scope = &self.invocations[&invoc_id].legacy_scope;
884-
let legacy_resolution = self.resolve_legacy_scope(ident, invoc_id, legacy_scope, true);
888+
let invoc_parent_legacy_scope = &self.invocations[&invoc_id].parent_legacy_scope;
889+
let legacy_resolution =
890+
self.resolve_legacy_scope(ident, invoc_id, invoc_parent_legacy_scope, true);
885891
let resolution = self.resolve_lexical_macro_path_segment(
886892
ident, MacroNS, invoc_id, true, true, kind == MacroKind::Attr, span
887893
);
@@ -1007,8 +1013,8 @@ impl<'a, 'cl> Resolver<'a, 'cl> {
10071013
arenas.alloc_invocation_data(InvocationData {
10081014
def_index: invoc.def_index,
10091015
module: Cell::new(graph_root),
1010-
expansion: Cell::new(LegacyScope::Empty),
1011-
legacy_scope: Cell::new(LegacyScope::Empty),
1016+
parent_legacy_scope: Cell::new(LegacyScope::Empty),
1017+
output_legacy_scope: Cell::new(LegacyScope::Empty),
10121018
})
10131019
});
10141020
};
@@ -1023,7 +1029,7 @@ impl<'a, 'cl> Resolver<'a, 'cl> {
10231029
pub fn define_macro(&mut self,
10241030
item: &ast::Item,
10251031
expansion: Mark,
1026-
legacy_scope: &mut LegacyScope<'a>) {
1032+
current_legacy_scope: &mut LegacyScope<'a>) {
10271033
self.local_macro_def_scopes.insert(item.id, self.current_module);
10281034
let ident = item.ident;
10291035
if ident.name == "macro_rules" {
@@ -1043,9 +1049,10 @@ impl<'a, 'cl> Resolver<'a, 'cl> {
10431049
let def = Def::Macro(def_id, MacroKind::Bang);
10441050
let vis = ty::Visibility::Invisible; // Doesn't matter for legacy bindings
10451051
let binding = (def, vis, item.span, expansion).to_name_binding(self.arenas);
1046-
*legacy_scope = LegacyScope::Binding(self.arenas.alloc_legacy_binding(
1047-
LegacyBinding { parent: Cell::new(*legacy_scope), binding, ident }
1048-
));
1052+
let legacy_binding = self.arenas.alloc_legacy_binding(LegacyBinding {
1053+
parent_legacy_scope: Cell::new(*current_legacy_scope), binding, ident
1054+
});
1055+
*current_legacy_scope = LegacyScope::Binding(legacy_binding);
10491056
self.all_macros.insert(ident.name, def);
10501057
if attr::contains_name(&item.attrs, "macro_export") {
10511058
let module = self.graph_root;

0 commit comments

Comments
 (0)