Skip to content

Commit d794cb0

Browse files
committed
Introduce a hir_owner_parent query.
1 parent 18bffdb commit d794cb0

File tree

3 files changed

+14
-2
lines changed

3 files changed

+14
-2
lines changed

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

+1-2
Original file line numberDiff line numberDiff line change
@@ -285,8 +285,7 @@ impl<'hir> Map<'hir> {
285285

286286
pub fn find_parent_node(&self, id: HirId) -> Option<HirId> {
287287
if id.local_id == ItemLocalId::from_u32(0) {
288-
let index = self.tcx.index_hir(LOCAL_CRATE);
289-
index.parenting.get(&id.owner).copied()
288+
Some(self.tcx.hir_owner_parent(id.owner))
290289
} else {
291290
let owner = self.tcx.hir_owner_nodes(id.owner)?;
292291
let node = owner.nodes[id.local_id].as_ref()?;

compiler/rustc_middle/src/hir/mod.rs

+4
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,10 @@ pub fn provide(providers: &mut Providers) {
129129
providers.hir_module_items = |tcx, id| &tcx.untracked_crate.modules[&id];
130130
providers.hir_owner = |tcx, id| tcx.index_hir(LOCAL_CRATE).map[id].signature;
131131
providers.hir_owner_nodes = |tcx, id| tcx.index_hir(LOCAL_CRATE).map[id].with_bodies.as_deref();
132+
providers.hir_owner_parent = |tcx, id| {
133+
let index = tcx.index_hir(LOCAL_CRATE);
134+
index.parenting.get(&id).copied().unwrap_or(CRATE_HIR_ID)
135+
};
132136
providers.hir_attrs = |tcx, id| AttributeMap { map: &tcx.untracked_crate.attrs, prefix: id };
133137
providers.def_span = |tcx, def_id| tcx.hir().span_if_local(def_id).unwrap_or(DUMMY_SP);
134138
providers.fn_arg_names = |tcx, id| {

compiler/rustc_middle/src/query/mod.rs

+9
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,15 @@ rustc_queries! {
5252
desc { |tcx| "HIR owner of `{}`", tcx.def_path_str(key.to_def_id()) }
5353
}
5454

55+
/// Gives access to the HIR node's parent for the HIR owner `key`.
56+
///
57+
/// This can be conveniently accessed by methods on `tcx.hir()`.
58+
/// Avoid calling this query directly.
59+
query hir_owner_parent(key: LocalDefId) -> hir::HirId {
60+
eval_always
61+
desc { |tcx| "HIR parent of `{}`", tcx.def_path_str(key.to_def_id()) }
62+
}
63+
5564
/// Gives access to the HIR nodes and bodies inside the HIR owner `key`.
5665
///
5766
/// This can be conveniently accessed by methods on `tcx.hir()`.

0 commit comments

Comments
 (0)