Skip to content

Commit

Permalink
hir: Add non-optional hir_owner_nodes for real OwnerIds
Browse files Browse the repository at this point in the history
  • Loading branch information
petrochenkov committed Jan 30, 2024
1 parent 64b6b5b commit 667d5d3
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 49 deletions.
93 changes: 53 additions & 40 deletions compiler/rustc_middle/src/hir/map/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,9 +107,8 @@ impl<'hir> Iterator for ParentOwnerIterator<'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.tcx.hir_owner(self.current_id.owner) {
return Some((self.current_id.owner, node));
}
let node = self.map.tcx.hir_owner_node(self.current_id.owner);
return Some((self.current_id.owner, node));
}
if self.current_id == CRATE_HIR_ID {
return None;
Expand All @@ -125,22 +124,42 @@ impl<'hir> Iterator for ParentOwnerIterator<'hir> {
self.current_id = HirId::make_owner(parent_id.def_id);

// If this `HirId` doesn't have an entry, skip it and look for its `parent_id`.
if let Some(node) = self.map.tcx.hir_owner(self.current_id.owner) {
return Some((self.current_id.owner, node));
}
let node = self.map.tcx.hir_owner_node(self.current_id.owner);
return Some((self.current_id.owner, node));
}
}
}

impl<'tcx> TyCtxt<'tcx> {
#[inline]
fn hir_owner(self, owner: OwnerId) -> Option<OwnerNode<'tcx>> {
Some(self.opt_hir_owner_nodes(owner.def_id)?.node())
fn expect_hir_owner_nodes(self, def_id: LocalDefId) -> &'tcx OwnerNodes<'tcx> {
self.opt_hir_owner_nodes(def_id)
.unwrap_or_else(|| span_bug!(self.def_span(def_id), "{def_id:?} is not an owner"))
}

#[inline]
pub fn hir_owner_nodes(self, owner_id: OwnerId) -> &'tcx OwnerNodes<'tcx> {
self.expect_hir_owner_nodes(owner_id.def_id)
}

#[inline]
fn opt_hir_owner_node(self, def_id: LocalDefId) -> Option<OwnerNode<'tcx>> {
self.opt_hir_owner_nodes(def_id).map(|nodes| nodes.node())
}

#[inline]
fn expect_hir_owner_node(self, def_id: LocalDefId) -> OwnerNode<'tcx> {
self.expect_hir_owner_nodes(def_id).node()
}

#[inline]
fn hir_owner_node(self, owner_id: OwnerId) -> OwnerNode<'tcx> {
self.hir_owner_nodes(owner_id).node()
}

/// Retrieves the `hir::Node` corresponding to `id`, returning `None` if cannot be found.
pub fn opt_hir_node(self, id: HirId) -> Option<Node<'tcx>> {
let owner = self.opt_hir_owner_nodes(id.owner)?;
let owner = self.hir_owner_nodes(id.owner);
let node = owner.nodes[id.local_id].as_ref()?;
Some(node.node)
}
Expand Down Expand Up @@ -174,8 +193,8 @@ impl<'hir> Map<'hir> {

#[inline]
pub fn root_module(self) -> &'hir Mod<'hir> {
match self.tcx.hir_owner(CRATE_OWNER_ID) {
Some(OwnerNode::Crate(item)) => item,
match self.tcx.hir_owner_node(CRATE_OWNER_ID) {
OwnerNode::Crate(item) => item,
_ => bug!(),
}
}
Expand Down Expand Up @@ -213,7 +232,7 @@ impl<'hir> Map<'hir> {
if id.local_id == ItemLocalId::from_u32(0) {
Some(self.tcx.hir_owner_parent(id.owner))
} else {
let owner = self.tcx.opt_hir_owner_nodes(id.owner)?;
let owner = self.tcx.hir_owner_nodes(id.owner);
let node = owner.nodes[id.local_id].as_ref()?;
let hir_id = HirId { owner: id.owner, local_id: node.parent };
// HIR indexing should have checked that.
Expand Down Expand Up @@ -241,32 +260,31 @@ impl<'hir> Map<'hir> {
}

pub fn get_generics(self, id: LocalDefId) -> Option<&'hir Generics<'hir>> {
let node = self.tcx.hir_owner(OwnerId { def_id: id })?;
node.generics()
self.tcx.opt_hir_owner_node(id)?.generics()
}

pub fn owner(self, id: OwnerId) -> OwnerNode<'hir> {
self.tcx.hir_owner(id).unwrap_or_else(|| bug!("expected owner for {:?}", id))
self.tcx.hir_owner_node(id)
}

pub fn item(self, id: ItemId) -> &'hir Item<'hir> {
self.tcx.hir_owner(id.owner_id).unwrap().expect_item()
self.tcx.hir_owner_node(id.owner_id).expect_item()
}

pub fn trait_item(self, id: TraitItemId) -> &'hir TraitItem<'hir> {
self.tcx.hir_owner(id.owner_id).unwrap().expect_trait_item()
self.tcx.hir_owner_node(id.owner_id).expect_trait_item()
}

pub fn impl_item(self, id: ImplItemId) -> &'hir ImplItem<'hir> {
self.tcx.hir_owner(id.owner_id).unwrap().expect_impl_item()
self.tcx.hir_owner_node(id.owner_id).expect_impl_item()
}

pub fn foreign_item(self, id: ForeignItemId) -> &'hir ForeignItem<'hir> {
self.tcx.hir_owner(id.owner_id).unwrap().expect_foreign_item()
self.tcx.hir_owner_node(id.owner_id).expect_foreign_item()
}

pub fn body(self, id: BodyId) -> &'hir Body<'hir> {
self.tcx.opt_hir_owner_nodes(id.hir_id.owner).unwrap().bodies[&id.hir_id.local_id]
self.tcx.hir_owner_nodes(id.hir_id.owner).bodies[&id.hir_id.local_id]
}

#[track_caller]
Expand Down Expand Up @@ -436,9 +454,9 @@ impl<'hir> Map<'hir> {

pub fn get_module(self, module: LocalModDefId) -> (&'hir Mod<'hir>, Span, HirId) {
let hir_id = HirId::make_owner(module.to_local_def_id());
match self.tcx.hir_owner(hir_id.owner) {
Some(OwnerNode::Item(&Item { span, kind: ItemKind::Mod(m), .. })) => (m, span, hir_id),
Some(OwnerNode::Crate(item)) => (item, item.spans.inner_span, hir_id),
match self.tcx.hir_owner_node(hir_id.owner) {
OwnerNode::Item(&Item { span, kind: ItemKind::Mod(m), .. }) => (m, span, hir_id),
OwnerNode::Crate(item) => (item, item.spans.inner_span, hir_id),
node => panic!("not a module: {node:?}"),
}
}
Expand Down Expand Up @@ -726,8 +744,8 @@ 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.tcx.hir_owner(parent)
&& let OwnerNode::Item(Item { kind: ItemKind::ForeignMod { abi, .. }, .. }) = node
if let OwnerNode::Item(Item { kind: ItemKind::ForeignMod { abi, .. }, .. }) =
self.tcx.hir_owner_node(parent)
{
return *abi;
}
Expand All @@ -738,37 +756,32 @@ impl<'hir> Map<'hir> {
}

pub fn expect_owner(self, def_id: LocalDefId) -> OwnerNode<'hir> {
self.tcx
.hir_owner(OwnerId { def_id })
.unwrap_or_else(|| bug!("expected owner for {:?}", def_id))
self.tcx.expect_hir_owner_node(def_id)
}

pub fn expect_item(self, id: LocalDefId) -> &'hir Item<'hir> {
match self.tcx.hir_owner(OwnerId { def_id: id }) {
Some(OwnerNode::Item(item)) => item,
match self.tcx.expect_hir_owner_node(id) {
OwnerNode::Item(item) => item,
_ => bug!("expected item, found {}", self.node_to_string(HirId::make_owner(id))),
}
}

pub fn expect_impl_item(self, id: LocalDefId) -> &'hir ImplItem<'hir> {
match self.tcx.hir_owner(OwnerId { def_id: id }) {
Some(OwnerNode::ImplItem(item)) => item,
match self.tcx.expect_hir_owner_node(id) {
OwnerNode::ImplItem(item) => item,
_ => bug!("expected impl item, found {}", self.node_to_string(HirId::make_owner(id))),
}
}

pub fn expect_trait_item(self, id: LocalDefId) -> &'hir TraitItem<'hir> {
match self.tcx.hir_owner(OwnerId { def_id: id }) {
Some(OwnerNode::TraitItem(item)) => item,
match self.tcx.expect_hir_owner_node(id) {
OwnerNode::TraitItem(item) => item,
_ => bug!("expected trait item, found {}", self.node_to_string(HirId::make_owner(id))),
}
}

pub fn get_fn_output(self, def_id: LocalDefId) -> Option<&'hir FnRetTy<'hir>> {
match self.tcx.hir_owner(OwnerId { def_id }) {
Some(node) => node.fn_decl().map(|fn_decl| &fn_decl.output),
_ => None,
}
Some(&self.tcx.opt_hir_owner_node(def_id)?.fn_decl()?.output)
}

pub fn expect_variant(self, id: HirId) -> &'hir Variant<'hir> {
Expand All @@ -779,8 +792,8 @@ impl<'hir> Map<'hir> {
}

pub fn expect_foreign_item(self, id: OwnerId) -> &'hir ForeignItem<'hir> {
match self.tcx.hir_owner(id) {
Some(OwnerNode::ForeignItem(item)) => item,
match self.tcx.hir_owner_node(id) {
OwnerNode::ForeignItem(item) => item,
_ => {
bug!(
"expected foreign item, found {}",
Expand Down
7 changes: 1 addition & 6 deletions compiler/rustc_mir_transform/src/coverage/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -452,10 +452,5 @@ fn get_body_span<'tcx>(
fn hash_mir_source<'tcx>(tcx: TyCtxt<'tcx>, hir_body: &'tcx rustc_hir::Body<'tcx>) -> u64 {
// FIXME(cjgillot) Stop hashing HIR manually here.
let owner = hir_body.id().hir_id.owner;
tcx.opt_hir_owner_nodes(owner)
.unwrap()
.opt_hash_including_bodies
.unwrap()
.to_smaller_hash()
.as_u64()
tcx.hir_owner_nodes(owner).opt_hash_including_bodies.unwrap().to_smaller_hash().as_u64()
}
4 changes: 1 addition & 3 deletions src/tools/clippy/clippy_lints/src/min_ident_chars.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,7 @@ impl Visitor<'_> for IdentVisitor<'_, '_> {
// reimplement it even if we wanted to
cx.tcx.opt_hir_node(hir_id)
} else {
let Some(owner) = cx.tcx.opt_hir_owner_nodes(hir_id.owner) else {
return;
};
let owner = cx.tcx.hir_owner_nodes(hir_id.owner);
owner.nodes.get(hir_id.local_id).copied().flatten().map(|p| p.node)
};
let Some(node) = node else {
Expand Down

0 comments on commit 667d5d3

Please sign in to comment.