Skip to content

Commit

Permalink
Introduce a hir_owner_parent query.
Browse files Browse the repository at this point in the history
  • Loading branch information
cjgillot committed Apr 29, 2021
1 parent 18bffdb commit d794cb0
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 2 deletions.
3 changes: 1 addition & 2 deletions compiler/rustc_middle/src/hir/map/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -285,8 +285,7 @@ impl<'hir> Map<'hir> {

pub fn find_parent_node(&self, id: HirId) -> Option<HirId> {
if id.local_id == ItemLocalId::from_u32(0) {
let index = self.tcx.index_hir(LOCAL_CRATE);
index.parenting.get(&id.owner).copied()
Some(self.tcx.hir_owner_parent(id.owner))
} else {
let owner = self.tcx.hir_owner_nodes(id.owner)?;
let node = owner.nodes[id.local_id].as_ref()?;
Expand Down
4 changes: 4 additions & 0 deletions compiler/rustc_middle/src/hir/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,10 @@ pub fn provide(providers: &mut Providers) {
providers.hir_module_items = |tcx, id| &tcx.untracked_crate.modules[&id];
providers.hir_owner = |tcx, id| tcx.index_hir(LOCAL_CRATE).map[id].signature;
providers.hir_owner_nodes = |tcx, id| tcx.index_hir(LOCAL_CRATE).map[id].with_bodies.as_deref();
providers.hir_owner_parent = |tcx, id| {
let index = tcx.index_hir(LOCAL_CRATE);
index.parenting.get(&id).copied().unwrap_or(CRATE_HIR_ID)
};
providers.hir_attrs = |tcx, id| AttributeMap { map: &tcx.untracked_crate.attrs, prefix: id };
providers.def_span = |tcx, def_id| tcx.hir().span_if_local(def_id).unwrap_or(DUMMY_SP);
providers.fn_arg_names = |tcx, id| {
Expand Down
9 changes: 9 additions & 0 deletions compiler/rustc_middle/src/query/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,15 @@ rustc_queries! {
desc { |tcx| "HIR owner of `{}`", tcx.def_path_str(key.to_def_id()) }
}

/// Gives access to the HIR node's parent for the HIR owner `key`.
///
/// This can be conveniently accessed by methods on `tcx.hir()`.
/// Avoid calling this query directly.
query hir_owner_parent(key: LocalDefId) -> hir::HirId {
eval_always
desc { |tcx| "HIR parent of `{}`", tcx.def_path_str(key.to_def_id()) }
}

/// Gives access to the HIR nodes and bodies inside the HIR owner `key`.
///
/// This can be conveniently accessed by methods on `tcx.hir()`.
Expand Down

0 comments on commit d794cb0

Please sign in to comment.