From edf4329d550423463d7236b26535796ba9f6ef28 Mon Sep 17 00:00:00 2001 From: xmakro Date: Tue, 8 Sep 2026 22:13:15 -0700 Subject: [PATCH] Remember immutable external nonmodule results [skip ci] --- compiler/rustc_resolve/src/build_reduced_graph.rs | 13 +++++++------ compiler/rustc_resolve/src/diagnostics/impls.rs | 7 ++++--- compiler/rustc_resolve/src/lib.rs | 5 +++-- 3 files changed, 14 insertions(+), 11 deletions(-) diff --git a/compiler/rustc_resolve/src/build_reduced_graph.rs b/compiler/rustc_resolve/src/build_reduced_graph.rs index 427861debb102..0a6c362c30b41 100644 --- a/compiler/rustc_resolve/src/build_reduced_graph.rs +++ b/compiler/rustc_resolve/src/build_reduced_graph.rs @@ -115,8 +115,8 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> { match def_id.as_local() { Some(local_def_id) => self.local_module_map.get(&local_def_id).map(|m| m.to_module()), None => { - if let module @ Some(..) = self.extern_module_map.borrow().get(&def_id) { - return module.map(|m| m.to_module()); + if let Some(module) = self.extern_module_map.borrow().get(&def_id) { + return module.map(ExternModule::to_module); } // We need the lock on the extern_module_map for the entire duration of this call. // It is otherwise entirely possible 2 different threads will create and allocate @@ -135,10 +135,10 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> { fn get_extern_module_with_lock( &self, def_id: DefId, - map_lock: &mut RefMut<'_, FxIndexMap>>, + map_lock: &mut RefMut<'_, FxIndexMap>>>, ) -> Option> { - if let module @ Some(..) = map_lock.get(&def_id) { - return module.copied(); + if let Some(module) = map_lock.get(&def_id) { + return *module; } // Query `def_kind` is not used because query system overhead is too expensive here. let def_kind = self.cstore().def_kind_untracked(def_id); @@ -164,10 +164,11 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> { parent.is_some_and(|module| module.no_implicit_prelude), self.arenas, ); - map_lock.insert(def_id, module); + map_lock.insert(def_id, Some(module)); return Some(module); } + map_lock.insert(def_id, None); None } diff --git a/compiler/rustc_resolve/src/diagnostics/impls.rs b/compiler/rustc_resolve/src/diagnostics/impls.rs index 67666a3cbff26..317c62c97fb91 100644 --- a/compiler/rustc_resolve/src/diagnostics/impls.rs +++ b/compiler/rustc_resolve/src/diagnostics/impls.rs @@ -2975,12 +2975,13 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> { .chain( self.extern_module_map .borrow() - .iter() - .filter(|(_, module)| { + .values() + .filter_map(|module| *module) + .filter(|module| { let module = module.to_module(); current_module.is_ancestor_of(module) && current_module != module }) - .flat_map(|(_, module)| module.name()), + .flat_map(|module| module.name()), ) .filter(|c| !c.to_string().is_empty()) .collect::>(); diff --git a/compiler/rustc_resolve/src/lib.rs b/compiler/rustc_resolve/src/lib.rs index 5f18a4036af33..460a1225246a3 100644 --- a/compiler/rustc_resolve/src/lib.rs +++ b/compiler/rustc_resolve/src/lib.rs @@ -1403,8 +1403,9 @@ pub struct Resolver<'ra, 'tcx> { local_modules: Vec>, /// Eagerly populated map of all local non-block modules. local_module_map: FxIndexMap>, - /// Lazily populated cache of modules loaded from external crates. - extern_module_map: CacheRefCell>>, + /// Lazily populated cache of modules loaded from external crates. `None` records an + /// immutable external definition that is not a module. + extern_module_map: CacheRefCell>>>, /// Maps glob imports to the names of items actually imported. glob_map: FxIndexMap>,