Skip to content

Commit

Permalink
resolve: Cleanup module allocation
Browse files Browse the repository at this point in the history
Construction of all modules is now centralized and performed by `fn new_module`.
  • Loading branch information
petrochenkov committed Sep 24, 2021
1 parent fd58eea commit 6e9adcb
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 56 deletions.
63 changes: 37 additions & 26 deletions compiler/rustc_resolve/src/build_reduced_graph.rs
Expand Up @@ -9,11 +9,9 @@ use crate::def_collector::collect_definitions;
use crate::imports::{Import, ImportKind};
use crate::macros::{MacroRulesBinding, MacroRulesScope, MacroRulesScopeRef};
use crate::Namespace::{self, MacroNS, TypeNS, ValueNS};
use crate::{CrateLint, Determinacy, PathResult, ResolutionError, VisResolutionError};
use crate::{
ExternPreludeEntry, ModuleOrUniformRoot, ParentScope, PerNS, Resolver, ResolverArenas,
};
use crate::{Module, ModuleData, ModuleKind, NameBinding, NameBindingKind, Segment, ToNameBinding};
use crate::{CrateLint, Determinacy, ExternPreludeEntry, Module, ModuleKind, ModuleOrUniformRoot};
use crate::{NameBinding, NameBindingKind, ParentScope, PathResult, PerNS, ResolutionError};
use crate::{Resolver, ResolverArenas, Segment, ToNameBinding, VisResolutionError};

use rustc_ast::visit::{self, AssocCtxt, Visitor};
use rustc_ast::{self as ast, AssocItem, AssocItemKind, MetaItemKind, StmtKind};
Expand Down Expand Up @@ -142,13 +140,14 @@ impl<'a> Resolver<'a> {
};

// Allocate and return a new module with the information we found
let kind = ModuleKind::Def(DefKind::Mod, def_id, name);
let module = self.arenas.alloc_module(ModuleData::new(
let module = self.arenas.new_module(
parent,
kind,
ModuleKind::Def(DefKind::Mod, def_id, name),
self.cstore().module_expansion_untracked(def_id, &self.session),
self.cstore().get_span_untracked(def_id, &self.session),
));
// FIXME: Account for `#[no_implicit_prelude]` attributes.
parent.map_or(false, |module| module.no_implicit_prelude),
);
self.extern_module_map.insert(def_id, module);
module
}
Expand Down Expand Up @@ -767,13 +766,14 @@ impl<'a, 'b> BuildReducedGraphVisitor<'a, 'b> {
}

ItemKind::Mod(..) => {
let module_kind = ModuleKind::Def(DefKind::Mod, def_id, ident.name);
let module = self.r.arenas.alloc_module(ModuleData {
no_implicit_prelude: parent.no_implicit_prelude || {
self.r.session.contains_name(&item.attrs, sym::no_implicit_prelude)
},
..ModuleData::new(Some(parent), module_kind, expansion.to_expn_id(), item.span)
});
let module = self.r.arenas.new_module(
Some(parent),
ModuleKind::Def(DefKind::Mod, def_id, ident.name),
expansion.to_expn_id(),
item.span,
parent.no_implicit_prelude
|| self.r.session.contains_name(&item.attrs, sym::no_implicit_prelude),
);
self.r.define(parent, ident, TypeNS, (module, vis, sp, expansion));
self.r.module_map.insert(local_def_id, module);

Expand Down Expand Up @@ -806,9 +806,13 @@ 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, expansion.to_expn_id(), item.span);
let module = self.r.arenas.new_module(
Some(parent),
ModuleKind::Def(DefKind::Enum, def_id, ident.name),
expansion.to_expn_id(),
item.span,
parent.no_implicit_prelude,
);
self.r.define(parent, ident, TypeNS, (module, vis, sp, expansion));
self.parent_scope.module = module;
}
Expand Down Expand Up @@ -876,9 +880,13 @@ 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, expansion.to_expn_id(), item.span);
let module = self.r.arenas.new_module(
Some(parent),
ModuleKind::Def(DefKind::Trait, def_id, ident.name),
expansion.to_expn_id(),
item.span,
parent.no_implicit_prelude,
);
self.r.define(parent, ident, TypeNS, (module, vis, sp, expansion));
self.parent_scope.module = module;
}
Expand Down Expand Up @@ -915,11 +923,12 @@ impl<'a, 'b> BuildReducedGraphVisitor<'a, 'b> {
let parent = self.parent_scope.module;
let expansion = self.parent_scope.expansion;
if self.block_needs_anonymous_module(block) {
let module = self.r.new_module(
parent,
let module = self.r.arenas.new_module(
Some(parent),
ModuleKind::Block(block.id),
expansion.to_expn_id(),
block.span,
parent.no_implicit_prelude,
);
self.r.block_map.insert(block.id, module);
self.parent_scope.module = module; // Descend into the block.
Expand All @@ -935,11 +944,13 @@ impl<'a, 'b> BuildReducedGraphVisitor<'a, 'b> {
// Record primary definitions.
match res {
Res::Def(kind @ (DefKind::Mod | DefKind::Enum | DefKind::Trait), def_id) => {
let module = self.r.new_module(
parent,
let module = self.r.arenas.new_module(
Some(parent),
ModuleKind::Def(kind, def_id, ident.name),
expansion.to_expn_id(),
span,
// FIXME: Account for `#[no_implicit_prelude]` attributes.
parent.no_implicit_prelude,
);
self.r.define(parent, ident, TypeNS, (module, vis, span, expansion));
}
Expand Down
66 changes: 36 additions & 30 deletions compiler/rustc_resolve/src/lib.rs
Expand Up @@ -41,7 +41,7 @@ use rustc_expand::base::{DeriveResolutions, SyntaxExtension, SyntaxExtensionKind
use rustc_hir::def::Namespace::*;
use rustc_hir::def::{self, CtorOf, DefKind, NonMacroAttrKind, PartialRes};
use rustc_hir::def_id::{CrateNum, DefId, DefIdMap, DefPathHash, LocalDefId};
use rustc_hir::def_id::{CRATE_DEF_INDEX, LOCAL_CRATE};
use rustc_hir::def_id::{CRATE_DEF_ID, CRATE_DEF_INDEX, LOCAL_CRATE};
use rustc_hir::definitions::{DefKey, DefPathData, Definitions};
use rustc_hir::TraitCandidate;
use rustc_index::vec::IndexVec;
Expand Down Expand Up @@ -533,7 +533,13 @@ pub struct ModuleData<'a> {
type Module<'a> = &'a ModuleData<'a>;

impl<'a> ModuleData<'a> {
fn new(parent: Option<Module<'a>>, kind: ModuleKind, expansion: ExpnId, span: Span) -> Self {
fn new(
parent: Option<Module<'a>>,
kind: ModuleKind,
expansion: ExpnId,
span: Span,
no_implicit_prelude: bool,
) -> Self {
let is_foreign = match kind {
ModuleKind::Def(_, def_id, _) => !def_id.is_local(),
ModuleKind::Block(_) => false,
Expand All @@ -544,7 +550,7 @@ impl<'a> ModuleData<'a> {
lazy_resolutions: Default::default(),
populate_on_access: Cell::new(is_foreign),
unexpanded_invocations: Default::default(),
no_implicit_prelude: false,
no_implicit_prelude,
glob_importers: RefCell::new(Vec::new()),
globs: RefCell::new(Vec::new()),
traits: RefCell::new(None),
Expand Down Expand Up @@ -1055,8 +1061,16 @@ pub struct ResolverArenas<'a> {
}

impl<'a> ResolverArenas<'a> {
fn alloc_module(&'a self, module: ModuleData<'a>) -> Module<'a> {
let module = self.modules.alloc(module);
fn new_module(
&'a self,
parent: Option<Module<'a>>,
kind: ModuleKind,
expn_id: ExpnId,
span: Span,
no_implicit_prelude: bool,
) -> Module<'a> {
let module =
self.modules.alloc(ModuleData::new(parent, kind, expn_id, span, no_implicit_prelude));
if module.def_id().map_or(true, |def_id| def_id.is_local()) {
self.local_modules.borrow_mut().push(module);
}
Expand Down Expand Up @@ -1258,26 +1272,29 @@ impl<'a> Resolver<'a> {
metadata_loader: Box<MetadataLoaderDyn>,
arenas: &'a ResolverArenas<'a>,
) -> Resolver<'a> {
let root_local_def_id = LocalDefId { local_def_index: CRATE_DEF_INDEX };
let root_def_id = root_local_def_id.to_def_id();
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, 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, ExpnId::root(), DUMMY_SP)
});
let root_def_id = CRATE_DEF_ID.to_def_id();
let graph_root = arenas.new_module(
None,
ModuleKind::Def(DefKind::Mod, root_def_id, kw::Empty),
ExpnId::root(),
krate.span,
session.contains_name(&krate.attrs, sym::no_implicit_prelude),
);
let empty_module = arenas.new_module(
None,
ModuleKind::Def(DefKind::Mod, root_def_id, kw::Empty),
ExpnId::root(),
DUMMY_SP,
true,
);
let mut module_map = FxHashMap::default();
module_map.insert(root_local_def_id, graph_root);
module_map.insert(CRATE_DEF_ID, graph_root);

let definitions = Definitions::new(session.local_stable_crate_id(), krate.span);
let root = definitions.get_root_def();

let mut visibilities = FxHashMap::default();
visibilities.insert(root_local_def_id, ty::Visibility::Public);
visibilities.insert(CRATE_DEF_ID, ty::Visibility::Public);

let mut def_id_to_node_id = IndexVec::default();
assert_eq!(def_id_to_node_id.push(CRATE_NODE_ID), root);
Expand Down Expand Up @@ -1629,17 +1646,6 @@ impl<'a> Resolver<'a> {
import_ids
}

fn new_module(
&self,
parent: Module<'a>,
kind: ModuleKind,
expn_id: ExpnId,
span: Span,
) -> Module<'a> {
let module = ModuleData::new(Some(parent), kind, expn_id, span);
self.arenas.alloc_module(module)
}

fn new_key(&mut self, ident: Ident, ns: Namespace) -> BindingKey {
let ident = ident.normalize_to_macros_2_0();
let disambiguator = if ident.name == kw::Underscore {
Expand Down

0 comments on commit 6e9adcb

Please sign in to comment.