Skip to content

Commit 419d205

Browse files
committed
Deduplicate associated_body and body_id
They match on almost the same patterns, which is fishy. Also turn `associated_body` into a method and do some cleanups nearby the call sites
1 parent a42873e commit 419d205

File tree

4 files changed

+33
-66
lines changed

4 files changed

+33
-66
lines changed

Diff for: compiler/rustc_hir/src/hir.rs

+21-14
Original file line numberDiff line numberDiff line change
@@ -3640,35 +3640,42 @@ impl<'hir> Node<'hir> {
36403640
}
36413641
}
36423642

3643-
pub fn body_id(&self) -> Option<BodyId> {
3643+
#[inline]
3644+
pub fn associated_body(&self) -> Option<(LocalDefId, BodyId)> {
36443645
match self {
36453646
Node::Item(Item {
3647+
owner_id,
36463648
kind:
3647-
ItemKind::Static(_, _, body)
3648-
| ItemKind::Const(_, _, body)
3649-
| ItemKind::Fn(_, _, body),
3649+
ItemKind::Const(_, _, body) | ItemKind::Static(.., body) | ItemKind::Fn(.., body),
36503650
..
36513651
})
36523652
| Node::TraitItem(TraitItem {
3653+
owner_id,
36533654
kind:
3654-
TraitItemKind::Fn(_, TraitFn::Provided(body)) | TraitItemKind::Const(_, Some(body)),
3655+
TraitItemKind::Const(_, Some(body)) | TraitItemKind::Fn(_, TraitFn::Provided(body)),
36553656
..
36563657
})
36573658
| Node::ImplItem(ImplItem {
3658-
kind: ImplItemKind::Fn(_, body) | ImplItemKind::Const(_, body),
3659-
..
3660-
})
3661-
| Node::Expr(Expr {
3662-
kind:
3663-
ExprKind::ConstBlock(ConstBlock { body, .. })
3664-
| ExprKind::Closure(Closure { body, .. })
3665-
| ExprKind::Repeat(_, ArrayLen::Body(AnonConst { body, .. })),
3659+
owner_id,
3660+
kind: ImplItemKind::Const(_, body) | ImplItemKind::Fn(_, body),
36663661
..
3667-
}) => Some(*body),
3662+
}) => Some((owner_id.def_id, *body)),
3663+
3664+
Node::Expr(Expr { kind: ExprKind::Closure(Closure { def_id, body, .. }), .. }) => {
3665+
Some((*def_id, *body))
3666+
}
3667+
3668+
Node::AnonConst(constant) => Some((constant.def_id, constant.body)),
3669+
Node::ConstBlock(constant) => Some((constant.def_id, constant.body)),
3670+
36683671
_ => None,
36693672
}
36703673
}
36713674

3675+
pub fn body_id(&self) -> Option<BodyId> {
3676+
Some(self.associated_body()?.1)
3677+
}
3678+
36723679
pub fn generics(self) -> Option<&'hir Generics<'hir>> {
36733680
match self {
36743681
Node::ForeignItem(ForeignItem {

Diff for: compiler/rustc_middle/src/hir/map/mod.rs

+7-47
Original file line numberDiff line numberDiff line change
@@ -20,44 +20,6 @@ use rustc_span::symbol::{kw, sym, Ident, Symbol};
2020
use rustc_span::{ErrorGuaranteed, Span};
2121
use rustc_target::spec::abi::Abi;
2222

23-
#[inline]
24-
pub fn associated_body(node: Node<'_>) -> Option<(LocalDefId, BodyId)> {
25-
match node {
26-
Node::Item(Item {
27-
owner_id,
28-
kind: ItemKind::Const(_, _, body) | ItemKind::Static(.., body) | ItemKind::Fn(.., body),
29-
..
30-
})
31-
| Node::TraitItem(TraitItem {
32-
owner_id,
33-
kind:
34-
TraitItemKind::Const(_, Some(body)) | TraitItemKind::Fn(_, TraitFn::Provided(body)),
35-
..
36-
})
37-
| Node::ImplItem(ImplItem {
38-
owner_id,
39-
kind: ImplItemKind::Const(_, body) | ImplItemKind::Fn(_, body),
40-
..
41-
}) => Some((owner_id.def_id, *body)),
42-
43-
Node::Expr(Expr { kind: ExprKind::Closure(Closure { def_id, body, .. }), .. }) => {
44-
Some((*def_id, *body))
45-
}
46-
47-
Node::AnonConst(constant) => Some((constant.def_id, constant.body)),
48-
Node::ConstBlock(constant) => Some((constant.def_id, constant.body)),
49-
50-
_ => None,
51-
}
52-
}
53-
54-
fn is_body_owner(node: Node<'_>, hir_id: HirId) -> bool {
55-
match associated_body(node) {
56-
Some((_, b)) => b.hir_id == hir_id,
57-
None => false,
58-
}
59-
}
60-
6123
// FIXME: the structure was necessary in the past but now it
6224
// only serves as "namespace" for HIR-related methods, and can be
6325
// removed if all the methods are reasonably renamed and moved to tcx
@@ -273,7 +235,7 @@ impl<'hir> Map<'hir> {
273235
#[track_caller]
274236
pub fn enclosing_body_owner(self, hir_id: HirId) -> LocalDefId {
275237
for (_, node) in self.parent_iter(hir_id) {
276-
if let Some((def_id, _)) = associated_body(node) {
238+
if let Some((def_id, _)) = node.associated_body() {
277239
return def_id;
278240
}
279241
}
@@ -286,20 +248,18 @@ impl<'hir> Map<'hir> {
286248
/// item (possibly associated), a closure, or a `hir::AnonConst`.
287249
pub fn body_owner(self, BodyId { hir_id }: BodyId) -> HirId {
288250
let parent = self.tcx.parent_hir_id(hir_id);
289-
assert!(is_body_owner(self.tcx.hir_node(parent), hir_id), "{hir_id:?}");
251+
assert_eq!(self.tcx.hir_node(parent).body_id().unwrap().hir_id, hir_id, "{hir_id:?}");
290252
parent
291253
}
292254

293255
pub fn body_owner_def_id(self, BodyId { hir_id }: BodyId) -> LocalDefId {
294-
associated_body(self.tcx.parent_hir_node(hir_id)).unwrap().0
256+
self.tcx.parent_hir_node(hir_id).associated_body().unwrap().0
295257
}
296258

297259
/// Given a `LocalDefId`, returns the `BodyId` associated with it,
298260
/// if the node is a body owner, otherwise returns `None`.
299261
pub fn maybe_body_owned_by(self, id: LocalDefId) -> Option<BodyId> {
300-
let node = self.tcx.hir_node_by_def_id(id);
301-
let (_, body_id) = associated_body(node)?;
302-
Some(body_id)
262+
self.tcx.hir_node_by_def_id(id).body_id()
303263
}
304264

305265
/// Given a body owner's id, returns the `BodyId` associated with it.
@@ -1314,7 +1274,7 @@ impl<'hir> Visitor<'hir> for ItemCollector<'hir> {
13141274
}
13151275

13161276
fn visit_item(&mut self, item: &'hir Item<'hir>) {
1317-
if associated_body(Node::Item(item)).is_some() {
1277+
if Node::Item(item).associated_body().is_some() {
13181278
self.body_owners.push(item.owner_id.def_id);
13191279
}
13201280

@@ -1355,7 +1315,7 @@ impl<'hir> Visitor<'hir> for ItemCollector<'hir> {
13551315
}
13561316

13571317
fn visit_trait_item(&mut self, item: &'hir TraitItem<'hir>) {
1358-
if associated_body(Node::TraitItem(item)).is_some() {
1318+
if Node::TraitItem(item).associated_body().is_some() {
13591319
self.body_owners.push(item.owner_id.def_id);
13601320
}
13611321

@@ -1364,7 +1324,7 @@ impl<'hir> Visitor<'hir> for ItemCollector<'hir> {
13641324
}
13651325

13661326
fn visit_impl_item(&mut self, item: &'hir ImplItem<'hir>) {
1367-
if associated_body(Node::ImplItem(item)).is_some() {
1327+
if Node::ImplItem(item).associated_body().is_some() {
13681328
self.body_owners.push(item.owner_id.def_id);
13691329
}
13701330

Diff for: compiler/rustc_mir_transform/src/coverage/mod.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ use self::spans::{BcbMapping, BcbMappingKind, CoverageSpans};
1313

1414
use crate::MirPass;
1515

16-
use rustc_middle::hir;
1716
use rustc_middle::mir::coverage::*;
1817
use rustc_middle::mir::{
1918
self, BasicBlock, BasicBlockData, Coverage, SourceInfo, Statement, StatementKind, Terminator,
@@ -368,8 +367,7 @@ fn extract_hir_info<'tcx>(tcx: TyCtxt<'tcx>, def_id: LocalDefId) -> ExtractedHir
368367
// to HIR for it.
369368

370369
let hir_node = tcx.hir_node_by_def_id(def_id);
371-
let (_, fn_body_id) =
372-
hir::map::associated_body(hir_node).expect("HIR node is a function with body");
370+
let fn_body_id = hir_node.body_id().expect("HIR node is a function with body");
373371
let hir_body = tcx.hir().body(fn_body_id);
374372

375373
let maybe_fn_sig = hir_node.fn_sig();

Diff for: src/tools/clippy/clippy_lints/src/needless_pass_by_ref_mut.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ use rustc_hir::{
1313
use rustc_hir_typeck::expr_use_visitor as euv;
1414
use rustc_infer::infer::{InferCtxt, TyCtxtInferExt};
1515
use rustc_lint::{LateContext, LateLintPass};
16-
use rustc_middle::hir::map::associated_body;
1716
use rustc_middle::mir::FakeReadCause;
1817
use rustc_middle::ty::{self, Ty, TyCtxt, UpvarId, UpvarPath};
1918
use rustc_session::impl_lint_pass;
@@ -112,7 +111,10 @@ fn check_closures<'tcx>(
112111
}
113112
ctx.prev_bind = None;
114113
ctx.prev_move_to_closure.clear();
115-
if let Some(body) = associated_body(cx.tcx.hir_node_by_def_id(closure))
114+
if let Some(body) = cx
115+
.tcx
116+
.hir_node_by_def_id(closure)
117+
.associated_body()
116118
.map(|(_, body_id)| hir.body(body_id))
117119
{
118120
euv::ExprUseVisitor::new(ctx, infcx, closure, cx.param_env, cx.typeck_results()).consume_body(body);

0 commit comments

Comments
 (0)