Skip to content

Commit

Permalink
Auto merge of #114516 - cjgillot:direct-module-parent, r=compiler-errors
Browse files Browse the repository at this point in the history
parent_module_from_def_id does not need to be a query.

r? `@ghost`
  • Loading branch information
bors committed Aug 6, 2023
2 parents e595409 + 7a51b30 commit 5973bfb
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 21 deletions.
11 changes: 0 additions & 11 deletions compiler/rustc_middle/src/hir/map/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
///
Expand Down
20 changes: 15 additions & 5 deletions compiler/rustc_middle/src/hir/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<ImplSubject<'tcx>> {
Expand All @@ -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;
Expand Down
5 changes: 0 additions & 5 deletions compiler/rustc_middle/src/query/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 5973bfb

Please sign in to comment.