Skip to content

Commit 9653ce4

Browse files
committed
hir: Simplify hir_owner_nodes query
The query accept arbitrary DefIds, not just owner DefIds. The return can be an `Option` because if there are no nodes, then it doesn't matter whether it's due to NonOwner or Phantom. Also rename the query to `opt_hir_owner_nodes`.
1 parent 5bd5d21 commit 9653ce4

File tree

7 files changed

+13
-18
lines changed

7 files changed

+13
-18
lines changed

compiler/rustc_incremental/src/assert_dep_graph.rs

Lines changed: 1 addition & 1 deletion
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

Lines changed: 2 additions & 2 deletions
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_middle/src/hir/map/mod.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -135,12 +135,12 @@ impl<'hir> Iterator for ParentOwnerIterator<'hir> {
135135
impl<'tcx> TyCtxt<'tcx> {
136136
#[inline]
137137
fn hir_owner(self, owner: OwnerId) -> Option<OwnerNode<'tcx>> {
138-
Some(self.hir_owner_nodes(owner).as_owner()?.node())
138+
Some(self.opt_hir_owner_nodes(owner.def_id)?.node())
139139
}
140140

141141
/// Retrieves the `hir::Node` corresponding to `id`, returning `None` if cannot be found.
142142
pub fn opt_hir_node(self, id: HirId) -> Option<Node<'tcx>> {
143-
let owner = self.hir_owner_nodes(id.owner).as_owner()?;
143+
let owner = self.opt_hir_owner_nodes(id.owner)?;
144144
let node = owner.nodes[id.local_id].as_ref()?;
145145
Some(node.node)
146146
}
@@ -213,7 +213,7 @@ impl<'hir> Map<'hir> {
213213
if id.local_id == ItemLocalId::from_u32(0) {
214214
Some(self.tcx.hir_owner_parent(id.owner))
215215
} else {
216-
let owner = self.tcx.hir_owner_nodes(id.owner).as_owner()?;
216+
let owner = self.tcx.opt_hir_owner_nodes(id.owner)?;
217217
let node = owner.nodes[id.local_id].as_ref()?;
218218
let hir_id = HirId { owner: id.owner, local_id: node.parent };
219219
// HIR indexing should have checked that.
@@ -266,7 +266,7 @@ impl<'hir> Map<'hir> {
266266
}
267267

268268
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]
269+
self.tcx.opt_hir_owner_nodes(id.hir_id.owner).unwrap().bodies[&id.hir_id.local_id]
270270
}
271271

272272
#[track_caller]

compiler/rustc_middle/src/hir/mod.rs

Lines changed: 2 additions & 7 deletions
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

Lines changed: 2 additions & 2 deletions
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_transform/src/coverage/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -452,7 +452,7 @@ 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)
455+
tcx.opt_hir_owner_nodes(owner)
456456
.unwrap()
457457
.opt_hash_including_bodies
458458
.unwrap()

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +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 {
96+
let Some(owner) = cx.tcx.opt_hir_owner_nodes(hir_id.owner) else {
9797
return;
9898
};
9999
owner.nodes.get(hir_id.local_id).copied().flatten().map(|p| p.node)

0 commit comments

Comments
 (0)