From 8443ea9a14996f7428b188e50dc7c0da97ab8308 Mon Sep 17 00:00:00 2001 From: Vadim Petrochenkov Date: Tue, 14 Sep 2021 00:13:14 +0300 Subject: [PATCH] resolve: Do not cache nearest parent mod in `ModuleData` --- .../rustc_resolve/src/build_reduced_graph.rs | 31 +++--------- compiler/rustc_resolve/src/late.rs | 4 +- compiler/rustc_resolve/src/lib.rs | 50 ++++++++----------- compiler/rustc_resolve/src/macros.rs | 2 +- 4 files changed, 31 insertions(+), 56 deletions(-) diff --git a/compiler/rustc_resolve/src/build_reduced_graph.rs b/compiler/rustc_resolve/src/build_reduced_graph.rs index 55f2b04c4f1c1..32d298e91d5b1 100644 --- a/compiler/rustc_resolve/src/build_reduced_graph.rs +++ b/compiler/rustc_resolve/src/build_reduced_graph.rs @@ -146,7 +146,6 @@ impl<'a> Resolver<'a> { let module = self.arenas.alloc_module(ModuleData::new( parent, kind, - def_id, self.cstore().module_expansion_untracked(def_id, &self.session), self.cstore().get_span_untracked(def_id, &self.session), )); @@ -274,7 +273,7 @@ impl<'a, 'b> BuildReducedGraphVisitor<'a, 'b> { self.r.visibilities[&def_id.expect_local()] } // Otherwise, the visibility is restricted to the nearest parent `mod` item. - _ => ty::Visibility::Restricted(self.parent_scope.module.nearest_parent_mod), + _ => ty::Visibility::Restricted(self.parent_scope.module.nearest_parent_mod()), }) } ast::VisibilityKind::Restricted { ref path, id, .. } => { @@ -773,13 +772,7 @@ impl<'a, 'b> BuildReducedGraphVisitor<'a, 'b> { no_implicit_prelude: parent.no_implicit_prelude || { self.r.session.contains_name(&item.attrs, sym::no_implicit_prelude) }, - ..ModuleData::new( - Some(parent), - module_kind, - def_id, - expansion.to_expn_id(), - item.span, - ) + ..ModuleData::new(Some(parent), module_kind, expansion.to_expn_id(), item.span) }); self.r.define(parent, ident, TypeNS, (module, vis, sp, expansion)); self.r.module_map.insert(local_def_id, module); @@ -814,13 +807,8 @@ impl<'a, 'b> BuildReducedGraphVisitor<'a, 'b> { ItemKind::Enum(_, _) => { let module_kind = ModuleKind::Def(DefKind::Enum, def_id, ident.name); - let module = self.r.new_module( - parent, - module_kind, - parent.nearest_parent_mod, - expansion.to_expn_id(), - item.span, - ); + let module = + self.r.new_module(parent, module_kind, expansion.to_expn_id(), item.span); self.r.define(parent, ident, TypeNS, (module, vis, sp, expansion)); self.parent_scope.module = module; } @@ -889,13 +877,8 @@ impl<'a, 'b> BuildReducedGraphVisitor<'a, 'b> { ItemKind::Trait(..) => { // Add all the items within to a new module. let module_kind = ModuleKind::Def(DefKind::Trait, def_id, ident.name); - let module = self.r.new_module( - parent, - module_kind, - parent.nearest_parent_mod, - expansion.to_expn_id(), - item.span, - ); + let module = + self.r.new_module(parent, module_kind, expansion.to_expn_id(), item.span); self.r.define(parent, ident, TypeNS, (module, vis, sp, expansion)); self.parent_scope.module = module; } @@ -935,7 +918,6 @@ impl<'a, 'b> BuildReducedGraphVisitor<'a, 'b> { let module = self.r.new_module( parent, ModuleKind::Block(block.id), - parent.nearest_parent_mod, expansion.to_expn_id(), block.span, ); @@ -956,7 +938,6 @@ impl<'a, 'b> BuildReducedGraphVisitor<'a, 'b> { let module = self.r.new_module( parent, ModuleKind::Def(kind, def_id, ident.name), - def_id, expansion.to_expn_id(), span, ); diff --git a/compiler/rustc_resolve/src/late.rs b/compiler/rustc_resolve/src/late.rs index c0b52d21fa639..c92353ab7c008 100644 --- a/compiler/rustc_resolve/src/late.rs +++ b/compiler/rustc_resolve/src/late.rs @@ -1872,7 +1872,7 @@ impl<'a: 'ast, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> { if this.should_report_errs() { let (err, candidates) = this.smart_resolve_report_errors(path, span, source, res); - let def_id = this.parent_scope.module.nearest_parent_mod; + let def_id = this.parent_scope.module.nearest_parent_mod(); let instead = res.is_some(); let suggestion = if res.is_none() { this.report_missing_type_error(path) } else { None }; @@ -1940,7 +1940,7 @@ impl<'a: 'ast, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> { drop(parent_err); - let def_id = this.parent_scope.module.nearest_parent_mod; + let def_id = this.parent_scope.module.nearest_parent_mod(); if this.should_report_errs() { this.r.use_injections.push(UseError { diff --git a/compiler/rustc_resolve/src/lib.rs b/compiler/rustc_resolve/src/lib.rs index d76ba80e42eab..017094d7e3b7c 100644 --- a/compiler/rustc_resolve/src/lib.rs +++ b/compiler/rustc_resolve/src/lib.rs @@ -505,10 +505,6 @@ pub struct ModuleData<'a> { /// What kind of module this is, because this may not be a `mod`. kind: ModuleKind, - /// The [`DefId`] of the nearest `mod` item ancestor (which may be this module). - /// This may be the crate root. - nearest_parent_mod: DefId, - /// Mapping between names and their (possibly in-progress) resolutions in this module. /// Resolutions in modules from other crates are not populated until accessed. lazy_resolutions: Resolutions<'a>, @@ -536,19 +532,16 @@ pub struct ModuleData<'a> { type Module<'a> = &'a ModuleData<'a>; impl<'a> ModuleData<'a> { - fn new( - parent: Option>, - kind: ModuleKind, - nearest_parent_mod: DefId, - expansion: ExpnId, - span: Span, - ) -> Self { + fn new(parent: Option>, kind: ModuleKind, expansion: ExpnId, span: Span) -> Self { + let is_foreign = match kind { + ModuleKind::Def(_, def_id, _) => !def_id.is_local(), + ModuleKind::Block(_) => false, + }; ModuleData { parent, kind, - nearest_parent_mod, lazy_resolutions: Default::default(), - populate_on_access: Cell::new(!nearest_parent_mod.is_local()), + populate_on_access: Cell::new(is_foreign), unexpanded_invocations: Default::default(), no_implicit_prelude: false, glob_importers: RefCell::new(Vec::new()), @@ -559,6 +552,13 @@ impl<'a> ModuleData<'a> { } } + fn nearest_parent_mod(&self) -> DefId { + match self.kind { + ModuleKind::Def(DefKind::Mod, def_id, _) => def_id, + _ => self.parent.expect("non-root module without parent").nearest_parent_mod(), + } + } + fn for_each_child(&'a self, resolver: &mut R, mut f: F) where R: AsMut>, @@ -1260,18 +1260,12 @@ impl<'a> Resolver<'a> { let root_module_kind = ModuleKind::Def(DefKind::Mod, root_def_id, kw::Empty); let graph_root = arenas.alloc_module(ModuleData { no_implicit_prelude: session.contains_name(&krate.attrs, sym::no_implicit_prelude), - ..ModuleData::new(None, root_module_kind, root_def_id, ExpnId::root(), krate.span) + ..ModuleData::new(None, root_module_kind, ExpnId::root(), krate.span) }); let empty_module_kind = ModuleKind::Def(DefKind::Mod, root_def_id, kw::Empty); let empty_module = arenas.alloc_module(ModuleData { no_implicit_prelude: true, - ..ModuleData::new( - Some(graph_root), - empty_module_kind, - root_def_id, - ExpnId::root(), - DUMMY_SP, - ) + ..ModuleData::new(Some(graph_root), empty_module_kind, ExpnId::root(), DUMMY_SP) }); let mut module_map = FxHashMap::default(); module_map.insert(root_local_def_id, graph_root); @@ -1636,11 +1630,10 @@ impl<'a> Resolver<'a> { &self, parent: Module<'a>, kind: ModuleKind, - nearest_parent_mod: DefId, expn_id: ExpnId, span: Span, ) -> Module<'a> { - let module = ModuleData::new(Some(parent), kind, nearest_parent_mod, expn_id, span); + let module = ModuleData::new(Some(parent), kind, expn_id, span); self.arenas.alloc_module(module) } @@ -2167,7 +2160,8 @@ impl<'a> Resolver<'a> { return self.graph_root; } }; - let module = self.get_module(DefId { index: CRATE_DEF_INDEX, ..module.nearest_parent_mod }); + let module = + self.get_module(DefId { index: CRATE_DEF_INDEX, ..module.nearest_parent_mod() }); debug!( "resolve_crate_root({:?}): got module {:?} ({:?}) (ident.span = {:?})", ident, @@ -2179,10 +2173,10 @@ impl<'a> Resolver<'a> { } fn resolve_self(&mut self, ctxt: &mut SyntaxContext, module: Module<'a>) -> Module<'a> { - let mut module = self.get_module(module.nearest_parent_mod); + let mut module = self.get_module(module.nearest_parent_mod()); while module.span.ctxt().normalize_to_macros_2_0() != *ctxt { let parent = module.parent.unwrap_or_else(|| self.macro_def_scope(ctxt.remove_mark())); - module = self.get_module(parent.nearest_parent_mod); + module = self.get_module(parent.nearest_parent_mod()); } module } @@ -2896,7 +2890,7 @@ impl<'a> Resolver<'a> { } fn is_accessible_from(&self, vis: ty::Visibility, module: Module<'a>) -> bool { - vis.is_accessible_from(module.nearest_parent_mod, self) + vis.is_accessible_from(module.nearest_parent_mod(), self) } fn set_binding_parent_module(&mut self, binding: &'a NameBinding<'a>, module: Module<'a>) { @@ -2920,7 +2914,7 @@ impl<'a> Resolver<'a> { self.binding_parent_modules.get(&PtrKey(modularized)), ) { (Some(macro_rules), Some(modularized)) => { - macro_rules.nearest_parent_mod == modularized.nearest_parent_mod + macro_rules.nearest_parent_mod() == modularized.nearest_parent_mod() && modularized.is_ancestor_of(macro_rules) } _ => false, diff --git a/compiler/rustc_resolve/src/macros.rs b/compiler/rustc_resolve/src/macros.rs index 4dbac47c3cc8e..5624f8bdb8579 100644 --- a/compiler/rustc_resolve/src/macros.rs +++ b/compiler/rustc_resolve/src/macros.rs @@ -305,7 +305,7 @@ impl<'a> ResolverExpand for Resolver<'a> { fast_print_path(path), res.opt_def_id(), res.opt_def_id().map(|macro_def_id| { - self.macro_def_scope_from_def_id(macro_def_id).nearest_parent_mod + self.macro_def_scope_from_def_id(macro_def_id).nearest_parent_mod() }), ), self.create_stable_hashing_context(),