Skip to content

Commit 057c469

Browse files
committed
Move some methods from tcx.hir() to tcx
Renamings: - find -> opt_hir_node - get -> hir_node - find_by_def_id -> opt_hir_node_by_def_id - get_by_def_id -> hir_node_by_def_id
1 parent 1670ff6 commit 057c469

File tree

118 files changed

+368
-366
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

118 files changed

+368
-366
lines changed

Diff for: compiler/rustc_borrowck/src/diagnostics/conflict_errors.rs

+16-14
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ use rustc_middle::mir::{
1818
PlaceRef, ProjectionElem, Rvalue, Statement, StatementKind, Terminator, TerminatorKind,
1919
VarBindingForm,
2020
};
21-
use rustc_middle::ty::{self, suggest_constraining_type_params, PredicateKind, Ty};
21+
use rustc_middle::ty::{self, suggest_constraining_type_params, PredicateKind, Ty, TyCtxt};
2222
use rustc_middle::util::CallKind;
2323
use rustc_mir_dataflow::move_paths::{InitKind, MoveOutIndex, MovePathIndex};
2424
use rustc_span::def_id::LocalDefId;
@@ -398,7 +398,7 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
398398
}
399399
let typeck = self.infcx.tcx.typeck(self.mir_def_id());
400400
let hir_id = hir.parent_id(expr.hir_id);
401-
if let Some(parent) = hir.find(hir_id) {
401+
if let Some(parent) = self.infcx.tcx.opt_hir_node(hir_id) {
402402
let (def_id, args, offset) = if let hir::Node::Expr(parent_expr) = parent
403403
&& let hir::ExprKind::MethodCall(_, _, args, _) = parent_expr.kind
404404
&& let Some(def_id) = typeck.type_dependent_def_id(parent_expr.hir_id)
@@ -413,7 +413,10 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
413413
(None, &[][..], 0)
414414
};
415415
if let Some(def_id) = def_id
416-
&& let Some(node) = hir.find(self.infcx.tcx.local_def_id_to_hir_id(def_id))
416+
&& let Some(node) = self
417+
.infcx
418+
.tcx
419+
.opt_hir_node(self.infcx.tcx.local_def_id_to_hir_id(def_id))
417420
&& let Some(fn_sig) = node.fn_sig()
418421
&& let Some(ident) = node.ident()
419422
&& let Some(pos) = args.iter().position(|arg| arg.hir_id == expr.hir_id)
@@ -1317,7 +1320,7 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
13171320
let tcx = self.infcx.tcx;
13181321
let hir = tcx.hir();
13191322

1320-
let Some(body_id) = hir.get(self.mir_hir_id()).body_id() else { return };
1323+
let Some(body_id) = tcx.hir_node(self.mir_hir_id()).body_id() else { return };
13211324
let typeck_results = tcx.typeck(self.mir_def_id());
13221325

13231326
struct ExprFinder<'hir> {
@@ -1509,7 +1512,7 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
15091512
let local_ty = self.body.local_decls[local].ty;
15101513

15111514
// Get the body the error happens in
1512-
let Some(body_id) = hir.get(self.mir_hir_id()).body_id() else { return };
1515+
let Some(body_id) = tcx.hir_node(self.mir_hir_id()).body_id() else { return };
15131516

15141517
let body_expr = hir.body(body_id).value;
15151518

@@ -1558,7 +1561,7 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
15581561
// Check that the parent of the closure is a method call,
15591562
// with receiver matching with local's type (modulo refs)
15601563
let parent = hir.parent_id(closure_expr.hir_id);
1561-
if let hir::Node::Expr(parent) = hir.get(parent) {
1564+
if let hir::Node::Expr(parent) = tcx.hir_node(parent) {
15621565
if let hir::ExprKind::MethodCall(_, recv, ..) = parent.kind {
15631566
let recv_ty = typeck_results.expr_ty(recv);
15641567

@@ -1635,15 +1638,14 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
16351638
issued_spans: &UseSpans<'tcx>,
16361639
) {
16371640
let UseSpans::ClosureUse { capture_kind_span, .. } = issued_spans else { return };
1638-
let hir = self.infcx.tcx.hir();
16391641

1640-
struct ExpressionFinder<'hir> {
1642+
struct ExpressionFinder<'tcx> {
16411643
capture_span: Span,
16421644
closure_change_spans: Vec<Span>,
16431645
closure_arg_span: Option<Span>,
16441646
in_closure: bool,
16451647
suggest_arg: String,
1646-
hir: rustc_middle::hir::map::Map<'hir>,
1648+
tcx: TyCtxt<'tcx>,
16471649
closure_local_id: Option<hir::HirId>,
16481650
closure_call_changes: Vec<(Span, String)>,
16491651
}
@@ -1657,7 +1659,7 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
16571659
fn_decl: hir::FnDecl { inputs, .. },
16581660
..
16591661
}) = e.kind
1660-
&& let Some(hir::Node::Expr(body)) = self.hir.find(body.hir_id)
1662+
&& let Some(hir::Node::Expr(body)) = self.tcx.opt_hir_node(body.hir_id)
16611663
{
16621664
self.suggest_arg = "this: &Self".to_string();
16631665
if inputs.len() > 0 {
@@ -1722,8 +1724,8 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
17221724
if let Some(hir::Node::ImplItem(hir::ImplItem {
17231725
kind: hir::ImplItemKind::Fn(_fn_sig, body_id),
17241726
..
1725-
})) = hir.find(self.mir_hir_id())
1726-
&& let Some(hir::Node::Expr(expr)) = hir.find(body_id.hir_id)
1727+
})) = self.infcx.tcx.opt_hir_node(self.mir_hir_id())
1728+
&& let Some(hir::Node::Expr(expr)) = self.infcx.tcx.opt_hir_node(body_id.hir_id)
17271729
{
17281730
let mut finder = ExpressionFinder {
17291731
capture_span: *capture_kind_span,
@@ -1733,7 +1735,7 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
17331735
suggest_arg: String::new(),
17341736
closure_local_id: None,
17351737
closure_call_changes: vec![],
1736-
hir,
1738+
tcx: self.infcx.tcx,
17371739
};
17381740
finder.visit_expr(expr);
17391741

@@ -2294,7 +2296,7 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
22942296
let proper_span = proper_span.source_callsite();
22952297
if let Some(scope) = self.body.source_scopes.get(source_info.scope)
22962298
&& let ClearCrossCrate::Set(scope_data) = &scope.local_data
2297-
&& let Some(node) = self.infcx.tcx.hir().find(scope_data.lint_root)
2299+
&& let Some(node) = self.infcx.tcx.opt_hir_node(scope_data.lint_root)
22982300
&& let Some(id) = node.body_id()
22992301
&& let hir::ExprKind::Block(block, _) = self.infcx.tcx.hir().body(id).value.kind
23002302
{

Diff for: compiler/rustc_borrowck/src/diagnostics/explain_borrow.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ impl<'tcx> BorrowExplanation<'tcx> {
8787
if let hir::ExprKind::Path(hir::QPath::Resolved(None, p)) = expr.kind
8888
&& let [hir::PathSegment { ident, args: None, .. }] = p.segments
8989
&& let hir::def::Res::Local(hir_id) = p.res
90-
&& let Some(hir::Node::Pat(pat)) = tcx.hir().find(hir_id)
90+
&& let Some(hir::Node::Pat(pat)) = tcx.opt_hir_node(hir_id)
9191
{
9292
err.span_label(pat.span, format!("binding `{ident}` declared here"));
9393
}

Diff for: compiler/rustc_borrowck/src/diagnostics/mutability_errors.rs

+26-27
Original file line numberDiff line numberDiff line change
@@ -393,7 +393,7 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
393393

394394
let upvar_hir_id = captured_place.get_root_variable();
395395

396-
if let Some(Node::Pat(pat)) = self.infcx.tcx.hir().find(upvar_hir_id)
396+
if let Some(Node::Pat(pat)) = self.infcx.tcx.opt_hir_node(upvar_hir_id)
397397
&& let hir::PatKind::Binding(hir::BindingAnnotation::NONE, _, upvar_ident, _) =
398398
pat.kind
399399
{
@@ -658,7 +658,6 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
658658
if self.body.local_kind(local) != LocalKind::Arg {
659659
return (false, None);
660660
}
661-
let hir_map = self.infcx.tcx.hir();
662661
let my_def = self.body.source.def_id();
663662
let my_hir = self.infcx.tcx.local_def_id_to_hir_id(my_def.as_local().unwrap());
664663
let Some(td) =
@@ -668,7 +667,7 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
668667
};
669668
(
670669
true,
671-
td.as_local().and_then(|tld| match hir_map.find_by_def_id(tld) {
670+
td.as_local().and_then(|tld| match self.infcx.tcx.opt_hir_node_by_def_id(tld) {
672671
Some(Node::Item(hir::Item {
673672
kind: hir::ItemKind::Trait(_, _, _, _, items),
674673
..
@@ -679,25 +678,27 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
679678
if !matches!(k, hir::AssocItemKind::Fn { .. }) {
680679
continue;
681680
}
682-
if hir_map.name(hi) != hir_map.name(my_hir) {
681+
if self.infcx.tcx.hir().name(hi) != self.infcx.tcx.hir().name(my_hir) {
683682
continue;
684683
}
685684
f_in_trait_opt = Some(hi);
686685
break;
687686
}
688-
f_in_trait_opt.and_then(|f_in_trait| match hir_map.find(f_in_trait) {
689-
Some(Node::TraitItem(hir::TraitItem {
690-
kind:
691-
hir::TraitItemKind::Fn(
692-
hir::FnSig { decl: hir::FnDecl { inputs, .. }, .. },
693-
_,
694-
),
695-
..
696-
})) => {
697-
let hir::Ty { span, .. } = inputs[local.index() - 1];
698-
Some(span)
687+
f_in_trait_opt.and_then(|f_in_trait| {
688+
match self.infcx.tcx.opt_hir_node(f_in_trait) {
689+
Some(Node::TraitItem(hir::TraitItem {
690+
kind:
691+
hir::TraitItemKind::Fn(
692+
hir::FnSig { decl: hir::FnDecl { inputs, .. }, .. },
693+
_,
694+
),
695+
..
696+
})) => {
697+
let hir::Ty { span, .. } = inputs[local.index() - 1];
698+
Some(span)
699+
}
700+
_ => None,
699701
}
700-
_ => None,
701702
})
702703
}
703704
_ => None,
@@ -738,12 +739,11 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
738739
}
739740
}
740741

741-
let hir_map = self.infcx.tcx.hir();
742742
let def_id = self.body.source.def_id();
743743
let hir_id = if let Some(local_def_id) = def_id.as_local()
744-
&& let Some(body_id) = hir_map.maybe_body_owned_by(local_def_id)
744+
&& let Some(body_id) = self.infcx.tcx.hir().maybe_body_owned_by(local_def_id)
745745
{
746-
let body = hir_map.body(body_id);
746+
let body = self.infcx.tcx.hir().body(body_id);
747747
let mut v = BindingFinder { span: pat_span, hir_id: None };
748748
v.visit_body(body);
749749
v.hir_id
@@ -759,7 +759,7 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
759759
&& let Some(hir::Node::Local(hir::Local {
760760
pat: hir::Pat { kind: hir::PatKind::Ref(_, _), .. },
761761
..
762-
})) = hir_map.find(hir_id)
762+
})) = self.infcx.tcx.opt_hir_node(hir_id)
763763
&& let Ok(name) =
764764
self.infcx.tcx.sess.source_map().span_to_snippet(local_decl.source_info.span)
765765
{
@@ -939,7 +939,7 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
939939
let closure_id = self.mir_hir_id();
940940
let closure_span = self.infcx.tcx.def_span(self.mir_def_id());
941941
let fn_call_id = hir.parent_id(closure_id);
942-
let node = hir.get(fn_call_id);
942+
let node = self.infcx.tcx.hir_node(fn_call_id);
943943
let def_id = hir.enclosing_body_owner(fn_call_id);
944944
let mut look_at_return = true;
945945
// If we can detect the expression to be an `fn` call where the closure was an argument,
@@ -998,7 +998,7 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
998998
if look_at_return && hir.get_return_block(closure_id).is_some() {
999999
// ...otherwise we are probably in the tail expression of the function, point at the
10001000
// return type.
1001-
match hir.get_by_def_id(hir.get_parent_item(fn_call_id).def_id) {
1001+
match self.infcx.tcx.hir_node_by_def_id(hir.get_parent_item(fn_call_id).def_id) {
10021002
hir::Node::Item(hir::Item { ident, kind: hir::ItemKind::Fn(sig, ..), .. })
10031003
| hir::Node::TraitItem(hir::TraitItem {
10041004
ident,
@@ -1196,12 +1196,11 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
11961196
hir::intravisit::walk_stmt(self, s);
11971197
}
11981198
}
1199-
let hir_map = self.infcx.tcx.hir();
12001199
let def_id = self.body.source.def_id();
12011200
let hir_id = if let Some(local_def_id) = def_id.as_local()
1202-
&& let Some(body_id) = hir_map.maybe_body_owned_by(local_def_id)
1201+
&& let Some(body_id) = self.infcx.tcx.hir().maybe_body_owned_by(local_def_id)
12031202
{
1204-
let body = hir_map.body(body_id);
1203+
let body = self.infcx.tcx.hir().body(body_id);
12051204
let mut v = BindingFinder { span: err_label_span, hir_id: None };
12061205
v.visit_body(body);
12071206
v.hir_id
@@ -1210,7 +1209,7 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
12101209
};
12111210

12121211
if let Some(hir_id) = hir_id
1213-
&& let Some(hir::Node::Local(local)) = hir_map.find(hir_id)
1212+
&& let Some(hir::Node::Local(local)) = self.infcx.tcx.opt_hir_node(hir_id)
12141213
{
12151214
let (changing, span, sugg) = match local.ty {
12161215
Some(ty) => ("changing", ty.span, message),
@@ -1396,7 +1395,7 @@ fn get_mut_span_in_struct_field<'tcx>(
13961395
&& let ty::Adt(def, _) = ty.kind()
13971396
&& let field = def.all_fields().nth(field.index())?
13981397
// Use the HIR types to construct the diagnostic message.
1399-
&& let node = tcx.hir().find_by_def_id(field.did.as_local()?)?
1398+
&& let node = tcx.opt_hir_node_by_def_id(field.did.as_local()?)?
14001399
// Now we're dealing with the actual struct that we're going to suggest a change to,
14011400
// we can expect a field that is an immutable reference to a type.
14021401
&& let hir::Node::Field(field) = node

Diff for: compiler/rustc_borrowck/src/diagnostics/region_name.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -672,7 +672,7 @@ impl<'tcx> MirBorrowckCtxt<'_, 'tcx> {
672672

673673
let mir_hir_id = self.mir_hir_id();
674674

675-
let (return_span, mir_description, hir_ty) = match hir.get(mir_hir_id) {
675+
let (return_span, mir_description, hir_ty) = match tcx.hir_node(mir_hir_id) {
676676
hir::Node::Expr(hir::Expr {
677677
kind: hir::ExprKind::Closure(&hir::Closure { fn_decl, body, fn_decl_span, .. }),
678678
..
@@ -689,7 +689,7 @@ impl<'tcx> MirBorrowckCtxt<'_, 'tcx> {
689689
hir::CoroutineSource::Closure => " of async closure",
690690
hir::CoroutineSource::Fn => {
691691
let parent_item =
692-
hir.get_by_def_id(hir.get_parent_item(mir_hir_id).def_id);
692+
tcx.hir_node_by_def_id(hir.get_parent_item(mir_hir_id).def_id);
693693
let output = &parent_item
694694
.fn_decl()
695695
.expect("coroutine lowered from async fn should be in fn")
@@ -706,7 +706,7 @@ impl<'tcx> MirBorrowckCtxt<'_, 'tcx> {
706706
hir::CoroutineSource::Closure => " of gen closure",
707707
hir::CoroutineSource::Fn => {
708708
let parent_item =
709-
hir.get_by_def_id(hir.get_parent_item(mir_hir_id).def_id);
709+
tcx.hir_node_by_def_id(hir.get_parent_item(mir_hir_id).def_id);
710710
let output = &parent_item
711711
.fn_decl()
712712
.expect("coroutine lowered from gen fn should be in fn")
@@ -826,7 +826,7 @@ impl<'tcx> MirBorrowckCtxt<'_, 'tcx> {
826826
let type_name =
827827
self.infcx.extract_inference_diagnostics_data(yield_ty.into(), Some(highlight)).name;
828828

829-
let yield_span = match tcx.hir().get(self.mir_hir_id()) {
829+
let yield_span = match tcx.hir_node(self.mir_hir_id()) {
830830
hir::Node::Expr(hir::Expr {
831831
kind: hir::ExprKind::Closure(&hir::Closure { fn_decl_span, .. }),
832832
..

Diff for: compiler/rustc_const_eval/src/const_eval/fn_queries.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ pub fn is_parent_const_impl_raw(tcx: TyCtxt<'_>, def_id: LocalDefId) -> bool {
3333
/// return if it has a `const` modifier. If it is an intrinsic, report whether said intrinsic
3434
/// has a `rustc_const_{un,}stable` attribute. Otherwise, return `Constness::NotConst`.
3535
fn constness(tcx: TyCtxt<'_>, def_id: LocalDefId) -> hir::Constness {
36-
let node = tcx.hir().get_by_def_id(def_id);
36+
let node = tcx.hir_node_by_def_id(def_id);
3737

3838
match node {
3939
hir::Node::Ctor(_)

Diff for: compiler/rustc_const_eval/src/interpret/validity.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,7 @@ impl<'rt, 'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> ValidityVisitor<'rt, 'mir, '
227227
// Sometimes the index is beyond the number of upvars (seen
228228
// for a coroutine).
229229
let var_hir_id = captured_place.get_root_variable();
230-
let node = self.ecx.tcx.hir().get(var_hir_id);
230+
let node = self.ecx.tcx.hir_node(var_hir_id);
231231
if let hir::Node::Pat(pat) = node {
232232
if let hir::PatKind::Binding(_, _, ident, _) = pat.kind {
233233
name = Some(ident.name);

Diff for: compiler/rustc_const_eval/src/transform/check_consts/ops.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ impl<'tcx> NonConstOp<'tcx> for FnCallNonConst<'tcx> {
120120
Param(param_ty) => {
121121
debug!(?param_ty);
122122
let caller_hir_id = tcx.local_def_id_to_hir_id(caller);
123-
if let Some(generics) = tcx.hir().get(caller_hir_id).generics() {
123+
if let Some(generics) = tcx.hir_node(caller_hir_id).generics() {
124124
let constraint = with_no_trimmed_paths!(format!(
125125
"~const {}",
126126
trait_ref.print_only_trait_path()

Diff for: compiler/rustc_hir_analysis/src/astconv/lint.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
2424
..
2525
}),
2626
..
27-
}) = tcx.hir().get_by_def_id(parent_id)
27+
}) = tcx.hir_node_by_def_id(parent_id)
2828
&& self_ty.hir_id == impl_self_ty.hir_id
2929
{
3030
if !of_trait_ref.trait_def_id().is_some_and(|def_id| def_id.is_local()) {

Diff for: compiler/rustc_hir_analysis/src/astconv/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2757,7 +2757,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
27572757
let hir = tcx.hir();
27582758

27592759
let hir::Node::ImplItem(hir::ImplItem { kind: hir::ImplItemKind::Fn(..), ident, .. }) =
2760-
hir.get(fn_hir_id)
2760+
tcx.hir_node(fn_hir_id)
27612761
else {
27622762
return None;
27632763
};

Diff for: compiler/rustc_hir_analysis/src/check/check.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -507,7 +507,7 @@ fn check_item_type(tcx: TyCtxt<'_>, id: hir::ItemId) {
507507
let origin = tcx.opaque_type_origin(id.owner_id.def_id);
508508
if let hir::OpaqueTyOrigin::FnReturn(fn_def_id)
509509
| hir::OpaqueTyOrigin::AsyncFn(fn_def_id) = origin
510-
&& let hir::Node::TraitItem(trait_item) = tcx.hir().get_by_def_id(fn_def_id)
510+
&& let hir::Node::TraitItem(trait_item) = tcx.hir_node_by_def_id(fn_def_id)
511511
&& let (_, hir::TraitFn::Required(..)) = trait_item.expect_fn()
512512
{
513513
// Skip opaques from RPIT in traits with no default body.
@@ -1176,7 +1176,7 @@ fn detect_discriminant_duplicate<'tcx>(tcx: TyCtxt<'tcx>, adt: ty::AdtDef<'tcx>)
11761176
ty::VariantDiscr::Explicit(discr_def_id) => {
11771177
// In the case the discriminant is both a duplicate and overflowed, let the user know
11781178
if let hir::Node::AnonConst(expr) =
1179-
tcx.hir().get_by_def_id(discr_def_id.expect_local())
1179+
tcx.hir_node_by_def_id(discr_def_id.expect_local())
11801180
&& let hir::ExprKind::Lit(lit) = &tcx.hir().body(expr.body).value.kind
11811181
&& let rustc_ast::LitKind::Int(lit_value, _int_kind) = &lit.node
11821182
&& *lit_value != dis.val

Diff for: compiler/rustc_hir_analysis/src/check/compare_impl_item.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2179,7 +2179,7 @@ pub(super) fn check_type_bounds<'tcx>(
21792179
let impl_ty_span = if impl_ty.is_impl_trait_in_trait() {
21802180
tcx.def_span(impl_ty_def_id)
21812181
} else {
2182-
match tcx.hir().get_by_def_id(impl_ty_def_id) {
2182+
match tcx.hir_node_by_def_id(impl_ty_def_id) {
21832183
hir::Node::TraitItem(hir::TraitItem {
21842184
kind: hir::TraitItemKind::Type(_, Some(ty)),
21852185
..

0 commit comments

Comments
 (0)