Skip to content

Commit 6206de2

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 41d97c8 commit 6206de2

File tree

4 files changed

+30
-65
lines changed

4 files changed

+30
-65
lines changed

compiler/rustc_hir/src/hir.rs

+21-14
Original file line numberDiff line numberDiff line change
@@ -3616,35 +3616,42 @@ impl<'hir> Node<'hir> {
36163616
}
36173617
}
36183618

3619-
pub fn body_id(&self) -> Option<BodyId> {
3619+
#[inline]
3620+
pub fn associated_body(&self) -> Option<(LocalDefId, BodyId)> {
36203621
match self {
36213622
Node::Item(Item {
3623+
owner_id,
36223624
kind:
3623-
ItemKind::Static(_, _, body)
3624-
| ItemKind::Const(_, _, body)
3625-
| ItemKind::Fn(_, _, body),
3625+
ItemKind::Const(_, _, body) | ItemKind::Static(.., body) | ItemKind::Fn(.., body),
36263626
..
36273627
})
36283628
| Node::TraitItem(TraitItem {
3629+
owner_id,
36293630
kind:
3630-
TraitItemKind::Fn(_, TraitFn::Provided(body)) | TraitItemKind::Const(_, Some(body)),
3631+
TraitItemKind::Const(_, Some(body)) | TraitItemKind::Fn(_, TraitFn::Provided(body)),
36313632
..
36323633
})
36333634
| Node::ImplItem(ImplItem {
3634-
kind: ImplItemKind::Fn(_, body) | ImplItemKind::Const(_, body),
3635-
..
3636-
})
3637-
| Node::Expr(Expr {
3638-
kind:
3639-
ExprKind::ConstBlock(ConstBlock { body, .. })
3640-
| ExprKind::Closure(Closure { body, .. })
3641-
| ExprKind::Repeat(_, ArrayLen::Body(AnonConst { body, .. })),
3635+
owner_id,
3636+
kind: ImplItemKind::Const(_, body) | ImplItemKind::Fn(_, body),
36423637
..
3643-
}) => Some(*body),
3638+
}) => Some((owner_id.def_id, *body)),
3639+
3640+
Node::Expr(Expr { kind: ExprKind::Closure(Closure { def_id, body, .. }), .. }) => {
3641+
Some((*def_id, *body))
3642+
}
3643+
3644+
Node::AnonConst(constant) => Some((constant.def_id, constant.body)),
3645+
Node::ConstBlock(constant) => Some((constant.def_id, constant.body)),
3646+
36443647
_ => None,
36453648
}
36463649
}
36473650

3651+
pub fn body_id(&self) -> Option<BodyId> {
3652+
Some(self.associated_body()?.1)
3653+
}
3654+
36483655
pub fn generics(self) -> Option<&'hir Generics<'hir>> {
36493656
match self {
36503657
Node::ForeignItem(ForeignItem {

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

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

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

303265
pub fn body_owner_def_id(self, BodyId { hir_id }: BodyId) -> LocalDefId {
304-
associated_body(self.tcx.parent_hir_node(hir_id)).unwrap().0
266+
self.tcx.parent_hir_node(hir_id).associated_body().unwrap().0
305267
}
306268

307269
/// Given a `LocalDefId`, returns the `BodyId` associated with it,
308270
/// if the node is a body owner, otherwise returns `None`.
309271
pub fn maybe_body_owned_by(self, id: LocalDefId) -> Option<BodyId> {
310272
let node = self.tcx.opt_hir_node_by_def_id(id)?;
311-
let (_, body_id) = associated_body(node)?;
312-
Some(body_id)
273+
node.body_id()
313274
}
314275

315276
/// Given a body owner's id, returns the `BodyId` associated with it.
@@ -1322,7 +1283,7 @@ impl<'hir> Visitor<'hir> for ItemCollector<'hir> {
13221283
}
13231284

13241285
fn visit_item(&mut self, item: &'hir Item<'hir>) {
1325-
if associated_body(Node::Item(item)).is_some() {
1286+
if Node::Item(item).associated_body().is_some() {
13261287
self.body_owners.push(item.owner_id.def_id);
13271288
}
13281289

@@ -1363,7 +1324,7 @@ impl<'hir> Visitor<'hir> for ItemCollector<'hir> {
13631324
}
13641325

13651326
fn visit_trait_item(&mut self, item: &'hir TraitItem<'hir>) {
1366-
if associated_body(Node::TraitItem(item)).is_some() {
1327+
if Node::TraitItem(item).associated_body().is_some() {
13671328
self.body_owners.push(item.owner_id.def_id);
13681329
}
13691330

@@ -1372,7 +1333,7 @@ impl<'hir> Visitor<'hir> for ItemCollector<'hir> {
13721333
}
13731334

13741335
fn visit_impl_item(&mut self, item: &'hir ImplItem<'hir>) {
1375-
if associated_body(Node::ImplItem(item)).is_some() {
1336+
if Node::ImplItem(item).associated_body().is_some() {
13761337
self.body_owners.push(item.owner_id.def_id);
13771338
}
13781339

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::middle::codegen_fn_attrs::CodegenFnAttrFlags;
1817
use rustc_middle::mir::coverage::*;
1918
use rustc_middle::mir::{
@@ -396,8 +395,7 @@ fn extract_hir_info<'tcx>(tcx: TyCtxt<'tcx>, def_id: LocalDefId) -> ExtractedHir
396395
// to HIR for it.
397396

398397
let hir_node = tcx.hir_node_by_def_id(def_id);
399-
let (_, fn_body_id) =
400-
hir::map::associated_body(hir_node).expect("HIR node is a function with body");
398+
let fn_body_id = hir_node.body_id().expect("HIR node is a function with body");
401399
let hir_body = tcx.hir().body(fn_body_id);
402400

403401
let maybe_fn_sig = hir_node.fn_sig();

src/tools/clippy/clippy_lints/src/needless_pass_by_ref_mut.rs

+1-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;
@@ -115,7 +114,7 @@ fn check_closures<'tcx>(
115114
if let Some(body) = cx
116115
.tcx
117116
.opt_hir_node_by_def_id(closure)
118-
.and_then(associated_body)
117+
.and_then(|n| n.associated_body())
119118
.map(|(_, body_id)| hir.body(body_id))
120119
{
121120
euv::ExprUseVisitor::new(ctx, infcx, closure, cx.param_env, cx.typeck_results()).consume_body(body);

0 commit comments

Comments
 (0)