Skip to content

Commit a802188

Browse files
committed
resolve: Use a single common map for local and foreign modules
1 parent 1a23858 commit a802188

File tree

4 files changed

+13
-20
lines changed

4 files changed

+13
-20
lines changed

compiler/rustc_resolve/src/build_reduced_graph.rs

+5-9
Original file line numberDiff line numberDiff line change
@@ -114,16 +114,12 @@ impl<'a> Resolver<'a> {
114114
}
115115

116116
pub fn get_module(&mut self, def_id: DefId) -> Module<'a> {
117-
// If this is a local module, it will be in `module_map`, no need to recalculate it.
118-
if let Some(def_id) = def_id.as_local() {
119-
return self.module_map[&def_id];
120-
}
121-
122117
// Cache module resolution
123-
if let Some(&module) = self.extern_module_map.get(&def_id) {
124-
return module;
118+
if let Some(module) = self.module_map.get(&def_id) {
119+
return *module;
125120
}
126121

122+
assert!(!def_id.is_local());
127123
let (name, parent) = if def_id.index == CRATE_DEF_INDEX {
128124
// This is the crate root
129125
(self.cstore().crate_name(def_id.krate), None)
@@ -148,7 +144,7 @@ impl<'a> Resolver<'a> {
148144
// FIXME: Account for `#[no_implicit_prelude]` attributes.
149145
parent.map_or(false, |module| module.no_implicit_prelude),
150146
);
151-
self.extern_module_map.insert(def_id, module);
147+
self.module_map.insert(def_id, module);
152148
module
153149
}
154150

@@ -772,7 +768,7 @@ impl<'a, 'b> BuildReducedGraphVisitor<'a, 'b> {
772768
|| self.r.session.contains_name(&item.attrs, sym::no_implicit_prelude),
773769
);
774770
self.r.define(parent, ident, TypeNS, (module, vis, sp, expansion));
775-
self.r.module_map.insert(local_def_id, module);
771+
self.r.module_map.insert(def_id, module);
776772

777773
// Descend into the module.
778774
self.parent_scope.module = module;

compiler/rustc_resolve/src/late.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -799,9 +799,7 @@ impl<'a: 'ast, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> {
799799
}
800800

801801
fn with_scope<T>(&mut self, id: NodeId, f: impl FnOnce(&mut Self) -> T) -> T {
802-
let id = self.r.local_def_id(id);
803-
let module = self.r.module_map.get(&id).cloned(); // clones a reference
804-
if let Some(module) = module {
802+
if let Some(module) = self.r.module_map.get(&self.r.local_def_id(id).to_def_id()).copied() {
805803
// Move down in the graph.
806804
let orig_module = replace(&mut self.parent_scope.module, module);
807805
self.with_rib(ValueNS, ModuleRibKind(module), |this| {

compiler/rustc_resolve/src/lib.rs

+2-4
Original file line numberDiff line numberDiff line change
@@ -943,8 +943,7 @@ pub struct Resolver<'a> {
943943
/// some AST passes can generate identifiers that only resolve to local or
944944
/// language items.
945945
empty_module: Module<'a>,
946-
module_map: FxHashMap<LocalDefId, Module<'a>>,
947-
extern_module_map: FxHashMap<DefId, Module<'a>>,
946+
module_map: FxHashMap<DefId, Module<'a>>,
948947
binding_parent_modules: FxHashMap<PtrKey<'a, NameBinding<'a>>, Module<'a>>,
949948
underscore_disambiguator: u32,
950949

@@ -1288,7 +1287,7 @@ impl<'a> Resolver<'a> {
12881287
true,
12891288
);
12901289
let mut module_map = FxHashMap::default();
1291-
module_map.insert(CRATE_DEF_ID, graph_root);
1290+
module_map.insert(root_def_id, graph_root);
12921291

12931292
let definitions = Definitions::new(session.local_stable_crate_id(), krate.span);
12941293
let root = definitions.get_root_def();
@@ -1355,7 +1354,6 @@ impl<'a> Resolver<'a> {
13551354
empty_module,
13561355
module_map,
13571356
block_map: Default::default(),
1358-
extern_module_map: FxHashMap::default(),
13591357
binding_parent_modules: FxHashMap::default(),
13601358
ast_transform_scopes: FxHashMap::default(),
13611359

compiler/rustc_resolve/src/macros.rs

+5-4
Original file line numberDiff line numberDiff line change
@@ -225,21 +225,22 @@ impl<'a> ResolverExpand for Resolver<'a> {
225225
features: &[Symbol],
226226
parent_module_id: Option<NodeId>,
227227
) -> LocalExpnId {
228-
let parent_module = parent_module_id.map(|module_id| self.local_def_id(module_id));
228+
let parent_module =
229+
parent_module_id.map(|module_id| self.local_def_id(module_id).to_def_id());
229230
let expn_id = LocalExpnId::fresh(
230231
ExpnData::allow_unstable(
231232
ExpnKind::AstPass(pass),
232233
call_site,
233234
self.session.edition(),
234235
features.into(),
235236
None,
236-
parent_module.map(LocalDefId::to_def_id),
237+
parent_module,
237238
),
238239
self.create_stable_hashing_context(),
239240
);
240241

241-
let parent_scope = parent_module
242-
.map_or(self.empty_module, |parent_def_id| self.module_map[&parent_def_id]);
242+
let parent_scope =
243+
parent_module.map_or(self.empty_module, |def_id| self.get_module(def_id));
243244
self.ast_transform_scopes.insert(expn_id, parent_scope);
244245

245246
expn_id

0 commit comments

Comments
 (0)