Skip to content

Commit 6709648

Browse files
committed
Use more of OwnerNode.
1 parent b88083a commit 6709648

File tree

1 file changed

+43
-54
lines changed
  • compiler/rustc_middle/src/hir/map

1 file changed

+43
-54
lines changed

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

+43-54
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use self::collector::NodeCollector;
22

3-
use crate::hir::{AttributeMap, IndexedHir};
3+
use crate::hir::{AttributeMap, IndexedHir, Owner};
44
use crate::ty::TyCtxt;
55
use rustc_ast as ast;
66
use rustc_data_structures::fingerprint::Fingerprint;
@@ -121,13 +121,13 @@ pub struct ParentOwnerIterator<'map, 'hir> {
121121
}
122122

123123
impl<'hir> Iterator for ParentOwnerIterator<'_, 'hir> {
124-
type Item = (HirId, Node<'hir>);
124+
type Item = (HirId, OwnerNode<'hir>);
125125

126126
fn next(&mut self) -> Option<Self::Item> {
127127
if self.current_id.local_id.index() != 0 {
128128
self.current_id.local_id = ItemLocalId::new(0);
129-
if let Some(node) = self.map.find(self.current_id) {
130-
return Some((self.current_id, node));
129+
if let Some(node) = self.map.tcx.hir_owner(self.current_id.owner) {
130+
return Some((self.current_id, node.node));
131131
}
132132
}
133133
if self.current_id == CRATE_HIR_ID {
@@ -144,8 +144,8 @@ impl<'hir> Iterator for ParentOwnerIterator<'_, 'hir> {
144144
self.current_id = HirId::make_owner(parent_id);
145145

146146
// If this `HirId` doesn't have an entry, skip it and look for its `parent_id`.
147-
if let Some(node) = self.map.find(self.current_id) {
148-
return Some((self.current_id, node));
147+
if let Some(node) = self.map.tcx.hir_owner(self.current_id.owner) {
148+
return Some((self.current_id, node.node));
149149
}
150150
}
151151
}
@@ -331,10 +331,12 @@ impl<'hir> Map<'hir> {
331331
}
332332

333333
pub fn get_generics(&self, id: DefId) -> Option<&'hir Generics<'hir>> {
334-
self.get_if_local(id).and_then(|node| match &node {
335-
Node::ImplItem(impl_item) => Some(&impl_item.generics),
336-
Node::TraitItem(trait_item) => Some(&trait_item.generics),
337-
Node::Item(Item {
334+
let id = id.as_local()?;
335+
let node = self.tcx.hir_owner(id)?;
336+
match node.node {
337+
OwnerNode::ImplItem(impl_item) => Some(&impl_item.generics),
338+
OwnerNode::TraitItem(trait_item) => Some(&trait_item.generics),
339+
OwnerNode::Item(Item {
338340
kind:
339341
ItemKind::Fn(_, generics, _)
340342
| ItemKind::TyAlias(_, generics)
@@ -347,35 +349,23 @@ impl<'hir> Map<'hir> {
347349
..
348350
}) => Some(generics),
349351
_ => None,
350-
})
352+
}
351353
}
352354

353355
pub fn item(&self, id: ItemId) -> &'hir Item<'hir> {
354-
match self.find(id.hir_id()).unwrap() {
355-
Node::Item(item) => item,
356-
_ => bug!(),
357-
}
356+
self.tcx.hir_owner(id.def_id).unwrap().node.expect_item()
358357
}
359358

360359
pub fn trait_item(&self, id: TraitItemId) -> &'hir TraitItem<'hir> {
361-
match self.find(id.hir_id()).unwrap() {
362-
Node::TraitItem(item) => item,
363-
_ => bug!(),
364-
}
360+
self.tcx.hir_owner(id.def_id).unwrap().node.expect_trait_item()
365361
}
366362

367363
pub fn impl_item(&self, id: ImplItemId) -> &'hir ImplItem<'hir> {
368-
match self.find(id.hir_id()).unwrap() {
369-
Node::ImplItem(item) => item,
370-
_ => bug!(),
371-
}
364+
self.tcx.hir_owner(id.def_id).unwrap().node.expect_impl_item()
372365
}
373366

374367
pub fn foreign_item(&self, id: ForeignItemId) -> &'hir ForeignItem<'hir> {
375-
match self.find(id.hir_id()).unwrap() {
376-
Node::ForeignItem(item) => item,
377-
_ => bug!(),
378-
}
368+
self.tcx.hir_owner(id.def_id).unwrap().node.expect_foreign_item()
379369
}
380370

381371
pub fn body(&self, id: BodyId) -> &'hir Body<'hir> {
@@ -519,10 +509,12 @@ impl<'hir> Map<'hir> {
519509
}
520510

521511
pub fn get_module(&self, module: LocalDefId) -> (&'hir Mod<'hir>, Span, HirId) {
522-
let hir_id = self.local_def_id_to_hir_id(module);
523-
match self.get(hir_id) {
524-
Node::Item(&Item { span, kind: ItemKind::Mod(ref m), .. }) => (m, span, hir_id),
525-
Node::Crate(item) => (&item, item.inner, hir_id),
512+
let hir_id = HirId::make_owner(module);
513+
match self.tcx.hir_owner(module).map(|o| o.node) {
514+
Some(OwnerNode::Item(&Item { span, kind: ItemKind::Mod(ref m), .. })) => {
515+
(m, span, hir_id)
516+
}
517+
Some(OwnerNode::Crate(item)) => (item, item.inner, hir_id),
526518
node => panic!("not a module: {:?}", node),
527519
}
528520
}
@@ -659,24 +651,20 @@ impl<'hir> Map<'hir> {
659651
/// in the HIR which is recorded by the map and is an item, either an item
660652
/// in a module, trait, or impl.
661653
pub fn get_parent_item(&self, hir_id: HirId) -> HirId {
662-
for (hir_id, node) in self.parent_owner_iter(hir_id) {
663-
if let Node::Crate(_)
664-
| Node::Item(_)
665-
| Node::ForeignItem(_)
666-
| Node::TraitItem(_)
667-
| Node::ImplItem(_) = node
668-
{
669-
return hir_id;
670-
}
654+
if let Some((hir_id, _node)) = self.parent_owner_iter(hir_id).next() {
655+
// A MacroDef does not have children.
656+
debug_assert!(!matches!(_node, OwnerNode::MacroDef(_)));
657+
hir_id
658+
} else {
659+
CRATE_HIR_ID
671660
}
672-
CRATE_HIR_ID
673661
}
674662

675663
/// Returns the `HirId` of `id`'s nearest module parent, or `id` itself if no
676664
/// module parent is in this map.
677665
pub(super) fn get_module_parent_node(&self, hir_id: HirId) -> HirId {
678666
for (hir_id, node) in self.parent_owner_iter(hir_id) {
679-
if let Node::Item(&Item { kind: ItemKind::Mod(_), .. }) = node {
667+
if let OwnerNode::Item(&Item { kind: ItemKind::Mod(_), .. }) = node {
680668
return hir_id;
681669
}
682670
}
@@ -749,31 +737,32 @@ impl<'hir> Map<'hir> {
749737

750738
pub fn get_foreign_abi(&self, hir_id: HirId) -> Abi {
751739
let parent = self.get_parent_item(hir_id);
752-
if let Some(node) = self.find(parent) {
753-
if let Node::Item(Item { kind: ItemKind::ForeignMod { abi, .. }, .. }) = node {
740+
if let Some(node) = self.tcx.hir_owner(self.local_def_id(parent)) {
741+
if let OwnerNode::Item(Item { kind: ItemKind::ForeignMod { abi, .. }, .. }) = node.node
742+
{
754743
return *abi;
755744
}
756745
}
757746
bug!("expected foreign mod or inlined parent, found {}", self.node_to_string(parent))
758747
}
759748

760749
pub fn expect_item(&self, id: HirId) -> &'hir Item<'hir> {
761-
match self.find(id) {
762-
Some(Node::Item(item)) => item,
750+
match self.tcx.hir_owner(id.expect_owner()) {
751+
Some(Owner { node: OwnerNode::Item(item) }) => item,
763752
_ => bug!("expected item, found {}", self.node_to_string(id)),
764753
}
765754
}
766755

767756
pub fn expect_impl_item(&self, id: HirId) -> &'hir ImplItem<'hir> {
768-
match self.find(id) {
769-
Some(Node::ImplItem(item)) => item,
757+
match self.tcx.hir_owner(id.expect_owner()) {
758+
Some(Owner { node: OwnerNode::ImplItem(item) }) => item,
770759
_ => bug!("expected impl item, found {}", self.node_to_string(id)),
771760
}
772761
}
773762

774763
pub fn expect_trait_item(&self, id: HirId) -> &'hir TraitItem<'hir> {
775-
match self.find(id) {
776-
Some(Node::TraitItem(item)) => item,
764+
match self.tcx.hir_owner(id.expect_owner()) {
765+
Some(Owner { node: OwnerNode::TraitItem(item) }) => item,
777766
_ => bug!("expected trait item, found {}", self.node_to_string(id)),
778767
}
779768
}
@@ -786,15 +775,15 @@ impl<'hir> Map<'hir> {
786775
}
787776

788777
pub fn expect_foreign_item(&self, id: HirId) -> &'hir ForeignItem<'hir> {
789-
match self.find(id) {
790-
Some(Node::ForeignItem(item)) => item,
778+
match self.tcx.hir_owner(id.expect_owner()) {
779+
Some(Owner { node: OwnerNode::ForeignItem(item) }) => item,
791780
_ => bug!("expected foreign item, found {}", self.node_to_string(id)),
792781
}
793782
}
794783

795784
pub fn expect_macro_def(&self, id: HirId) -> &'hir MacroDef<'hir> {
796-
match self.find(id) {
797-
Some(Node::MacroDef(macro_def)) => macro_def,
785+
match self.tcx.hir_owner(id.expect_owner()) {
786+
Some(Owner { node: OwnerNode::MacroDef(macro_def) }) => macro_def,
798787
_ => bug!("expected macro def, found {}", self.node_to_string(id)),
799788
}
800789
}

0 commit comments

Comments
 (0)