diff --git a/compiler/rustc_middle/src/hir/map/mod.rs b/compiler/rustc_middle/src/hir/map/mod.rs index 0256e09e4b553..2aebb8f541fa9 100644 --- a/compiler/rustc_middle/src/hir/map/mod.rs +++ b/compiler/rustc_middle/src/hir/map/mod.rs @@ -735,17 +735,6 @@ impl<'hir> Map<'hir> { } } - /// Returns the `OwnerId` of `id`'s nearest module parent, or `id` itself if no - /// module parent is in this map. - pub(super) fn get_module_parent_node(self, hir_id: HirId) -> OwnerId { - for (def_id, node) in self.parent_owner_iter(hir_id) { - if let OwnerNode::Item(&Item { kind: ItemKind::Mod(_), .. }) = node { - return def_id; - } - } - CRATE_OWNER_ID - } - /// When on an if expression, a match arm tail expression or a match arm, give back /// the enclosing `if` or `match` expression. /// diff --git a/compiler/rustc_middle/src/hir/mod.rs b/compiler/rustc_middle/src/hir/mod.rs index 45a07fdd29327..06b25556c82a4 100644 --- a/compiler/rustc_middle/src/hir/mod.rs +++ b/compiler/rustc_middle/src/hir/mod.rs @@ -102,7 +102,21 @@ impl<'tcx> TyCtxt<'tcx> { } pub fn parent_module(self, id: HirId) -> LocalDefId { - self.parent_module_from_def_id(id.owner.def_id) + if !id.is_owner() && self.def_kind(id.owner) == DefKind::Mod { + id.owner.def_id + } else { + self.parent_module_from_def_id(id.owner.def_id) + } + } + + pub fn parent_module_from_def_id(self, mut id: LocalDefId) -> LocalDefId { + while let Some(parent) = self.opt_local_parent(id) { + id = parent; + if self.def_kind(id) == DefKind::Mod { + break; + } + } + id } pub fn impl_subject(self, def_id: DefId) -> EarlyBinder> { @@ -120,10 +134,6 @@ impl<'tcx> TyCtxt<'tcx> { } pub fn provide(providers: &mut Providers) { - providers.parent_module_from_def_id = |tcx, id| { - let hir = tcx.hir(); - hir.get_module_parent_node(hir.local_def_id_to_hir_id(id)).def_id - }; providers.hir_crate_items = map::hir_crate_items; providers.crate_hash = map::crate_hash; providers.hir_module_items = map::hir_module_items; diff --git a/compiler/rustc_middle/src/query/mod.rs b/compiler/rustc_middle/src/query/mod.rs index 35fe6ab99fb9f..533c3b66cabde 100644 --- a/compiler/rustc_middle/src/query/mod.rs +++ b/compiler/rustc_middle/src/query/mod.rs @@ -398,11 +398,6 @@ rustc_queries! { desc { "computing `#[expect]`ed lints in this crate" } } - query parent_module_from_def_id(key: LocalDefId) -> LocalDefId { - eval_always - desc { |tcx| "getting the parent module of `{}`", tcx.def_path_str(key) } - } - query expn_that_defined(key: DefId) -> rustc_span::ExpnId { desc { |tcx| "getting the expansion that defined `{}`", tcx.def_path_str(key) } separate_provide_extern