resolve: Use a single common map for local and foreign modules

This commit is contained in:
Vadim Petrochenkov 2021-09-12 01:59:05 +03:00
parent 1a23858983
commit a8021888c8
4 changed files with 13 additions and 20 deletions

View File

@ -114,16 +114,12 @@ impl<'a> Resolver<'a> {
}
pub fn get_module(&mut self, def_id: DefId) -> Module<'a> {
// If this is a local module, it will be in `module_map`, no need to recalculate it.
if let Some(def_id) = def_id.as_local() {
return self.module_map[&def_id];
}
// Cache module resolution
if let Some(&module) = self.extern_module_map.get(&def_id) {
return module;
if let Some(module) = self.module_map.get(&def_id) {
return *module;
}
assert!(!def_id.is_local());
let (name, parent) = if def_id.index == CRATE_DEF_INDEX {
// This is the crate root
(self.cstore().crate_name(def_id.krate), None)
@ -148,7 +144,7 @@ impl<'a> Resolver<'a> {
// FIXME: Account for `#[no_implicit_prelude]` attributes.
parent.map_or(false, |module| module.no_implicit_prelude),
);
self.extern_module_map.insert(def_id, module);
self.module_map.insert(def_id, module);
module
}
@ -772,7 +768,7 @@ impl<'a, 'b> BuildReducedGraphVisitor<'a, 'b> {
|| 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);
self.r.module_map.insert(def_id, module);
// Descend into the module.
self.parent_scope.module = module;

View File

@ -799,9 +799,7 @@ impl<'a: 'ast, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> {
}
fn with_scope<T>(&mut self, id: NodeId, f: impl FnOnce(&mut Self) -> T) -> T {
let id = self.r.local_def_id(id);
let module = self.r.module_map.get(&id).cloned(); // clones a reference
if let Some(module) = module {
if let Some(module) = self.r.module_map.get(&self.r.local_def_id(id).to_def_id()).copied() {
// Move down in the graph.
let orig_module = replace(&mut self.parent_scope.module, module);
self.with_rib(ValueNS, ModuleRibKind(module), |this| {

View File

@ -943,8 +943,7 @@ pub struct Resolver<'a> {
/// some AST passes can generate identifiers that only resolve to local or
/// language items.
empty_module: Module<'a>,
module_map: FxHashMap<LocalDefId, Module<'a>>,
extern_module_map: FxHashMap<DefId, Module<'a>>,
module_map: FxHashMap<DefId, Module<'a>>,
binding_parent_modules: FxHashMap<PtrKey<'a, NameBinding<'a>>, Module<'a>>,
underscore_disambiguator: u32,
@ -1288,7 +1287,7 @@ impl<'a> Resolver<'a> {
true,
);
let mut module_map = FxHashMap::default();
module_map.insert(CRATE_DEF_ID, graph_root);
module_map.insert(root_def_id, graph_root);
let definitions = Definitions::new(session.local_stable_crate_id(), krate.span);
let root = definitions.get_root_def();
@ -1355,7 +1354,6 @@ impl<'a> Resolver<'a> {
empty_module,
module_map,
block_map: Default::default(),
extern_module_map: FxHashMap::default(),
binding_parent_modules: FxHashMap::default(),
ast_transform_scopes: FxHashMap::default(),

View File

@ -225,7 +225,8 @@ impl<'a> ResolverExpand for Resolver<'a> {
features: &[Symbol],
parent_module_id: Option<NodeId>,
) -> LocalExpnId {
let parent_module = parent_module_id.map(|module_id| self.local_def_id(module_id));
let parent_module =
parent_module_id.map(|module_id| self.local_def_id(module_id).to_def_id());
let expn_id = LocalExpnId::fresh(
ExpnData::allow_unstable(
ExpnKind::AstPass(pass),
@ -233,13 +234,13 @@ impl<'a> ResolverExpand for Resolver<'a> {
self.session.edition(),
features.into(),
None,
parent_module.map(LocalDefId::to_def_id),
parent_module,
),
self.create_stable_hashing_context(),
);
let parent_scope = parent_module
.map_or(self.empty_module, |parent_def_id| self.module_map[&parent_def_id]);
let parent_scope =
parent_module.map_or(self.empty_module, |def_id| self.get_module(def_id));
self.ast_transform_scopes.insert(expn_id, parent_scope);
expn_id