1
Fork 0

Use more of OwnerNode.

This commit is contained in:
Camille GILLOT 2021-03-27 13:56:22 +01:00
parent b88083a58c
commit 6709648d17

View file

@ -1,6 +1,6 @@
use self::collector::NodeCollector;
use crate::hir::{AttributeMap, IndexedHir};
use crate::hir::{AttributeMap, IndexedHir, Owner};
use crate::ty::TyCtxt;
use rustc_ast as ast;
use rustc_data_structures::fingerprint::Fingerprint;
@ -121,13 +121,13 @@ pub struct ParentOwnerIterator<'map, 'hir> {
}
impl<'hir> Iterator for ParentOwnerIterator<'_, 'hir> {
type Item = (HirId, Node<'hir>);
type Item = (HirId, OwnerNode<'hir>);
fn next(&mut self) -> Option<Self::Item> {
if self.current_id.local_id.index() != 0 {
self.current_id.local_id = ItemLocalId::new(0);
if let Some(node) = self.map.find(self.current_id) {
return Some((self.current_id, node));
if let Some(node) = self.map.tcx.hir_owner(self.current_id.owner) {
return Some((self.current_id, node.node));
}
}
if self.current_id == CRATE_HIR_ID {
@ -144,8 +144,8 @@ impl<'hir> Iterator for ParentOwnerIterator<'_, 'hir> {
self.current_id = HirId::make_owner(parent_id);
// If this `HirId` doesn't have an entry, skip it and look for its `parent_id`.
if let Some(node) = self.map.find(self.current_id) {
return Some((self.current_id, node));
if let Some(node) = self.map.tcx.hir_owner(self.current_id.owner) {
return Some((self.current_id, node.node));
}
}
}
@ -331,10 +331,12 @@ impl<'hir> Map<'hir> {
}
pub fn get_generics(&self, id: DefId) -> Option<&'hir Generics<'hir>> {
self.get_if_local(id).and_then(|node| match &node {
Node::ImplItem(impl_item) => Some(&impl_item.generics),
Node::TraitItem(trait_item) => Some(&trait_item.generics),
Node::Item(Item {
let id = id.as_local()?;
let node = self.tcx.hir_owner(id)?;
match node.node {
OwnerNode::ImplItem(impl_item) => Some(&impl_item.generics),
OwnerNode::TraitItem(trait_item) => Some(&trait_item.generics),
OwnerNode::Item(Item {
kind:
ItemKind::Fn(_, generics, _)
| ItemKind::TyAlias(_, generics)
@ -347,35 +349,23 @@ impl<'hir> Map<'hir> {
..
}) => Some(generics),
_ => None,
})
}
}
pub fn item(&self, id: ItemId) -> &'hir Item<'hir> {
match self.find(id.hir_id()).unwrap() {
Node::Item(item) => item,
_ => bug!(),
}
self.tcx.hir_owner(id.def_id).unwrap().node.expect_item()
}
pub fn trait_item(&self, id: TraitItemId) -> &'hir TraitItem<'hir> {
match self.find(id.hir_id()).unwrap() {
Node::TraitItem(item) => item,
_ => bug!(),
}
self.tcx.hir_owner(id.def_id).unwrap().node.expect_trait_item()
}
pub fn impl_item(&self, id: ImplItemId) -> &'hir ImplItem<'hir> {
match self.find(id.hir_id()).unwrap() {
Node::ImplItem(item) => item,
_ => bug!(),
}
self.tcx.hir_owner(id.def_id).unwrap().node.expect_impl_item()
}
pub fn foreign_item(&self, id: ForeignItemId) -> &'hir ForeignItem<'hir> {
match self.find(id.hir_id()).unwrap() {
Node::ForeignItem(item) => item,
_ => bug!(),
}
self.tcx.hir_owner(id.def_id).unwrap().node.expect_foreign_item()
}
pub fn body(&self, id: BodyId) -> &'hir Body<'hir> {
@ -519,10 +509,12 @@ impl<'hir> Map<'hir> {
}
pub fn get_module(&self, module: LocalDefId) -> (&'hir Mod<'hir>, Span, HirId) {
let hir_id = self.local_def_id_to_hir_id(module);
match self.get(hir_id) {
Node::Item(&Item { span, kind: ItemKind::Mod(ref m), .. }) => (m, span, hir_id),
Node::Crate(item) => (&item, item.inner, hir_id),
let hir_id = HirId::make_owner(module);
match self.tcx.hir_owner(module).map(|o| o.node) {
Some(OwnerNode::Item(&Item { span, kind: ItemKind::Mod(ref m), .. })) => {
(m, span, hir_id)
}
Some(OwnerNode::Crate(item)) => (item, item.inner, hir_id),
node => panic!("not a module: {:?}", node),
}
}
@ -659,24 +651,20 @@ impl<'hir> Map<'hir> {
/// in the HIR which is recorded by the map and is an item, either an item
/// in a module, trait, or impl.
pub fn get_parent_item(&self, hir_id: HirId) -> HirId {
for (hir_id, node) in self.parent_owner_iter(hir_id) {
if let Node::Crate(_)
| Node::Item(_)
| Node::ForeignItem(_)
| Node::TraitItem(_)
| Node::ImplItem(_) = node
{
return hir_id;
}
if let Some((hir_id, _node)) = self.parent_owner_iter(hir_id).next() {
// A MacroDef does not have children.
debug_assert!(!matches!(_node, OwnerNode::MacroDef(_)));
hir_id
} else {
CRATE_HIR_ID
}
CRATE_HIR_ID
}
/// Returns the `HirId` of `id`'s nearest module parent, or `id` itself if no
/// module parent is in this map.
pub(super) fn get_module_parent_node(&self, hir_id: HirId) -> HirId {
for (hir_id, node) in self.parent_owner_iter(hir_id) {
if let Node::Item(&Item { kind: ItemKind::Mod(_), .. }) = node {
if let OwnerNode::Item(&Item { kind: ItemKind::Mod(_), .. }) = node {
return hir_id;
}
}
@ -749,8 +737,9 @@ impl<'hir> Map<'hir> {
pub fn get_foreign_abi(&self, hir_id: HirId) -> Abi {
let parent = self.get_parent_item(hir_id);
if let Some(node) = self.find(parent) {
if let Node::Item(Item { kind: ItemKind::ForeignMod { abi, .. }, .. }) = node {
if let Some(node) = self.tcx.hir_owner(self.local_def_id(parent)) {
if let OwnerNode::Item(Item { kind: ItemKind::ForeignMod { abi, .. }, .. }) = node.node
{
return *abi;
}
}
@ -758,22 +747,22 @@ impl<'hir> Map<'hir> {
}
pub fn expect_item(&self, id: HirId) -> &'hir Item<'hir> {
match self.find(id) {
Some(Node::Item(item)) => item,
match self.tcx.hir_owner(id.expect_owner()) {
Some(Owner { node: OwnerNode::Item(item) }) => item,
_ => bug!("expected item, found {}", self.node_to_string(id)),
}
}
pub fn expect_impl_item(&self, id: HirId) -> &'hir ImplItem<'hir> {
match self.find(id) {
Some(Node::ImplItem(item)) => item,
match self.tcx.hir_owner(id.expect_owner()) {
Some(Owner { node: OwnerNode::ImplItem(item) }) => item,
_ => bug!("expected impl item, found {}", self.node_to_string(id)),
}
}
pub fn expect_trait_item(&self, id: HirId) -> &'hir TraitItem<'hir> {
match self.find(id) {
Some(Node::TraitItem(item)) => item,
match self.tcx.hir_owner(id.expect_owner()) {
Some(Owner { node: OwnerNode::TraitItem(item) }) => item,
_ => bug!("expected trait item, found {}", self.node_to_string(id)),
}
}
@ -786,15 +775,15 @@ impl<'hir> Map<'hir> {
}
pub fn expect_foreign_item(&self, id: HirId) -> &'hir ForeignItem<'hir> {
match self.find(id) {
Some(Node::ForeignItem(item)) => item,
match self.tcx.hir_owner(id.expect_owner()) {
Some(Owner { node: OwnerNode::ForeignItem(item) }) => item,
_ => bug!("expected foreign item, found {}", self.node_to_string(id)),
}
}
pub fn expect_macro_def(&self, id: HirId) -> &'hir MacroDef<'hir> {
match self.find(id) {
Some(Node::MacroDef(macro_def)) => macro_def,
match self.tcx.hir_owner(id.expect_owner()) {
Some(Owner { node: OwnerNode::MacroDef(macro_def) }) => macro_def,
_ => bug!("expected macro def, found {}", self.node_to_string(id)),
}
}