Skip to content

Commit f296c16

Browse files
committed
Auto merge of #122021 - oli-obk:delangitemification, r=compiler-errors
Use hir::Node helper methods instead of repeating the same impl multiple times I wanted to do something entirely different and stumbled upon a bunch of cleanups
2 parents 8579a18 + 69c4e81 commit f296c16

File tree

8 files changed

+55
-185
lines changed

8 files changed

+55
-185
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_hir_analysis/src/collect/generics_of.rs

+3-23
Original file line numberDiff line numberDiff line change
@@ -508,29 +508,9 @@ fn has_late_bound_regions<'tcx>(tcx: TyCtxt<'tcx>, node: Node<'tcx>) -> Option<S
508508
visitor.has_late_bound_regions
509509
}
510510

511-
match node {
512-
Node::TraitItem(item) => match &item.kind {
513-
hir::TraitItemKind::Fn(sig, _) => has_late_bound_regions(tcx, item.generics, sig.decl),
514-
_ => None,
515-
},
516-
Node::ImplItem(item) => match &item.kind {
517-
hir::ImplItemKind::Fn(sig, _) => has_late_bound_regions(tcx, item.generics, sig.decl),
518-
_ => None,
519-
},
520-
Node::ForeignItem(item) => match item.kind {
521-
hir::ForeignItemKind::Fn(fn_decl, _, generics) => {
522-
has_late_bound_regions(tcx, generics, fn_decl)
523-
}
524-
_ => None,
525-
},
526-
Node::Item(item) => match &item.kind {
527-
hir::ItemKind::Fn(sig, .., generics, _) => {
528-
has_late_bound_regions(tcx, generics, sig.decl)
529-
}
530-
_ => None,
531-
},
532-
_ => None,
533-
}
511+
let decl = node.fn_decl()?;
512+
let generics = node.generics()?;
513+
has_late_bound_regions(tcx, generics, decl)
534514
}
535515

536516
struct AnonConstInParamTyDetector {

Diff for: compiler/rustc_hir_analysis/src/collect/predicates_of.rs

+17-66
Original file line numberDiff line numberDiff line change
@@ -123,43 +123,22 @@ fn gather_explicit_predicates_of(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::Gen
123123
// Preserving the order of insertion is important here so as not to break UI tests.
124124
let mut predicates: FxIndexSet<(ty::Clause<'_>, Span)> = FxIndexSet::default();
125125

126-
let ast_generics = match node {
127-
Node::TraitItem(item) => item.generics,
128-
129-
Node::ImplItem(item) => item.generics,
130-
131-
Node::Item(item) => match item.kind {
126+
let ast_generics = node.generics().unwrap_or(NO_GENERICS);
127+
if let Node::Item(item) = node {
128+
match item.kind {
132129
ItemKind::Impl(impl_) => {
133130
if impl_.defaultness.is_default() {
134131
is_default_impl_trait = tcx
135132
.impl_trait_ref(def_id)
136133
.map(|t| ty::Binder::dummy(t.instantiate_identity()));
137134
}
138-
impl_.generics
139135
}
140-
ItemKind::Fn(.., generics, _)
141-
| ItemKind::TyAlias(_, generics)
142-
| ItemKind::Const(_, generics, _)
143-
| ItemKind::Enum(_, generics)
144-
| ItemKind::Struct(_, generics)
145-
| ItemKind::Union(_, generics) => generics,
146-
147-
ItemKind::Trait(_, _, generics, self_bounds, ..)
148-
| ItemKind::TraitAlias(generics, self_bounds) => {
136+
137+
ItemKind::Trait(_, _, _, self_bounds, ..) | ItemKind::TraitAlias(_, self_bounds) => {
149138
is_trait = Some(self_bounds);
150-
generics
151139
}
152-
ItemKind::OpaqueTy(OpaqueTy { generics, .. }) => generics,
153-
_ => NO_GENERICS,
154-
},
155-
156-
Node::ForeignItem(item) => match item.kind {
157-
ForeignItemKind::Static(..) => NO_GENERICS,
158-
ForeignItemKind::Fn(_, _, generics) => generics,
159-
ForeignItemKind::Type => NO_GENERICS,
160-
},
161-
162-
_ => NO_GENERICS,
140+
_ => {}
141+
}
163142
};
164143

165144
let generics = tcx.generics_of(def_id);
@@ -703,45 +682,17 @@ pub(super) fn type_param_predicates(
703682
let mut extend = None;
704683

705684
let item_hir_id = tcx.local_def_id_to_hir_id(item_def_id);
706-
let ast_generics = match tcx.hir_node(item_hir_id) {
707-
Node::TraitItem(item) => item.generics,
708-
709-
Node::ImplItem(item) => item.generics,
710-
711-
Node::Item(item) => {
712-
match item.kind {
713-
ItemKind::Fn(.., generics, _)
714-
| ItemKind::Impl(&hir::Impl { generics, .. })
715-
| ItemKind::TyAlias(_, generics)
716-
| ItemKind::Const(_, generics, _)
717-
| ItemKind::OpaqueTy(&OpaqueTy {
718-
generics,
719-
origin: hir::OpaqueTyOrigin::TyAlias { .. },
720-
..
721-
})
722-
| ItemKind::Enum(_, generics)
723-
| ItemKind::Struct(_, generics)
724-
| ItemKind::Union(_, generics) => generics,
725-
ItemKind::Trait(_, _, generics, ..) => {
726-
// Implied `Self: Trait` and supertrait bounds.
727-
if param_id == item_hir_id {
728-
let identity_trait_ref =
729-
ty::TraitRef::identity(tcx, item_def_id.to_def_id());
730-
extend = Some((identity_trait_ref.to_predicate(tcx), item.span));
731-
}
732-
generics
733-
}
734-
_ => return result,
735-
}
736-
}
737-
738-
Node::ForeignItem(item) => match item.kind {
739-
ForeignItemKind::Fn(_, _, generics) => generics,
740-
_ => return result,
741-
},
742685

743-
_ => return result,
744-
};
686+
let hir_node = tcx.hir_node(item_hir_id);
687+
let Some(ast_generics) = hir_node.generics() else { return result };
688+
if let Node::Item(item) = hir_node
689+
&& let ItemKind::Trait(..) = item.kind
690+
// Implied `Self: Trait` and supertrait bounds.
691+
&& param_id == item_hir_id
692+
{
693+
let identity_trait_ref = ty::TraitRef::identity(tcx, item_def_id.to_def_id());
694+
extend = Some((identity_trait_ref.to_predicate(tcx), item.span));
695+
}
745696

746697
let icx = ItemCtxt::new(tcx, item_def_id);
747698
let extra_predicates = extend.into_iter().chain(

Diff for: compiler/rustc_hir_typeck/src/lib.rs

+1-23
Original file line numberDiff line numberDiff line change
@@ -95,29 +95,7 @@ macro_rules! type_error_struct {
9595
fn primary_body_of(
9696
node: Node<'_>,
9797
) -> Option<(hir::BodyId, Option<&hir::Ty<'_>>, Option<&hir::FnSig<'_>>)> {
98-
match node {
99-
Node::Item(item) => match item.kind {
100-
hir::ItemKind::Const(ty, _, body) | hir::ItemKind::Static(ty, _, body) => {
101-
Some((body, Some(ty), None))
102-
}
103-
hir::ItemKind::Fn(ref sig, .., body) => Some((body, None, Some(sig))),
104-
_ => None,
105-
},
106-
Node::TraitItem(item) => match item.kind {
107-
hir::TraitItemKind::Const(ty, Some(body)) => Some((body, Some(ty), None)),
108-
hir::TraitItemKind::Fn(ref sig, hir::TraitFn::Provided(body)) => {
109-
Some((body, None, Some(sig)))
110-
}
111-
_ => None,
112-
},
113-
Node::ImplItem(item) => match item.kind {
114-
hir::ImplItemKind::Const(ty, body) => Some((body, Some(ty), None)),
115-
hir::ImplItemKind::Fn(ref sig, body) => Some((body, None, Some(sig))),
116-
_ => None,
117-
},
118-
Node::AnonConst(constant) => Some((constant.body, None, None)),
119-
_ => None,
120-
}
98+
Some((node.body_id()?, node.ty(), node.fn_sig()))
12199
}
122100

123101
fn has_typeck_results(tcx: TyCtxt<'_>, def_id: DefId) -> bool {

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: compiler/rustc_passes/src/check_attr.rs

+1-7
Original file line numberDiff line numberDiff line change
@@ -609,13 +609,7 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
609609
&& !self.tcx.sess.target.is_like_wasm
610610
&& !self.tcx.sess.opts.actually_rustdoc
611611
{
612-
let hir::Node::Item(item) = self.tcx.hir_node(hir_id) else {
613-
unreachable!();
614-
};
615-
let hir::ItemKind::Fn(sig, _, _) = item.kind else {
616-
// target is `Fn`
617-
unreachable!();
618-
};
612+
let sig = self.tcx.hir_node(hir_id).fn_sig().unwrap();
619613

620614
self.dcx().emit_err(errors::LangItemWithTargetFeature {
621615
attr_span: attr.span,

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)