Skip to content

Commit d53ddcd

Browse files
committed
Auto merge of #120346 - petrochenkov:ownodes, r=oli-obk
hir: Refactor getters for owner nodes
2 parents 80deabd + db41f4a commit d53ddcd

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

49 files changed

+918
-925
lines changed

compiler/rustc_hir_analysis/src/check/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -578,7 +578,7 @@ pub fn check_function_signature<'tcx>(
578578
fn_id: LocalDefId,
579579
) -> rustc_span::Span {
580580
let mut args = {
581-
let node = tcx.hir().expect_owner(fn_id);
581+
let node = tcx.expect_hir_owner_node(fn_id);
582582
let decl = node.fn_decl().unwrap_or_else(|| bug!("expected fn decl, found {:?}", node));
583583
decl.inputs.iter().map(|t| t.span).chain(std::iter::once(decl.output.span()))
584584
};

compiler/rustc_hir_analysis/src/check/wfcheck.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ where
188188
}
189189

190190
fn check_well_formed(tcx: TyCtxt<'_>, def_id: hir::OwnerId) -> Result<(), ErrorGuaranteed> {
191-
let node = tcx.hir().owner(def_id);
191+
let node = tcx.hir_owner_node(def_id);
192192
let mut res = match node {
193193
hir::OwnerNode::Crate(_) => bug!("check_well_formed cannot be applied to the crate root"),
194194
hir::OwnerNode::Item(item) => check_item(tcx, item),

compiler/rustc_hir_analysis/src/collect/resolve_bound_vars.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,7 @@ fn resolve_bound_vars(tcx: TyCtxt<'_>, local_def_id: hir::OwnerId) -> ResolveBou
254254
map: &mut named_variable_map,
255255
scope: &Scope::Root { opt_parent_item: None },
256256
};
257-
match tcx.hir().owner(local_def_id) {
257+
match tcx.hir_owner_node(local_def_id) {
258258
hir::OwnerNode::Item(item) => visitor.visit_item(item),
259259
hir::OwnerNode::ForeignItem(item) => visitor.visit_foreign_item(item),
260260
hir::OwnerNode::TraitItem(item) => {

compiler/rustc_incremental/src/assert_dep_graph.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ impl<'tcx> IfThisChanged<'tcx> {
131131
None => DepNode::from_def_path_hash(
132132
self.tcx,
133133
def_path_hash,
134-
dep_kinds::hir_owner_nodes,
134+
dep_kinds::opt_hir_owner_nodes,
135135
),
136136
Some(n) => {
137137
match DepNode::from_label_string(self.tcx, n.as_str(), def_path_hash) {

compiler/rustc_incremental/src/persist/dirty_clean.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,8 @@ const BASE_FN: &[&str] = &[
5757

5858
/// DepNodes for Hir, which is pretty much everything
5959
const BASE_HIR: &[&str] = &[
60-
// hir_owner_nodes should be computed for all nodes
61-
label_strs::hir_owner_nodes,
60+
// opt_hir_owner_nodes should be computed for all nodes
61+
label_strs::opt_hir_owner_nodes,
6262
];
6363

6464
/// `impl` implementation of struct/trait

compiler/rustc_infer/src/infer/error_reporting/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2555,7 +2555,7 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
25552555
add_lt_suggs,
25562556
new_lt: &new_lt,
25572557
};
2558-
match self.tcx.hir().expect_owner(lifetime_scope) {
2558+
match self.tcx.expect_hir_owner_node(lifetime_scope) {
25592559
hir::OwnerNode::Item(i) => visitor.visit_item(i),
25602560
hir::OwnerNode::ForeignItem(i) => visitor.visit_foreign_item(i),
25612561
hir::OwnerNode::ImplItem(i) => visitor.visit_impl_item(i),

compiler/rustc_infer/src/infer/error_reporting/nice_region_error/static_impl_trait.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -443,7 +443,7 @@ impl<'a, 'tcx> NiceRegionError<'a, 'tcx> {
443443
if let hir::OwnerNode::Item(Item {
444444
kind: ItemKind::Impl(hir::Impl { self_ty, .. }),
445445
..
446-
}) = tcx.hir().owner(impl_did)
446+
}) = tcx.hir_owner_node(impl_did)
447447
{
448448
Some((impl_item.ident, self_ty))
449449
} else {

compiler/rustc_lint/src/levels.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ fn shallow_lint_levels_on(tcx: TyCtxt<'_>, owner: hir::OwnerId) -> ShallowLintLe
181181
// Otherwise, we need to visit the attributes in source code order, so we fetch HIR and do
182182
// a standard visit.
183183
// FIXME(#102522) Just iterate on attrs once that iteration order matches HIR's.
184-
_ => match tcx.hir().owner(owner) {
184+
_ => match tcx.hir_owner_node(owner) {
185185
hir::OwnerNode::Item(item) => levels.visit_item(item),
186186
hir::OwnerNode::ForeignItem(item) => levels.visit_foreign_item(item),
187187
hir::OwnerNode::TraitItem(item) => levels.visit_trait_item(item),

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

+51-46
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.hir_owner_nodes(owner).as_owner()?.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+
pub 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+
pub 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.hir_owner_nodes(id.owner).as_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.hir_owner_nodes(id.owner).as_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,27 @@ 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()
246-
}
247-
248-
pub fn owner(self, id: OwnerId) -> OwnerNode<'hir> {
249-
self.tcx.hir_owner(id).unwrap_or_else(|| bug!("expected owner for {:?}", id))
263+
self.tcx.opt_hir_owner_node(id)?.generics()
250264
}
251265

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

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

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

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

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

272286
#[track_caller]
@@ -436,9 +450,9 @@ impl<'hir> Map<'hir> {
436450

437451
pub fn get_module(self, module: LocalModDefId) -> (&'hir Mod<'hir>, Span, HirId) {
438452
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),
453+
match self.tcx.hir_owner_node(hir_id.owner) {
454+
OwnerNode::Item(&Item { span, kind: ItemKind::Mod(m), .. }) => (m, span, hir_id),
455+
OwnerNode::Crate(item) => (item, item.spans.inner_span, hir_id),
442456
node => panic!("not a module: {node:?}"),
443457
}
444458
}
@@ -726,8 +740,8 @@ impl<'hir> Map<'hir> {
726740

727741
pub fn get_foreign_abi(self, hir_id: HirId) -> Abi {
728742
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
743+
if let OwnerNode::Item(Item { kind: ItemKind::ForeignMod { abi, .. }, .. }) =
744+
self.tcx.hir_owner_node(parent)
731745
{
732746
return *abi;
733747
}
@@ -737,38 +751,29 @@ impl<'hir> Map<'hir> {
737751
)
738752
}
739753

740-
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))
744-
}
745-
746754
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,
755+
match self.tcx.expect_hir_owner_node(id) {
756+
OwnerNode::Item(item) => item,
749757
_ => bug!("expected item, found {}", self.node_to_string(HirId::make_owner(id))),
750758
}
751759
}
752760

753761
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,
762+
match self.tcx.expect_hir_owner_node(id) {
763+
OwnerNode::ImplItem(item) => item,
756764
_ => bug!("expected impl item, found {}", self.node_to_string(HirId::make_owner(id))),
757765
}
758766
}
759767

760768
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,
769+
match self.tcx.expect_hir_owner_node(id) {
770+
OwnerNode::TraitItem(item) => item,
763771
_ => bug!("expected trait item, found {}", self.node_to_string(HirId::make_owner(id))),
764772
}
765773
}
766774

767775
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-
}
776+
Some(&self.tcx.opt_hir_owner_node(def_id)?.fn_decl()?.output)
772777
}
773778

774779
pub fn expect_variant(self, id: HirId) -> &'hir Variant<'hir> {
@@ -779,8 +784,8 @@ impl<'hir> Map<'hir> {
779784
}
780785

781786
pub fn expect_foreign_item(self, id: OwnerId) -> &'hir ForeignItem<'hir> {
782-
match self.tcx.hir_owner(id) {
783-
Some(OwnerNode::ForeignItem(item)) => item,
787+
match self.tcx.hir_owner_node(id) {
788+
OwnerNode::ForeignItem(item) => item,
784789
_ => {
785790
bug!(
786791
"expected foreign item, found {}",

compiler/rustc_middle/src/hir/mod.rs

+2-7
Original file line numberDiff line numberDiff line change
@@ -135,13 +135,8 @@ pub fn provide(providers: &mut Providers) {
135135
MaybeOwner::NonOwner(hir_id) => hir_id,
136136
})
137137
};
138-
providers.hir_owner_nodes = |tcx, id| {
139-
if let Some(i) = tcx.hir_crate(()).owners.get(id.def_id) {
140-
i.map(|i| &i.nodes)
141-
} else {
142-
MaybeOwner::Phantom
143-
}
144-
};
138+
providers.opt_hir_owner_nodes =
139+
|tcx, id| tcx.hir_crate(()).owners.get(id)?.as_owner().map(|i| &i.nodes);
145140
providers.hir_owner_parent = |tcx, id| {
146141
// Accessing the local_parent is ok since its value is hashed as part of `id`'s DefPathHash.
147142
tcx.opt_local_parent(id.def_id).map_or(CRATE_HIR_ID, |parent| {

compiler/rustc_middle/src/query/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -190,11 +190,11 @@ rustc_queries! {
190190
desc { |tcx| "getting HIR parent of `{}`", tcx.def_path_str(key) }
191191
}
192192

193-
/// Gives access to the HIR nodes and bodies inside the HIR owner `key`.
193+
/// Gives access to the HIR nodes and bodies inside `key` if it's a HIR owner.
194194
///
195195
/// This can be conveniently accessed by methods on `tcx.hir()`.
196196
/// Avoid calling this query directly.
197-
query hir_owner_nodes(key: hir::OwnerId) -> hir::MaybeOwner<&'tcx hir::OwnerNodes<'tcx>> {
197+
query opt_hir_owner_nodes(key: LocalDefId) -> Option<&'tcx hir::OwnerNodes<'tcx>> {
198198
desc { |tcx| "getting HIR owner items in `{}`", tcx.def_path_str(key) }
199199
}
200200

compiler/rustc_mir_build/src/check_unsafety.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -583,7 +583,7 @@ impl UnsafeOpKind {
583583
suggest_unsafe_block: bool,
584584
) {
585585
let parent_id = tcx.hir().get_parent_item(hir_id);
586-
let parent_owner = tcx.hir().owner(parent_id);
586+
let parent_owner = tcx.hir_owner_node(parent_id);
587587
let should_suggest = parent_owner.fn_sig().is_some_and(|sig| sig.header.is_unsafe());
588588
let unsafe_not_inherited_note = if should_suggest {
589589
suggest_unsafe_block.then(|| {

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.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/methods/iter_nth_zero.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use rustc_span::sym;
1111
use super::ITER_NTH_ZERO;
1212

1313
pub(super) fn check(cx: &LateContext<'_>, expr: &hir::Expr<'_>, recv: &hir::Expr<'_>, arg: &hir::Expr<'_>) {
14-
if let OwnerNode::Item(item) = cx.tcx.hir().owner(cx.tcx.hir().get_parent_item(expr.hir_id))
14+
if let OwnerNode::Item(item) = cx.tcx.hir_owner_node(cx.tcx.hir().get_parent_item(expr.hir_id))
1515
&& let def_id = item.owner_id.to_def_id()
1616
&& is_trait_method(cx, expr, sym::Iterator)
1717
&& let Some(Constant::Int(0)) = constant(cx, cx.typeck_results(), arg)

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.hir_owner_nodes(hir_id.owner).as_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 {

src/tools/clippy/clippy_lints/src/returns.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ impl<'tcx> LateLintPass<'tcx> for Return {
183183
&& let ExprKind::Ret(Some(ret)) = expr.kind
184184
&& let ExprKind::Match(.., MatchSource::TryDesugar(_)) = ret.kind
185185
// Ensure this is not the final stmt, otherwise removing it would cause a compile error
186-
&& let OwnerNode::Item(item) = cx.tcx.hir().owner(cx.tcx.hir().get_parent_item(expr.hir_id))
186+
&& let OwnerNode::Item(item) = cx.tcx.hir_owner_node(cx.tcx.hir().get_parent_item(expr.hir_id))
187187
&& let ItemKind::Fn(_, _, body) = item.kind
188188
&& let block = cx.tcx.hir().body(body).value
189189
&& let ExprKind::Block(block, _) = block.kind

tests/incremental/dirty_clean.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,11 @@ mod y {
2626
use x;
2727

2828
#[rustc_clean(
29-
except="hir_owner_nodes,generics_of,predicates_of,type_of,fn_sig",
29+
except="opt_hir_owner_nodes,generics_of,predicates_of,type_of,fn_sig",
3030
cfg="cfail2",
3131
)]
3232
pub fn y() {
33-
//[cfail2]~^ ERROR `hir_owner_nodes(y)` should be dirty but is not
33+
//[cfail2]~^ ERROR `opt_hir_owner_nodes(y)` should be dirty but is not
3434
//[cfail2]~| ERROR `generics_of(y)` should be dirty but is not
3535
//[cfail2]~| ERROR `predicates_of(y)` should be dirty but is not
3636
//[cfail2]~| ERROR `type_of(y)` should be dirty but is not

tests/incremental/hash-module-order.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,14 @@
1212
#![feature(rustc_attrs)]
1313

1414
#[cfg(rpass1)]
15-
#[rustc_clean(cfg="rpass1",except="hir_owner_nodes")]
15+
#[rustc_clean(cfg="rpass1",except="opt_hir_owner_nodes")]
1616
mod foo {
1717
struct First;
1818
struct Second;
1919
}
2020

2121
#[cfg(rpass2)]
22-
#[rustc_clean(cfg="rpass2",except="hir_owner_nodes")]
22+
#[rustc_clean(cfg="rpass2",except="opt_hir_owner_nodes")]
2323
mod foo {
2424
struct Second;
2525
struct First;

0 commit comments

Comments
 (0)