Skip to content

Commit a58cc02

Browse files
committed
hir: Add non-optional hir_owner_nodes for real OwnerIds
1 parent 9653ce4 commit a58cc02

File tree

3 files changed

+50
-49
lines changed

3 files changed

+50
-49
lines changed

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

Lines changed: 48 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -107,9 +107,8 @@ impl<'hir> Iterator for ParentOwnerIterator<'hir> {
107107
fn next(&mut self) -> Option<Self::Item> {
108108
if self.current_id.local_id.index() != 0 {
109109
self.current_id.local_id = ItemLocalId::new(0);
110-
if let Some(node) = self.map.tcx.hir_owner(self.current_id.owner) {
111-
return Some((self.current_id.owner, node));
112-
}
110+
let node = self.map.tcx.hir_owner_node(self.current_id.owner);
111+
return Some((self.current_id.owner, node));
113112
}
114113
if self.current_id == CRATE_HIR_ID {
115114
return None;
@@ -125,22 +124,37 @@ impl<'hir> Iterator for ParentOwnerIterator<'hir> {
125124
self.current_id = HirId::make_owner(parent_id.def_id);
126125

127126
// If this `HirId` doesn't have an entry, skip it and look for its `parent_id`.
128-
if let Some(node) = self.map.tcx.hir_owner(self.current_id.owner) {
129-
return Some((self.current_id.owner, node));
130-
}
127+
let node = self.map.tcx.hir_owner_node(self.current_id.owner);
128+
return Some((self.current_id.owner, node));
131129
}
132130
}
133131
}
134132

135133
impl<'tcx> TyCtxt<'tcx> {
136134
#[inline]
137-
fn hir_owner(self, owner: OwnerId) -> Option<OwnerNode<'tcx>> {
138-
Some(self.opt_hir_owner_nodes(owner.def_id)?.node())
135+
pub fn hir_owner_nodes(self, owner_id: OwnerId) -> &'tcx OwnerNodes<'tcx> {
136+
self.opt_hir_owner_nodes(owner_id.def_id)
137+
.unwrap_or_else(|| span_bug!(self.def_span(owner_id), "{owner_id:?} is not an owner"))
138+
}
139+
140+
#[inline]
141+
fn opt_hir_owner_node(self, def_id: LocalDefId) -> Option<OwnerNode<'tcx>> {
142+
self.opt_hir_owner_nodes(def_id).map(|nodes| nodes.node())
143+
}
144+
145+
#[inline]
146+
fn expect_hir_owner_node(self, def_id: LocalDefId) -> OwnerNode<'tcx> {
147+
self.hir_owner_nodes(OwnerId { def_id }).node()
148+
}
149+
150+
#[inline]
151+
fn hir_owner_node(self, owner_id: OwnerId) -> OwnerNode<'tcx> {
152+
self.hir_owner_nodes(owner_id).node()
139153
}
140154

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

175189
#[inline]
176190
pub fn root_module(self) -> &'hir Mod<'hir> {
177-
match self.tcx.hir_owner(CRATE_OWNER_ID) {
178-
Some(OwnerNode::Crate(item)) => item,
191+
match self.tcx.hir_owner_node(CRATE_OWNER_ID) {
192+
OwnerNode::Crate(item) => item,
179193
_ => bug!(),
180194
}
181195
}
@@ -213,7 +227,7 @@ impl<'hir> Map<'hir> {
213227
if id.local_id == ItemLocalId::from_u32(0) {
214228
Some(self.tcx.hir_owner_parent(id.owner))
215229
} else {
216-
let owner = self.tcx.opt_hir_owner_nodes(id.owner)?;
230+
let owner = self.tcx.hir_owner_nodes(id.owner);
217231
let node = owner.nodes[id.local_id].as_ref()?;
218232
let hir_id = HirId { owner: id.owner, local_id: node.parent };
219233
// HIR indexing should have checked that.
@@ -241,32 +255,31 @@ impl<'hir> Map<'hir> {
241255
}
242256

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

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

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

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

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

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

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

272285
#[track_caller]
@@ -436,9 +449,9 @@ impl<'hir> Map<'hir> {
436449

437450
pub fn get_module(self, module: LocalModDefId) -> (&'hir Mod<'hir>, Span, HirId) {
438451
let hir_id = HirId::make_owner(module.to_local_def_id());
439-
match self.tcx.hir_owner(hir_id.owner) {
440-
Some(OwnerNode::Item(&Item { span, kind: ItemKind::Mod(m), .. })) => (m, span, hir_id),
441-
Some(OwnerNode::Crate(item)) => (item, item.spans.inner_span, hir_id),
452+
match self.tcx.hir_owner_node(hir_id.owner) {
453+
OwnerNode::Item(&Item { span, kind: ItemKind::Mod(m), .. }) => (m, span, hir_id),
454+
OwnerNode::Crate(item) => (item, item.spans.inner_span, hir_id),
442455
node => panic!("not a module: {node:?}"),
443456
}
444457
}
@@ -726,8 +739,8 @@ impl<'hir> Map<'hir> {
726739

727740
pub fn get_foreign_abi(self, hir_id: HirId) -> Abi {
728741
let parent = self.get_parent_item(hir_id);
729-
if let Some(node) = self.tcx.hir_owner(parent)
730-
&& let OwnerNode::Item(Item { kind: ItemKind::ForeignMod { abi, .. }, .. }) = node
742+
if let OwnerNode::Item(Item { kind: ItemKind::ForeignMod { abi, .. }, .. }) =
743+
self.tcx.hir_owner_node(parent)
731744
{
732745
return *abi;
733746
}
@@ -738,37 +751,32 @@ impl<'hir> Map<'hir> {
738751
}
739752

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

746757
pub fn expect_item(self, id: LocalDefId) -> &'hir Item<'hir> {
747-
match self.tcx.hir_owner(OwnerId { def_id: id }) {
748-
Some(OwnerNode::Item(item)) => item,
758+
match self.tcx.expect_hir_owner_node(id) {
759+
OwnerNode::Item(item) => item,
749760
_ => bug!("expected item, found {}", self.node_to_string(HirId::make_owner(id))),
750761
}
751762
}
752763

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

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

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

774782
pub fn expect_variant(self, id: HirId) -> &'hir Variant<'hir> {
@@ -779,8 +787,8 @@ impl<'hir> Map<'hir> {
779787
}
780788

781789
pub fn expect_foreign_item(self, id: OwnerId) -> &'hir ForeignItem<'hir> {
782-
match self.tcx.hir_owner(id) {
783-
Some(OwnerNode::ForeignItem(item)) => item,
790+
match self.tcx.hir_owner_node(id) {
791+
OwnerNode::ForeignItem(item) => item,
784792
_ => {
785793
bug!(
786794
"expected foreign item, found {}",

compiler/rustc_mir_transform/src/coverage/mod.rs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -452,10 +452,5 @@ fn get_body_span<'tcx>(
452452
fn hash_mir_source<'tcx>(tcx: TyCtxt<'tcx>, hir_body: &'tcx rustc_hir::Body<'tcx>) -> u64 {
453453
// FIXME(cjgillot) Stop hashing HIR manually here.
454454
let owner = hir_body.id().hir_id.owner;
455-
tcx.opt_hir_owner_nodes(owner)
456-
.unwrap()
457-
.opt_hash_including_bodies
458-
.unwrap()
459-
.to_smaller_hash()
460-
.as_u64()
455+
tcx.hir_owner_nodes(owner).opt_hash_including_bodies.unwrap().to_smaller_hash().as_u64()
461456
}

src/tools/clippy/clippy_lints/src/min_ident_chars.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -93,9 +93,7 @@ impl Visitor<'_> for IdentVisitor<'_, '_> {
9393
// reimplement it even if we wanted to
9494
cx.tcx.opt_hir_node(hir_id)
9595
} else {
96-
let Some(owner) = cx.tcx.opt_hir_owner_nodes(hir_id.owner) else {
97-
return;
98-
};
96+
let owner = cx.tcx.hir_owner_nodes(hir_id.owner);
9997
owner.nodes.get(hir_id.local_id).copied().flatten().map(|p| p.node)
10098
};
10199
let Some(node) = node else {

0 commit comments

Comments
 (0)