Skip to content

Commit 667d5d3

Browse files
committed
hir: Add non-optional hir_owner_nodes for real OwnerIds
1 parent 64b6b5b commit 667d5d3

File tree

3 files changed

+55
-49
lines changed

3 files changed

+55
-49
lines changed

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

+53-40
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,42 @@ 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+
fn expect_hir_owner_nodes(self, def_id: LocalDefId) -> &'tcx OwnerNodes<'tcx> {
136+
self.opt_hir_owner_nodes(def_id)
137+
.unwrap_or_else(|| span_bug!(self.def_span(def_id), "{def_id:?} is not an owner"))
138+
}
139+
140+
#[inline]
141+
pub fn hir_owner_nodes(self, owner_id: OwnerId) -> &'tcx OwnerNodes<'tcx> {
142+
self.expect_hir_owner_nodes(owner_id.def_id)
143+
}
144+
145+
#[inline]
146+
fn opt_hir_owner_node(self, def_id: LocalDefId) -> Option<OwnerNode<'tcx>> {
147+
self.opt_hir_owner_nodes(def_id).map(|nodes| nodes.node())
148+
}
149+
150+
#[inline]
151+
fn expect_hir_owner_node(self, def_id: LocalDefId) -> OwnerNode<'tcx> {
152+
self.expect_hir_owner_nodes(def_id).node()
153+
}
154+
155+
#[inline]
156+
fn hir_owner_node(self, owner_id: OwnerId) -> OwnerNode<'tcx> {
157+
self.hir_owner_nodes(owner_id).node()
139158
}
140159

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

175194
#[inline]
176195
pub fn root_module(self) -> &'hir Mod<'hir> {
177-
match self.tcx.hir_owner(CRATE_OWNER_ID) {
178-
Some(OwnerNode::Crate(item)) => item,
196+
match self.tcx.hir_owner_node(CRATE_OWNER_ID) {
197+
OwnerNode::Crate(item) => item,
179198
_ => bug!(),
180199
}
181200
}
@@ -213,7 +232,7 @@ impl<'hir> Map<'hir> {
213232
if id.local_id == ItemLocalId::from_u32(0) {
214233
Some(self.tcx.hir_owner_parent(id.owner))
215234
} else {
216-
let owner = self.tcx.opt_hir_owner_nodes(id.owner)?;
235+
let owner = self.tcx.hir_owner_nodes(id.owner);
217236
let node = owner.nodes[id.local_id].as_ref()?;
218237
let hir_id = HirId { owner: id.owner, local_id: node.parent };
219238
// HIR indexing should have checked that.
@@ -241,32 +260,31 @@ impl<'hir> Map<'hir> {
241260
}
242261

243262
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()
263+
self.tcx.opt_hir_owner_node(id)?.generics()
246264
}
247265

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

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

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

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

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

268286
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]
287+
self.tcx.hir_owner_nodes(id.hir_id.owner).bodies[&id.hir_id.local_id]
270288
}
271289

272290
#[track_caller]
@@ -436,9 +454,9 @@ impl<'hir> Map<'hir> {
436454

437455
pub fn get_module(self, module: LocalModDefId) -> (&'hir Mod<'hir>, Span, HirId) {
438456
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),
457+
match self.tcx.hir_owner_node(hir_id.owner) {
458+
OwnerNode::Item(&Item { span, kind: ItemKind::Mod(m), .. }) => (m, span, hir_id),
459+
OwnerNode::Crate(item) => (item, item.spans.inner_span, hir_id),
442460
node => panic!("not a module: {node:?}"),
443461
}
444462
}
@@ -726,8 +744,8 @@ impl<'hir> Map<'hir> {
726744

727745
pub fn get_foreign_abi(self, hir_id: HirId) -> Abi {
728746
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
747+
if let OwnerNode::Item(Item { kind: ItemKind::ForeignMod { abi, .. }, .. }) =
748+
self.tcx.hir_owner_node(parent)
731749
{
732750
return *abi;
733751
}
@@ -738,37 +756,32 @@ impl<'hir> Map<'hir> {
738756
}
739757

740758
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))
759+
self.tcx.expect_hir_owner_node(def_id)
744760
}
745761

746762
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,
763+
match self.tcx.expect_hir_owner_node(id) {
764+
OwnerNode::Item(item) => item,
749765
_ => bug!("expected item, found {}", self.node_to_string(HirId::make_owner(id))),
750766
}
751767
}
752768

753769
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,
770+
match self.tcx.expect_hir_owner_node(id) {
771+
OwnerNode::ImplItem(item) => item,
756772
_ => bug!("expected impl item, found {}", self.node_to_string(HirId::make_owner(id))),
757773
}
758774
}
759775

760776
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,
777+
match self.tcx.expect_hir_owner_node(id) {
778+
OwnerNode::TraitItem(item) => item,
763779
_ => bug!("expected trait item, found {}", self.node_to_string(HirId::make_owner(id))),
764780
}
765781
}
766782

767783
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-
}
784+
Some(&self.tcx.opt_hir_owner_node(def_id)?.fn_decl()?.output)
772785
}
773786

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

781794
pub fn expect_foreign_item(self, id: OwnerId) -> &'hir ForeignItem<'hir> {
782-
match self.tcx.hir_owner(id) {
783-
Some(OwnerNode::ForeignItem(item)) => item,
795+
match self.tcx.hir_owner_node(id) {
796+
OwnerNode::ForeignItem(item) => item,
784797
_ => {
785798
bug!(
786799
"expected foreign item, found {}",

compiler/rustc_mir_transform/src/coverage/mod.rs

+1-6
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

+1-3
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)