Skip to content

Commit b1b19bd

Browse files
get_parent and find_parent
1 parent 6af339d commit b1b19bd

File tree

29 files changed

+86
-93
lines changed

29 files changed

+86
-93
lines changed

compiler/rustc_hir/src/hir.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -3460,7 +3460,7 @@ impl<'hir> Node<'hir> {
34603460
/// ```ignore (illustrative)
34613461
/// ctor
34623462
/// .ctor_hir_id()
3463-
/// .and_then(|ctor_id| tcx.hir().find(tcx.hir().parent_id(ctor_id)))
3463+
/// .and_then(|ctor_id| tcx.hir().find_parent(ctor_id))
34643464
/// .and_then(|parent| parent.ident())
34653465
/// ```
34663466
pub fn ident(&self) -> Option<Ident> {

compiler/rustc_hir_analysis/src/astconv/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2936,7 +2936,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
29362936
let hir::Node::ImplItem(hir::ImplItem { kind: hir::ImplItemKind::Fn(..), ident, .. }) =
29372937
hir.get(fn_hir_id) else { return None };
29382938
let hir::Node::Item(hir::Item { kind: hir::ItemKind::Impl(i), .. }) =
2939-
hir.get(hir.parent_id(fn_hir_id)) else { bug!("ImplItem should have Impl parent") };
2939+
hir.get_parent(fn_hir_id) else { bug!("ImplItem should have Impl parent") };
29402940

29412941
let trait_ref = self.instantiate_mono_trait_ref(
29422942
i.of_trait.as_ref()?,

compiler/rustc_hir_analysis/src/collect.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1108,7 +1108,7 @@ fn fn_sig(tcx: TyCtxt<'_>, def_id: DefId) -> ty::PolyFnSig<'_> {
11081108
ImplItem(hir::ImplItem { kind: ImplItemKind::Fn(sig, _), generics, .. }) => {
11091109
// Do not try to infer the return type for a impl method coming from a trait
11101110
if let Item(hir::Item { kind: ItemKind::Impl(i), .. }) =
1111-
tcx.hir().get(tcx.hir().parent_id(hir_id))
1111+
tcx.hir().get_parent(hir_id)
11121112
&& i.of_trait.is_some()
11131113
{
11141114
<dyn AstConv<'_>>::ty_of_fn(

compiler/rustc_hir_analysis/src/collect/generics_of.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ pub(super) fn generics_of(tcx: TyCtxt<'_>, def_id: DefId) -> ty::Generics {
103103
// `min_const_generics`.
104104
Some(parent_def_id.to_def_id())
105105
} else {
106-
let parent_node = tcx.hir().get(tcx.hir().parent_id(hir_id));
106+
let parent_node = tcx.hir().get_parent(hir_id);
107107
match parent_node {
108108
// HACK(eddyb) this provides the correct generics for repeat
109109
// expressions' count (i.e. `N` in `[x; N]`), and explicit
@@ -320,7 +320,7 @@ pub(super) fn generics_of(tcx: TyCtxt<'_>, def_id: DefId) -> ty::Generics {
320320

321321
// provide junk type parameter defs for const blocks.
322322
if let Node::AnonConst(_) = node {
323-
let parent_node = tcx.hir().get(tcx.hir().parent_id(hir_id));
323+
let parent_node = tcx.hir().get_parent(hir_id);
324324
if let Node::Expr(&Expr { kind: ExprKind::ConstBlock(_), .. }) = parent_node {
325325
params.push(ty::GenericParamDef {
326326
index: next_index(),

compiler/rustc_hir_analysis/src/collect/type_of.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -402,7 +402,7 @@ pub(super) fn type_of(tcx: TyCtxt<'_>, def_id: DefId) -> Ty<'_> {
402402
}
403403

404404
Node::AnonConst(_) => {
405-
let parent_node = tcx.hir().get(tcx.hir().parent_id(hir_id));
405+
let parent_node = tcx.hir().get_parent(hir_id);
406406
match parent_node {
407407
Node::Ty(&Ty { kind: TyKind::Array(_, ref constant), .. })
408408
| Node::Expr(&Expr { kind: ExprKind::Repeat(_, ref constant), .. })
@@ -445,7 +445,7 @@ pub(super) fn type_of(tcx: TyCtxt<'_>, def_id: DefId) -> Ty<'_> {
445445
..
446446
},
447447
) if let Node::TraitRef(trait_ref) =
448-
tcx.hir().get(tcx.hir().parent_id(binding_id))
448+
tcx.hir().get_parent(binding_id)
449449
&& e.hir_id == hir_id =>
450450
{
451451
let Some(trait_def_id) = trait_ref.trait_def_id() else {
@@ -472,7 +472,7 @@ pub(super) fn type_of(tcx: TyCtxt<'_>, def_id: DefId) -> Ty<'_> {
472472
Node::TypeBinding(
473473
binding @ &TypeBinding { hir_id: binding_id, gen_args, ref kind, .. },
474474
) if let Node::TraitRef(trait_ref) =
475-
tcx.hir().get(tcx.hir().parent_id(binding_id))
475+
tcx.hir().get_parent(binding_id)
476476
&& let Some((idx, _)) =
477477
gen_args.args.iter().enumerate().find(|(_, arg)| {
478478
if let GenericArg::Const(ct) = arg {

compiler/rustc_hir_typeck/src/_match.rs

+1-4
Original file line numberDiff line numberDiff line change
@@ -294,10 +294,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
294294
};
295295
if let hir::Node::Block(block) = node {
296296
// check that the body's parent is an fn
297-
let parent = self
298-
.tcx
299-
.hir()
300-
.get(self.tcx.hir().parent_id(self.tcx.hir().parent_id(block.hir_id)));
297+
let parent = self.tcx.hir().get_parent(self.tcx.hir().parent_id(block.hir_id));
301298
if let (Some(expr), hir::Node::Item(hir::Item { kind: hir::ItemKind::Fn(..), .. })) =
302299
(&block.expr, parent)
303300
{

compiler/rustc_hir_typeck/src/demand.rs

+4-7
Original file line numberDiff line numberDiff line change
@@ -258,10 +258,9 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
258258
hir::Path { res: hir::def::Res::Local(hir_id), .. },
259259
)) => {
260260
if let Some(hir::Node::Pat(pat)) = self.tcx.hir().find(*hir_id) {
261-
let parent = self.tcx.hir().parent_id(pat.hir_id);
262261
primary_span = pat.span;
263262
secondary_span = pat.span;
264-
match self.tcx.hir().find(parent) {
263+
match self.tcx.hir().find_parent(pat.hir_id) {
265264
Some(hir::Node::Local(hir::Local { ty: Some(ty), .. })) => {
266265
primary_span = ty.span;
267266
post_message = " type";
@@ -857,7 +856,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
857856
_ => None,
858857
}?;
859858

860-
match hir.find(hir.parent_id(expr.hir_id))? {
859+
match hir.find_parent(expr.hir_id)? {
861860
Node::ExprField(field) => {
862861
if field.ident.name == local.name && field.is_shorthand {
863862
return Some(local.name);
@@ -1040,7 +1039,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
10401039
if let Some(hir::Node::Expr(hir::Expr {
10411040
kind: hir::ExprKind::Assign(..),
10421041
..
1043-
})) = self.tcx.hir().find(self.tcx.hir().parent_id(expr.hir_id))
1042+
})) = self.tcx.hir().find_parent(expr.hir_id)
10441043
{
10451044
if mutability.is_mut() {
10461045
// Suppressing this diagnostic, we'll properly print it in `check_expr_assign`
@@ -1267,9 +1266,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
12671266

12681267
let mut sugg = vec![];
12691268

1270-
if let Some(hir::Node::ExprField(field)) =
1271-
self.tcx.hir().find(self.tcx.hir().parent_id(expr.hir_id))
1272-
{
1269+
if let Some(hir::Node::ExprField(field)) = self.tcx.hir().find_parent(expr.hir_id) {
12731270
// `expr` is a literal field for a struct, only suggest if appropriate
12741271
if field.is_shorthand {
12751272
// This is a field literal

compiler/rustc_hir_typeck/src/expr.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1083,7 +1083,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
10831083
// Do not suggest `if let x = y` as `==` is way more likely to be the intention.
10841084
let hir = self.tcx.hir();
10851085
if let hir::Node::Expr(hir::Expr { kind: ExprKind::If { .. }, .. }) =
1086-
hir.get(hir.parent_id(hir.parent_id(expr.hir_id)))
1086+
hir.get_parent(hir.parent_id(expr.hir_id))
10871087
{
10881088
err.span_suggestion_verbose(
10891089
expr.span.shrink_to_lo(),
@@ -2462,7 +2462,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
24622462
err.span_label(field.span, "method, not a field");
24632463
let expr_is_call =
24642464
if let hir::Node::Expr(hir::Expr { kind: ExprKind::Call(callee, _args), .. }) =
2465-
self.tcx.hir().get(self.tcx.hir().parent_id(expr.hir_id))
2465+
self.tcx.hir().get_parent(expr.hir_id)
24662466
{
24672467
expr.hir_id == callee.hir_id
24682468
} else {

compiler/rustc_hir_typeck/src/fn_ctxt/_impl.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -1435,9 +1435,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
14351435
pub(in super::super) fn expr_in_place(&self, mut expr_id: hir::HirId) -> bool {
14361436
let mut contained_in_place = false;
14371437

1438-
while let hir::Node::Expr(parent_expr) =
1439-
self.tcx.hir().get(self.tcx.hir().parent_id(expr_id))
1440-
{
1438+
while let hir::Node::Expr(parent_expr) = self.tcx.hir().get_parent(expr_id) {
14411439
match &parent_expr.kind {
14421440
hir::ExprKind::Assign(lhs, ..) | hir::ExprKind::AssignOp(_, lhs, ..) => {
14431441
if lhs.hir_id == expr_id {

compiler/rustc_hir_typeck/src/fn_ctxt/checks.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1803,7 +1803,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
18031803
hir_id: call_hir_id,
18041804
span: call_span,
18051805
..
1806-
}) = hir.get(hir.parent_id(expr.hir_id))
1806+
}) = hir.get_parent(expr.hir_id)
18071807
&& callee.hir_id == expr.hir_id
18081808
{
18091809
if self.closure_span_overlaps_error(error, *call_span) {

compiler/rustc_hir_typeck/src/method/suggest.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1451,7 +1451,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
14511451
let filename = tcx.sess.source_map().span_to_filename(span);
14521452

14531453
let parent_node =
1454-
self.tcx.hir().get(self.tcx.hir().parent_id(hir_id));
1454+
self.tcx.hir().get_parent(hir_id);
14551455
let msg = format!(
14561456
"you must specify a type for this binding, like `{}`",
14571457
concrete_type,

compiler/rustc_hir_typeck/src/pat.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -936,7 +936,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
936936
res.descr(),
937937
),
938938
);
939-
match self.tcx.hir().get(self.tcx.hir().parent_id(pat.hir_id)) {
939+
match self.tcx.hir().get_parent(pat.hir_id) {
940940
hir::Node::PatField(..) => {
941941
e.span_suggestion_verbose(
942942
ident.span.shrink_to_hi(),

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

+33-36
Original file line numberDiff line numberDiff line change
@@ -585,45 +585,42 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
585585
let hir::StmtKind::Local(local) = &stmt.kind else { continue; };
586586
local.pat.walk(&mut find_compatible_candidates);
587587
}
588-
match hir.find(hir.parent_id(blk.hir_id)) {
589-
Some(hir::Node::Expr(hir::Expr { hir_id, .. })) => {
590-
match hir.find(hir.parent_id(*hir_id)) {
591-
Some(hir::Node::Arm(hir::Arm { pat, .. })) => {
592-
pat.walk(&mut find_compatible_candidates);
593-
}
594-
Some(
595-
hir::Node::Item(hir::Item { kind: hir::ItemKind::Fn(_, _, body), .. })
596-
| hir::Node::ImplItem(hir::ImplItem {
597-
kind: hir::ImplItemKind::Fn(_, body),
598-
..
599-
})
600-
| hir::Node::TraitItem(hir::TraitItem {
601-
kind: hir::TraitItemKind::Fn(_, hir::TraitFn::Provided(body)),
602-
..
603-
})
604-
| hir::Node::Expr(hir::Expr {
605-
kind: hir::ExprKind::Closure(hir::Closure { body, .. }),
606-
..
607-
}),
608-
) => {
609-
for param in hir.body(*body).params {
610-
param.pat.walk(&mut find_compatible_candidates);
611-
}
612-
}
613-
Some(hir::Node::Expr(hir::Expr {
614-
kind:
615-
hir::ExprKind::If(
616-
hir::Expr { kind: hir::ExprKind::Let(let_), .. },
617-
then_block,
618-
_,
619-
),
588+
match hir.find_parent(blk.hir_id) {
589+
Some(hir::Node::Expr(hir::Expr { hir_id, .. })) => match hir.find_parent(*hir_id) {
590+
Some(hir::Node::Arm(hir::Arm { pat, .. })) => {
591+
pat.walk(&mut find_compatible_candidates);
592+
}
593+
Some(
594+
hir::Node::Item(hir::Item { kind: hir::ItemKind::Fn(_, _, body), .. })
595+
| hir::Node::ImplItem(hir::ImplItem {
596+
kind: hir::ImplItemKind::Fn(_, body), ..
597+
})
598+
| hir::Node::TraitItem(hir::TraitItem {
599+
kind: hir::TraitItemKind::Fn(_, hir::TraitFn::Provided(body)),
600+
..
601+
})
602+
| hir::Node::Expr(hir::Expr {
603+
kind: hir::ExprKind::Closure(hir::Closure { body, .. }),
620604
..
621-
})) if then_block.hir_id == *hir_id => {
622-
let_.pat.walk(&mut find_compatible_candidates);
605+
}),
606+
) => {
607+
for param in hir.body(*body).params {
608+
param.pat.walk(&mut find_compatible_candidates);
623609
}
624-
_ => {}
625610
}
626-
}
611+
Some(hir::Node::Expr(hir::Expr {
612+
kind:
613+
hir::ExprKind::If(
614+
hir::Expr { kind: hir::ExprKind::Let(let_), .. },
615+
then_block,
616+
_,
617+
),
618+
..
619+
})) if then_block.hir_id == *hir_id => {
620+
let_.pat.walk(&mut find_compatible_candidates);
621+
}
622+
_ => {}
623+
},
627624
_ => {}
628625
}
629626

compiler/rustc_lint/src/builtin.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1526,7 +1526,7 @@ impl<'tcx> LateLintPass<'tcx> for UnreachablePub {
15261526

15271527
fn check_field_def(&mut self, cx: &LateContext<'_>, field: &hir::FieldDef<'_>) {
15281528
let map = cx.tcx.hir();
1529-
if matches!(map.get(map.parent_id(field.hir_id)), Node::Variant(_)) {
1529+
if matches!(map.get_parent(field.hir_id), Node::Variant(_)) {
15301530
return;
15311531
}
15321532
self.perform_lint(cx, "field", field.def_id, field.vis_span, false);

compiler/rustc_lint/src/internal.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ impl<'tcx> LateLintPass<'tcx> for TyTyKind {
143143
TyKind::Path(QPath::Resolved(_, path)) => {
144144
if lint_ty_kind_usage(cx, &path.res) {
145145
let hir = cx.tcx.hir();
146-
let span = match hir.find(hir.parent_id(ty.hir_id)) {
146+
let span = match hir.find_parent(ty.hir_id) {
147147
Some(Node::Pat(Pat {
148148
kind:
149149
PatKind::Path(qpath)

compiler/rustc_lint/src/nonstandard_style.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -444,7 +444,7 @@ impl<'tcx> LateLintPass<'tcx> for NonSnakeCase {
444444

445445
fn check_pat(&mut self, cx: &LateContext<'_>, p: &hir::Pat<'_>) {
446446
if let PatKind::Binding(_, hid, ident, _) = p.kind {
447-
if let hir::Node::PatField(field) = cx.tcx.hir().get(cx.tcx.hir().parent_id(hid)) {
447+
if let hir::Node::PatField(field) = cx.tcx.hir().get_parent(hid) {
448448
if !field.is_shorthand {
449449
// Only check if a new name has been introduced, to avoid warning
450450
// on both the struct definition and this pattern.

compiler/rustc_lint/src/types.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -129,8 +129,7 @@ fn lint_overflowing_range_endpoint<'tcx>(
129129
// which are represented as `ExprKind::Struct`.
130130
let par_id = cx.tcx.hir().parent_id(expr.hir_id);
131131
let Node::ExprField(field) = cx.tcx.hir().get(par_id) else { return false };
132-
let field_par_id = cx.tcx.hir().parent_id(field.hir_id);
133-
let Node::Expr(struct_expr) = cx.tcx.hir().get(field_par_id) else { return false };
132+
let Node::Expr(struct_expr) = cx.tcx.hir().get_parent(field.hir_id) else { return false };
134133
if !is_range_literal(struct_expr) {
135134
return false;
136135
};

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

+13-5
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,7 @@ impl<'hir> Map<'hir> {
246246
},
247247
Node::Variant(_) => DefKind::Variant,
248248
Node::Ctor(variant_data) => {
249-
let ctor_of = match self.find(self.parent_id(hir_id)) {
249+
let ctor_of = match self.find_parent(hir_id) {
250250
Some(Node::Item(..)) => def::CtorOf::Struct,
251251
Some(Node::Variant(..)) => def::CtorOf::Variant,
252252
_ => unreachable!(),
@@ -257,7 +257,7 @@ impl<'hir> Map<'hir> {
257257
}
258258
}
259259
Node::AnonConst(_) => {
260-
let inline = match self.find(self.parent_id(hir_id)) {
260+
let inline = match self.find_parent(hir_id) {
261261
Some(Node::Expr(&Expr {
262262
kind: ExprKind::ConstBlock(ref anon_const), ..
263263
})) if anon_const.hir_id == hir_id => true,
@@ -317,6 +317,14 @@ impl<'hir> Map<'hir> {
317317
.unwrap_or_else(|| bug!("No parent for node {:?}", self.node_to_string(hir_id)))
318318
}
319319

320+
pub fn get_parent(self, hir_id: HirId) -> Node<'hir> {
321+
self.get(self.parent_id(hir_id))
322+
}
323+
324+
pub fn find_parent(self, hir_id: HirId) -> Option<Node<'hir>> {
325+
self.find(self.opt_parent_id(hir_id)?)
326+
}
327+
320328
/// Retrieves the `Node` corresponding to `id`, returning `None` if cannot be found.
321329
pub fn find(self, id: HirId) -> Option<Node<'hir>> {
322330
if id.local_id == ItemLocalId::from_u32(0) {
@@ -664,7 +672,7 @@ impl<'hir> Map<'hir> {
664672

665673
/// Checks if the node is left-hand side of an assignment.
666674
pub fn is_lhs(self, id: HirId) -> bool {
667-
match self.find(self.parent_id(id)) {
675+
match self.find_parent(id) {
668676
Some(Node::Expr(expr)) => match expr.kind {
669677
ExprKind::Assign(lhs, _rhs, _span) => lhs.hir_id == id,
670678
_ => false,
@@ -892,7 +900,7 @@ impl<'hir> Map<'hir> {
892900
Node::Pat(&Pat { kind: PatKind::Binding(_, _, ident, _), .. }) => Some(ident),
893901
// A `Ctor` doesn't have an identifier itself, but its parent
894902
// struct/variant does. Compare with `hir::Map::opt_span`.
895-
Node::Ctor(..) => match self.find(self.parent_id(id))? {
903+
Node::Ctor(..) => match self.find_parent(id)? {
896904
Node::Item(item) => Some(item.ident),
897905
Node::Variant(variant) => Some(variant.ident),
898906
_ => unreachable!(),
@@ -1093,7 +1101,7 @@ impl<'hir> Map<'hir> {
10931101
/// Returns the HirId of `N` in `struct Foo<const N: usize = { ... }>` when
10941102
/// called with the HirId for the `{ ... }` anon const
10951103
pub fn opt_const_param_default_param_def_id(self, anon_const: HirId) -> Option<LocalDefId> {
1096-
match self.get(self.parent_id(anon_const)) {
1104+
match self.get_parent(anon_const) {
10971105
Node::GenericParam(GenericParam {
10981106
def_id: param_id,
10991107
kind: GenericParamKind::Const { .. },

compiler/rustc_middle/src/ty/error.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -457,7 +457,7 @@ impl<'tcx> TyCtxt<'tcx> {
457457
.def_id
458458
.as_local()
459459
.map(|id| hir.local_def_id_to_hir_id(id))
460-
.and_then(|id| self.hir().find(self.hir().parent_id(id)))
460+
.and_then(|id| self.hir().find_parent(id))
461461
.as_ref()
462462
.and_then(|node| node.generics())
463463
{

compiler/rustc_trait_selection/src/traits/error_reporting/suggestions.rs

+2-4
Original file line numberDiff line numberDiff line change
@@ -838,8 +838,7 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
838838

839839
let hir = self.tcx.hir();
840840
let hir_id = hir.local_def_id_to_hir_id(def_id.as_local()?);
841-
let parent_node = hir.parent_id(hir_id);
842-
match hir.find(parent_node) {
841+
match hir.find_parent(hir_id) {
843842
Some(hir::Node::Stmt(hir::Stmt { kind: hir::StmtKind::Local(local), .. })) => {
844843
get_name(err, &local.pat.kind)
845844
}
@@ -3287,8 +3286,7 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
32873286
if let hir::ExprKind::Path(hir::QPath::Resolved(None, path)) = expr.kind
32883287
&& let hir::Path { res: hir::def::Res::Local(hir_id), .. } = path
32893288
&& let Some(hir::Node::Pat(binding)) = self.tcx.hir().find(*hir_id)
3290-
&& let parent_hir_id = self.tcx.hir().parent_id(binding.hir_id)
3291-
&& let Some(parent) = self.tcx.hir().find(parent_hir_id)
3289+
&& let Some(parent) = self.tcx.hir().find_parent(binding.hir_id)
32923290
{
32933291
// We've reached the root of the method call chain...
32943292
if let hir::Node::Local(local) = parent

0 commit comments

Comments
 (0)