Skip to content

Commit d17eb78

Browse files
committed
Separate hir_owner query into two queries to avoid using extensive data on incr comp most of the time
1 parent a0bcce4 commit d17eb78

File tree

3 files changed

+29
-16
lines changed

3 files changed

+29
-16
lines changed

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

+15-15
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ impl<'hir> Iterator for ParentOwnerIterator<'hir> {
123123
fn next(&mut self) -> Option<Self::Item> {
124124
if self.current_id.local_id.index() != 0 {
125125
self.current_id.local_id = ItemLocalId::new(0);
126-
if let MaybeOwner::Owner(node) = self.map.tcx.hir_owner(self.current_id.owner) {
126+
if let Some(node) = self.map.tcx.hir_owner(self.current_id.owner) {
127127
return Some((self.current_id.owner, node.node));
128128
}
129129
}
@@ -141,7 +141,7 @@ impl<'hir> Iterator for ParentOwnerIterator<'hir> {
141141
self.current_id = HirId::make_owner(parent_id);
142142

143143
// If this `HirId` doesn't have an entry, skip it and look for its `parent_id`.
144-
if let MaybeOwner::Owner(node) = self.map.tcx.hir_owner(self.current_id.owner) {
144+
if let Some(node) = self.map.tcx.hir_owner(self.current_id.owner) {
145145
return Some((self.current_id.owner, node.node));
146146
}
147147
}
@@ -155,7 +155,7 @@ impl<'hir> Map<'hir> {
155155

156156
pub fn root_module(&self) -> &'hir Mod<'hir> {
157157
match self.tcx.hir_owner(CRATE_DEF_ID).map(|o| o.node) {
158-
MaybeOwner::Owner(OwnerNode::Crate(item)) => item,
158+
Some(OwnerNode::Crate(item)) => item,
159159
_ => bug!(),
160160
}
161161
}
@@ -215,7 +215,7 @@ impl<'hir> Map<'hir> {
215215

216216
#[inline]
217217
pub fn local_def_id_to_hir_id(&self, def_id: LocalDefId) -> HirId {
218-
let owner = self.tcx.hir_owner(def_id);
218+
let owner = self.tcx.local_def_id_to_hir_id(def_id);
219219
match owner {
220220
MaybeOwner::Owner(_) => HirId::make_owner(def_id),
221221
MaybeOwner::Phantom => bug!("No HirId for {:?}", def_id),
@@ -340,7 +340,7 @@ impl<'hir> Map<'hir> {
340340
/// Retrieves the `Node` corresponding to `id`, returning `None` if cannot be found.
341341
pub fn find(&self, id: HirId) -> Option<Node<'hir>> {
342342
if id.local_id == ItemLocalId::from_u32(0) {
343-
let owner = self.tcx.hir_owner(id.owner).as_owner()?;
343+
let owner = self.tcx.hir_owner(id.owner)?;
344344
Some(owner.node.into())
345345
} else {
346346
let owner = self.tcx.hir_owner_nodes(id.owner).as_owner()?;
@@ -371,7 +371,7 @@ impl<'hir> Map<'hir> {
371371
}
372372

373373
pub fn get_generics(&self, id: LocalDefId) -> Option<&'hir Generics<'hir>> {
374-
let node = self.tcx.hir_owner(id).as_owner()?;
374+
let node = self.tcx.hir_owner(id)?;
375375
match node.node {
376376
OwnerNode::ImplItem(impl_item) => Some(&impl_item.generics),
377377
OwnerNode::TraitItem(trait_item) => Some(&trait_item.generics),
@@ -588,10 +588,10 @@ impl<'hir> Map<'hir> {
588588
pub fn get_module(&self, module: LocalDefId) -> (&'hir Mod<'hir>, Span, HirId) {
589589
let hir_id = HirId::make_owner(module);
590590
match self.tcx.hir_owner(module).map(|o| o.node) {
591-
MaybeOwner::Owner(OwnerNode::Item(&Item {
592-
span, kind: ItemKind::Mod(ref m), ..
593-
})) => (m, span, hir_id),
594-
MaybeOwner::Owner(OwnerNode::Crate(item)) => (item, item.inner, hir_id),
591+
Some(OwnerNode::Item(&Item { span, kind: ItemKind::Mod(ref m), .. })) => {
592+
(m, span, hir_id)
593+
}
594+
Some(OwnerNode::Crate(item)) => (item, item.inner, hir_id),
595595
node => panic!("not a module: {:?}", node),
596596
}
597597
}
@@ -885,7 +885,7 @@ impl<'hir> Map<'hir> {
885885

886886
pub fn get_foreign_abi(&self, hir_id: HirId) -> Abi {
887887
let parent = self.get_parent_item(hir_id);
888-
if let MaybeOwner::Owner(node) = self.tcx.hir_owner(parent) {
888+
if let Some(node) = self.tcx.hir_owner(parent) {
889889
if let OwnerNode::Item(Item { kind: ItemKind::ForeignMod { abi, .. }, .. }) = node.node
890890
{
891891
return *abi;
@@ -899,21 +899,21 @@ impl<'hir> Map<'hir> {
899899

900900
pub fn expect_item(&self, id: LocalDefId) -> &'hir Item<'hir> {
901901
match self.tcx.hir_owner(id) {
902-
MaybeOwner::Owner(Owner { node: OwnerNode::Item(item), .. }) => item,
902+
Some(Owner { node: OwnerNode::Item(item), .. }) => item,
903903
_ => bug!("expected item, found {}", self.node_to_string(HirId::make_owner(id))),
904904
}
905905
}
906906

907907
pub fn expect_impl_item(&self, id: LocalDefId) -> &'hir ImplItem<'hir> {
908908
match self.tcx.hir_owner(id) {
909-
MaybeOwner::Owner(Owner { node: OwnerNode::ImplItem(item), .. }) => item,
909+
Some(Owner { node: OwnerNode::ImplItem(item), .. }) => item,
910910
_ => bug!("expected impl item, found {}", self.node_to_string(HirId::make_owner(id))),
911911
}
912912
}
913913

914914
pub fn expect_trait_item(&self, id: LocalDefId) -> &'hir TraitItem<'hir> {
915915
match self.tcx.hir_owner(id) {
916-
MaybeOwner::Owner(Owner { node: OwnerNode::TraitItem(item), .. }) => item,
916+
Some(Owner { node: OwnerNode::TraitItem(item), .. }) => item,
917917
_ => bug!("expected trait item, found {}", self.node_to_string(HirId::make_owner(id))),
918918
}
919919
}
@@ -927,7 +927,7 @@ impl<'hir> Map<'hir> {
927927

928928
pub fn expect_foreign_item(&self, id: LocalDefId) -> &'hir ForeignItem<'hir> {
929929
match self.tcx.hir_owner(id) {
930-
MaybeOwner::Owner(Owner { node: OwnerNode::ForeignItem(item), .. }) => item,
930+
Some(Owner { node: OwnerNode::ForeignItem(item), .. }) => item,
931931
_ => {
932932
bug!("expected foreign item, found {}", self.node_to_string(HirId::make_owner(id)))
933933
}

compiler/rustc_middle/src/hir/mod.rs

+5
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,11 @@ pub fn provide(providers: &mut Providers) {
6565
providers.crate_hash = map::crate_hash;
6666
providers.hir_module_items = map::hir_module_items;
6767
providers.hir_owner = |tcx, id| {
68+
let owner = tcx.hir_crate(()).owners.get(id)?.as_owner()?;
69+
let node = owner.node();
70+
Some(Owner { node, hash_without_bodies: owner.nodes.hash_without_bodies })
71+
};
72+
providers.local_def_id_to_hir_id = |tcx, id| {
6873
tcx.hir_crate(()).owners[id].map(|owner| {
6974
let node = owner.nodes.node();
7075
Owner { node, hash_without_bodies: owner.nodes.hash_without_bodies }

compiler/rustc_middle/src/query/mod.rs

+9-1
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,18 @@ rustc_queries! {
5252
///
5353
/// This can be conveniently accessed by methods on `tcx.hir()`.
5454
/// Avoid calling this query directly.
55-
query hir_owner(key: LocalDefId) -> hir::MaybeOwner<crate::hir::Owner<'tcx>> {
55+
query hir_owner(key: LocalDefId) -> Option<crate::hir::Owner<'tcx>> {
5656
desc { |tcx| "HIR owner of `{}`", tcx.def_path_str(key.to_def_id()) }
5757
}
5858

59+
/// Gives access to the HIR ID for the given `LocalDefId` owner `key`.
60+
///
61+
/// This can be conveniently accessed by methods on `tcx.hir()`.
62+
/// Avoid calling this query directly.
63+
query local_def_id_to_hir_id(key: LocalDefId) -> hir::MaybeOwner<crate::hir::Owner<'tcx>> {
64+
desc { |tcx| "HIR ID of `{}`", tcx.def_path_str(key.to_def_id()) }
65+
}
66+
5967
/// Gives access to the HIR node's parent for the HIR owner `key`.
6068
///
6169
/// This can be conveniently accessed by methods on `tcx.hir()`.

0 commit comments

Comments
 (0)