Skip to content

Commit df03e42

Browse files
committed
resolve: Track whole parent scope in the visitors
Instead of tracking current module and other components separately. (`ParentScope` includes the module as a component.)
1 parent ff85d1c commit df03e42

File tree

4 files changed

+78
-96
lines changed

4 files changed

+78
-96
lines changed

src/librustc_resolve/build_reduced_graph.rs

Lines changed: 40 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -332,7 +332,8 @@ impl<'a> Resolver<'a> {
332332

333333
impl<'a> BuildReducedGraphVisitor<'_, 'a> {
334334
/// Constructs the reduced graph for one item.
335-
fn build_reduced_graph_for_item(&mut self, item: &Item, parent_scope: &ParentScope<'a>) {
335+
fn build_reduced_graph_for_item(&mut self, item: &Item) {
336+
let parent_scope = &self.parent_scope.clone();
336337
let parent = parent_scope.module;
337338
let expansion = parent_scope.expansion;
338339
let ident = item.ident.gensym_if_underscore();
@@ -439,7 +440,7 @@ impl<'a> BuildReducedGraphVisitor<'_, 'a> {
439440
self.module_map.insert(def_id, module);
440441

441442
// Descend into the module.
442-
self.current_module = module;
443+
self.parent_scope.module = module;
443444
}
444445

445446
// Handled in `rustc_metadata::{native_libs,link_args}`
@@ -563,7 +564,7 @@ impl<'a> BuildReducedGraphVisitor<'_, 'a> {
563564
expansion,
564565
item.span);
565566
self.define(parent, ident, TypeNS, (module, vis, sp, expansion));
566-
self.current_module = module;
567+
self.parent_scope.module = module;
567568
}
568569

569570
ItemKind::MacroDef(..) | ItemKind::Mac(_) => unreachable!(),
@@ -605,7 +606,7 @@ impl<'a> BuildReducedGraphVisitor<'_, 'a> {
605606
}
606607

607608
/// Constructs the reduced graph for one foreign item.
608-
fn build_reduced_graph_for_foreign_item(&mut self, item: &ForeignItem, expn_id: ExpnId) {
609+
fn build_reduced_graph_for_foreign_item(&mut self, item: &ForeignItem) {
609610
let (res, ns) = match item.node {
610611
ForeignItemKind::Fn(..) => {
611612
(Res::Def(DefKind::Fn, self.definitions.local_def_id(item.id)), ValueNS)
@@ -618,27 +619,23 @@ impl<'a> BuildReducedGraphVisitor<'_, 'a> {
618619
}
619620
ForeignItemKind::Macro(_) => unreachable!(),
620621
};
621-
let parent = self.current_module;
622-
let parent_scope = &ParentScope {
623-
module: self.current_module,
624-
expansion: self.expansion,
625-
legacy: self.current_legacy_scope,
626-
derives: Vec::new(),
627-
};
628-
let vis = self.resolver.resolve_visibility(&item.vis, parent_scope);
629-
self.define(parent, item.ident, ns, (res, vis, item.span, expn_id));
622+
let parent = self.parent_scope.module;
623+
let expansion = self.parent_scope.expansion;
624+
let vis = self.resolver.resolve_visibility(&item.vis, &self.parent_scope);
625+
self.define(parent, item.ident, ns, (res, vis, item.span, expansion));
630626
}
631627

632-
fn build_reduced_graph_for_block(&mut self, block: &Block, expn_id: ExpnId) {
633-
let parent = self.current_module;
628+
fn build_reduced_graph_for_block(&mut self, block: &Block) {
629+
let parent = self.parent_scope.module;
630+
let expansion = self.parent_scope.expansion;
634631
if self.block_needs_anonymous_module(block) {
635632
let module = self.new_module(parent,
636633
ModuleKind::Block(block.id),
637634
parent.normal_ancestor_id,
638-
expn_id,
635+
expansion,
639636
block.span);
640637
self.block_map.insert(block.id, module);
641-
self.current_module = module; // Descend into the block.
638+
self.parent_scope.module = module; // Descend into the block.
642639
}
643640
}
644641
}
@@ -827,7 +824,7 @@ impl<'a> BuildReducedGraphVisitor<'_, 'a> {
827824
let mut single_imports = Vec::new();
828825
for attr in &item.attrs {
829826
if attr.check_name(sym::macro_use) {
830-
if self.current_module.parent.is_some() {
827+
if self.parent_scope.module.parent.is_some() {
831828
span_err!(self.session, item.span, E0468,
832829
"an `extern crate` loading macros must be at the crate root");
833830
}
@@ -933,9 +930,7 @@ impl<'a> BuildReducedGraphVisitor<'_, 'a> {
933930

934931
pub struct BuildReducedGraphVisitor<'a, 'b> {
935932
pub resolver: &'a mut Resolver<'b>,
936-
pub current_module: Module<'b>,
937-
pub current_legacy_scope: LegacyScope<'b>,
938-
pub expansion: ExpnId,
933+
pub parent_scope: ParentScope<'b>,
939934
}
940935

941936
impl<'b> Deref for BuildReducedGraphVisitor<'_, 'b> {
@@ -955,11 +950,11 @@ impl<'a, 'b> BuildReducedGraphVisitor<'a, 'b> {
955950
fn visit_invoc(&mut self, id: ast::NodeId) -> &'b InvocationData<'b> {
956951
let invoc_id = id.placeholder_to_expn_id();
957952

958-
self.current_module.unresolved_invocations.borrow_mut().insert(invoc_id);
953+
self.parent_scope.module.unresolved_invocations.borrow_mut().insert(invoc_id);
959954

960955
let invocation_data = self.arenas.alloc_invocation_data(InvocationData {
961-
module: self.current_module,
962-
parent_legacy_scope: self.current_legacy_scope,
956+
module: self.parent_scope.module,
957+
parent_legacy_scope: self.parent_scope.legacy,
963958
output_legacy_scope: Cell::new(None),
964959
});
965960
let old_invocation_data = self.invocations.insert(invoc_id, invocation_data);
@@ -988,38 +983,32 @@ impl<'a, 'b> Visitor<'a> for BuildReducedGraphVisitor<'a, 'b> {
988983
method!(visit_ty: ast::Ty, ast::TyKind::Mac, walk_ty);
989984

990985
fn visit_item(&mut self, item: &'a Item) {
991-
let parent_scope = &ParentScope {
992-
module: self.current_module,
993-
expansion: self.expansion,
994-
legacy: self.current_legacy_scope,
995-
derives: Vec::new(),
996-
};
997986
let macro_use = match item.node {
998987
ItemKind::MacroDef(..) => {
999-
self.current_legacy_scope = self.resolver.define_macro(item, parent_scope);
988+
self.parent_scope.legacy = self.resolver.define_macro(item, &self.parent_scope);
1000989
return
1001990
}
1002991
ItemKind::Mac(..) => {
1003-
self.current_legacy_scope = LegacyScope::Invocation(self.visit_invoc(item.id));
992+
self.parent_scope.legacy = LegacyScope::Invocation(self.visit_invoc(item.id));
1004993
return
1005994
}
1006995
ItemKind::Mod(..) => self.contains_macro_use(&item.attrs),
1007996
_ => false,
1008997
};
1009998

1010-
let orig_current_module = self.current_module;
1011-
let orig_current_legacy_scope = self.current_legacy_scope;
1012-
self.build_reduced_graph_for_item(item, parent_scope);
999+
let orig_current_module = self.parent_scope.module;
1000+
let orig_current_legacy_scope = self.parent_scope.legacy;
1001+
self.build_reduced_graph_for_item(item);
10131002
visit::walk_item(self, item);
1014-
self.current_module = orig_current_module;
1003+
self.parent_scope.module = orig_current_module;
10151004
if !macro_use {
1016-
self.current_legacy_scope = orig_current_legacy_scope;
1005+
self.parent_scope.legacy = orig_current_legacy_scope;
10171006
}
10181007
}
10191008

10201009
fn visit_stmt(&mut self, stmt: &'a ast::Stmt) {
10211010
if let ast::StmtKind::Mac(..) = stmt.node {
1022-
self.current_legacy_scope = LegacyScope::Invocation(self.visit_invoc(stmt.id));
1011+
self.parent_scope.legacy = LegacyScope::Invocation(self.visit_invoc(stmt.id));
10231012
} else {
10241013
visit::walk_stmt(self, stmt);
10251014
}
@@ -1031,21 +1020,21 @@ impl<'a, 'b> Visitor<'a> for BuildReducedGraphVisitor<'a, 'b> {
10311020
return;
10321021
}
10331022

1034-
self.build_reduced_graph_for_foreign_item(foreign_item, self.expansion);
1023+
self.build_reduced_graph_for_foreign_item(foreign_item);
10351024
visit::walk_foreign_item(self, foreign_item);
10361025
}
10371026

10381027
fn visit_block(&mut self, block: &'a Block) {
1039-
let orig_current_module = self.current_module;
1040-
let orig_current_legacy_scope = self.current_legacy_scope;
1041-
self.build_reduced_graph_for_block(block, self.expansion);
1028+
let orig_current_module = self.parent_scope.module;
1029+
let orig_current_legacy_scope = self.parent_scope.legacy;
1030+
self.build_reduced_graph_for_block(block);
10421031
visit::walk_block(self, block);
1043-
self.current_module = orig_current_module;
1044-
self.current_legacy_scope = orig_current_legacy_scope;
1032+
self.parent_scope.module = orig_current_module;
1033+
self.parent_scope.legacy = orig_current_legacy_scope;
10451034
}
10461035

10471036
fn visit_trait_item(&mut self, item: &'a TraitItem) {
1048-
let parent = self.current_module;
1037+
let parent = self.parent_scope.module;
10491038

10501039
if let TraitItemKind::Macro(_) = item.node {
10511040
self.visit_invoc(item.id);
@@ -1067,11 +1056,12 @@ impl<'a, 'b> Visitor<'a> for BuildReducedGraphVisitor<'a, 'b> {
10671056
};
10681057

10691058
let vis = ty::Visibility::Public;
1070-
self.resolver.define(parent, item.ident, ns, (res, vis, item.span, self.expansion));
1059+
let expansion = self.parent_scope.expansion;
1060+
self.resolver.define(parent, item.ident, ns, (res, vis, item.span, expansion));
10711061

1072-
self.current_module = parent.parent.unwrap(); // nearest normal ancestor
1062+
self.parent_scope.module = parent.parent.unwrap(); // nearest normal ancestor
10731063
visit::walk_trait_item(self, item);
1074-
self.current_module = parent;
1064+
self.parent_scope.module = parent;
10751065
}
10761066

10771067
fn visit_token(&mut self, t: Token) {
@@ -1086,15 +1076,8 @@ impl<'a, 'b> Visitor<'a> for BuildReducedGraphVisitor<'a, 'b> {
10861076

10871077
fn visit_attribute(&mut self, attr: &'a ast::Attribute) {
10881078
if !attr.is_sugared_doc && is_builtin_attr(attr) {
1089-
let parent_scope = ParentScope {
1090-
module: self.current_module.nearest_item_scope(),
1091-
expansion: self.expansion,
1092-
legacy: self.current_legacy_scope,
1093-
// Let's hope discerning built-in attributes from derive helpers is not necessary
1094-
derives: Vec::new(),
1095-
};
1096-
parent_scope.module.builtin_attrs.borrow_mut().push((
1097-
attr.path.segments[0].ident, parent_scope
1079+
self.parent_scope.module.builtin_attrs.borrow_mut().push((
1080+
attr.path.segments[0].ident, self.parent_scope.clone()
10981081
));
10991082
}
11001083
visit::walk_attribute(self, attr);

0 commit comments

Comments
 (0)