Skip to content

Commit 5973bfb

Browse files
committed
Auto merge of rust-lang#114516 - cjgillot:direct-module-parent, r=compiler-errors
parent_module_from_def_id does not need to be a query. r? `@ghost`
2 parents e595409 + 7a51b30 commit 5973bfb

File tree

3 files changed

+15
-21
lines changed

3 files changed

+15
-21
lines changed

compiler/rustc_middle/src/hir/map/mod.rs

-11
Original file line numberDiff line numberDiff line change
@@ -735,17 +735,6 @@ impl<'hir> Map<'hir> {
735735
}
736736
}
737737

738-
/// Returns the `OwnerId` of `id`'s nearest module parent, or `id` itself if no
739-
/// module parent is in this map.
740-
pub(super) fn get_module_parent_node(self, hir_id: HirId) -> OwnerId {
741-
for (def_id, node) in self.parent_owner_iter(hir_id) {
742-
if let OwnerNode::Item(&Item { kind: ItemKind::Mod(_), .. }) = node {
743-
return def_id;
744-
}
745-
}
746-
CRATE_OWNER_ID
747-
}
748-
749738
/// When on an if expression, a match arm tail expression or a match arm, give back
750739
/// the enclosing `if` or `match` expression.
751740
///

compiler/rustc_middle/src/hir/mod.rs

+15-5
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,21 @@ impl<'tcx> TyCtxt<'tcx> {
102102
}
103103

104104
pub fn parent_module(self, id: HirId) -> LocalDefId {
105-
self.parent_module_from_def_id(id.owner.def_id)
105+
if !id.is_owner() && self.def_kind(id.owner) == DefKind::Mod {
106+
id.owner.def_id
107+
} else {
108+
self.parent_module_from_def_id(id.owner.def_id)
109+
}
110+
}
111+
112+
pub fn parent_module_from_def_id(self, mut id: LocalDefId) -> LocalDefId {
113+
while let Some(parent) = self.opt_local_parent(id) {
114+
id = parent;
115+
if self.def_kind(id) == DefKind::Mod {
116+
break;
117+
}
118+
}
119+
id
106120
}
107121

108122
pub fn impl_subject(self, def_id: DefId) -> EarlyBinder<ImplSubject<'tcx>> {
@@ -120,10 +134,6 @@ impl<'tcx> TyCtxt<'tcx> {
120134
}
121135

122136
pub fn provide(providers: &mut Providers) {
123-
providers.parent_module_from_def_id = |tcx, id| {
124-
let hir = tcx.hir();
125-
hir.get_module_parent_node(hir.local_def_id_to_hir_id(id)).def_id
126-
};
127137
providers.hir_crate_items = map::hir_crate_items;
128138
providers.crate_hash = map::crate_hash;
129139
providers.hir_module_items = map::hir_module_items;

compiler/rustc_middle/src/query/mod.rs

-5
Original file line numberDiff line numberDiff line change
@@ -398,11 +398,6 @@ rustc_queries! {
398398
desc { "computing `#[expect]`ed lints in this crate" }
399399
}
400400

401-
query parent_module_from_def_id(key: LocalDefId) -> LocalDefId {
402-
eval_always
403-
desc { |tcx| "getting the parent module of `{}`", tcx.def_path_str(key) }
404-
}
405-
406401
query expn_that_defined(key: DefId) -> rustc_span::ExpnId {
407402
desc { |tcx| "getting the expansion that defined `{}`", tcx.def_path_str(key) }
408403
separate_provide_extern

0 commit comments

Comments
 (0)