Skip to content

Commit

Permalink
resolve: Use a single common map for local and foreign modules
Browse files Browse the repository at this point in the history
  • Loading branch information
petrochenkov committed Sep 24, 2021
1 parent 1a23858 commit a802188
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 20 deletions.
14 changes: 5 additions & 9 deletions compiler/rustc_resolve/src/build_reduced_graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
}

Expand Down Expand Up @@ -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;
Expand Down
4 changes: 1 addition & 3 deletions compiler/rustc_resolve/src/late.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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| {
Expand Down
6 changes: 2 additions & 4 deletions compiler/rustc_resolve/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,

Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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(),

Expand Down
9 changes: 5 additions & 4 deletions compiler/rustc_resolve/src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -225,21 +225,22 @@ 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),
call_site,
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
Expand Down

0 comments on commit a802188

Please sign in to comment.