Skip to content

Commit a5e8e86

Browse files
committed
hir_owner_parent optimized to inlined call for non-incremental build
1 parent 1e1a394 commit a5e8e86

File tree

2 files changed

+29
-15
lines changed

2 files changed

+29
-15
lines changed

compiler/rustc_middle/src/hir/mod.rs

Lines changed: 28 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,33 @@ impl<'tcx> TyCtxt<'tcx> {
203203
}
204204
})
205205
}
206+
207+
#[inline]
208+
fn hir_owner_parent_impl(self, owner_id: OwnerId) -> HirId {
209+
self.opt_local_parent(owner_id.def_id).map_or(CRATE_HIR_ID, |parent_def_id| {
210+
let parent_owner_id = self.local_def_id_to_hir_id(parent_def_id).owner;
211+
HirId {
212+
owner: parent_owner_id,
213+
local_id: self.hir_crate(()).owners[parent_owner_id.def_id]
214+
.unwrap()
215+
.parenting
216+
.get(&owner_id.def_id)
217+
.copied()
218+
.unwrap_or(ItemLocalId::ZERO),
219+
}
220+
})
221+
}
222+
223+
/// Optimization of `hir_owner_parent` query as an inlined function
224+
/// in case of non-incremental build. The query itself renamed to `hir_owner_parent_q`.
225+
#[inline]
226+
pub fn hir_owner_parent(self, owner_id: OwnerId) -> HirId {
227+
if self.dep_graph.is_fully_enabled() {
228+
self.hir_owner_parent_q(owner_id)
229+
} else {
230+
self.hir_owner_parent_impl(owner_id)
231+
}
232+
}
206233
}
207234

208235
/// Hashes computed by [`TyCtxt::hash_owner_nodes`] if necessary.
@@ -224,20 +251,7 @@ pub fn provide(providers: &mut Providers) {
224251
};
225252
providers.opt_hir_owner_nodes =
226253
|tcx, id| tcx.hir_crate(()).owners.get(id)?.as_owner().map(|i| &i.nodes);
227-
providers.hir_owner_parent = |tcx, owner_id| {
228-
tcx.opt_local_parent(owner_id.def_id).map_or(CRATE_HIR_ID, |parent_def_id| {
229-
let parent_owner_id = tcx.local_def_id_to_hir_id(parent_def_id).owner;
230-
HirId {
231-
owner: parent_owner_id,
232-
local_id: tcx.hir_crate(()).owners[parent_owner_id.def_id]
233-
.unwrap()
234-
.parenting
235-
.get(&owner_id.def_id)
236-
.copied()
237-
.unwrap_or(ItemLocalId::ZERO),
238-
}
239-
})
240-
};
254+
providers.hir_owner_parent_q = |tcx, owner_id| tcx.hir_owner_parent_impl(owner_id);
241255
providers.hir_attr_map = |tcx, id| {
242256
tcx.hir_crate(()).owners[id.def_id].as_owner().map_or(AttributeMap::EMPTY, |o| &o.attrs)
243257
};

compiler/rustc_middle/src/query/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,7 @@ rustc_queries! {
255255
///
256256
/// This can be conveniently accessed by `tcx.hir_*` methods.
257257
/// Avoid calling this query directly.
258-
query hir_owner_parent(key: hir::OwnerId) -> hir::HirId {
258+
query hir_owner_parent_q(key: hir::OwnerId) -> hir::HirId {
259259
desc { |tcx| "getting HIR parent of `{}`", tcx.def_path_str(key) }
260260
}
261261

0 commit comments

Comments
 (0)