From 36e19b3ba149c60acfa802c68fac590ea1550607 Mon Sep 17 00:00:00 2001 From: xmakro Date: Tue, 8 Sep 2026 20:37:14 -0700 Subject: [PATCH] Index names in immutable external trait tables [skip ci] --- compiler/rustc_resolve/src/lib.rs | 23 +++++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/compiler/rustc_resolve/src/lib.rs b/compiler/rustc_resolve/src/lib.rs index 5f18a4036af33..974ee55f79403 100644 --- a/compiler/rustc_resolve/src/lib.rs +++ b/compiler/rustc_resolve/src/lib.rs @@ -683,6 +683,7 @@ struct ModuleData<'ra> { /// Mapping between names and their (possibly in-progress) resolutions in this module. /// Resolutions in modules from other crates are not populated until accessed. lazy_resolutions: Resolutions<'ra>, + external_trait_item_names: OnceLock>, /// Used to disambiguate underscore items (`const _: T = ...`) in the module. underscore_disambiguator: CmCell, @@ -748,6 +749,7 @@ impl<'ra> ModuleData<'ra> { parent, kind, lazy_resolutions, + external_trait_item_names: OnceLock::new(), underscore_disambiguator: CmCell::new(0), unexpanded_invocations: Default::default(), no_implicit_prelude, @@ -2168,10 +2170,23 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> { assoc_item: Option<(Symbol, Namespace)>, ) -> bool { match (trait_module, assoc_item) { - (Some(trait_module), Some((name, ns))) => self - .resolutions(trait_module) - .iter() - .any(|(key, _name_resolution)| key.ns == ns && key.ident.name == name), + (Some(trait_module), Some((name, ns))) => { + if !trait_module.is_local() { + // External tables are immutable after their lazy initialization. + // Preserve this filter's deliberate disregard for syntax context. + let names = trait_module.external_trait_item_names.get_or_init(|| { + self.resolutions(trait_module) + .keys() + .map(|key| (key.ident.name, key.ns)) + .collect() + }); + names.contains(&(name, ns)) + } else { + self.resolutions(trait_module) + .iter() + .any(|(key, _)| key.ns == ns && key.ident.name == name) + } + } _ => true, } }