Skip to content

Commit a34c26e

Browse files
committed
Make body_owned_by return the body directly.
Almost all callers want this anyway, and now we can use it to also return fed bodies
1 parent ceb45d5 commit a34c26e

File tree

38 files changed

+136
-131
lines changed

38 files changed

+136
-131
lines changed

compiler/rustc_borrowck/src/diagnostics/conflict_errors.rs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -399,8 +399,8 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
399399
}
400400
}
401401
let hir = self.infcx.tcx.hir();
402-
if let Some(body_id) = hir.maybe_body_owned_by(self.mir_def_id()) {
403-
let expr = hir.body(body_id).value;
402+
if let Some(body) = hir.maybe_body_owned_by(self.mir_def_id()) {
403+
let expr = body.value;
404404
let place = &self.move_data.move_paths[mpi].place;
405405
let span = place.as_local().map(|local| self.body.local_decls[local].source_info.span);
406406
let mut finder = ExpressionFinder {
@@ -556,11 +556,10 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
556556
// We use the statements were the binding was initialized, and inspect the HIR to look
557557
// for the branching codepaths that aren't covered, to point at them.
558558
let map = self.infcx.tcx.hir();
559-
let body_id = map.body_owned_by(self.mir_def_id());
560-
let body = map.body(body_id);
559+
let body = map.body_owned_by(self.mir_def_id());
561560

562561
let mut visitor = ConditionVisitor { spans: &spans, name: &name, errors: vec![] };
563-
visitor.visit_body(body);
562+
visitor.visit_body(&body);
564563

565564
let mut show_assign_sugg = false;
566565
let isnt_initialized = if let InitializationRequiringAction::PartialAssignment
@@ -665,7 +664,7 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
665664
}
666665

667666
let mut visitor = LetVisitor { decl_span, sugg_span: None };
668-
visitor.visit_body(body);
667+
visitor.visit_body(&body);
669668
if let Some(span) = visitor.sugg_span {
670669
self.suggest_assign_value(&mut err, moved_place, span);
671670
}

compiler/rustc_borrowck/src/diagnostics/mutability_errors.rs

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -647,16 +647,15 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
647647
let hir_map = self.infcx.tcx.hir();
648648
let def_id = self.body.source.def_id();
649649
let Some(local_def_id) = def_id.as_local() else { return };
650-
let Some(body_id) = hir_map.maybe_body_owned_by(local_def_id) else { return };
651-
let body = self.infcx.tcx.hir().body(body_id);
650+
let Some(body) = hir_map.maybe_body_owned_by(local_def_id) else { return };
652651

653652
let mut v = SuggestIndexOperatorAlternativeVisitor {
654653
assign_span: span,
655654
err,
656655
ty,
657656
suggested: false,
658657
};
659-
v.visit_body(body);
658+
v.visit_body(&body);
660659
if !v.suggested {
661660
err.help(format!(
662661
"to modify a `{ty}`, use `.get_mut()`, `.insert()` or the entry API",
@@ -746,9 +745,8 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
746745
// `fn foo(&x: &i32)` -> `fn foo(&(mut x): &i32)`
747746
let def_id = self.body.source.def_id();
748747
if let Some(local_def_id) = def_id.as_local()
749-
&& let Some(body_id) = self.infcx.tcx.hir().maybe_body_owned_by(local_def_id)
750-
&& let body = self.infcx.tcx.hir().body(body_id)
751-
&& let Some(hir_id) = (BindingFinder { span: pat_span }).visit_body(body).break_value()
748+
&& let Some(body) = self.infcx.tcx.hir().maybe_body_owned_by(local_def_id)
749+
&& let Some(hir_id) = (BindingFinder { span: pat_span }).visit_body(&body).break_value()
752750
&& let node = self.infcx.tcx.hir_node(hir_id)
753751
&& let hir::Node::LetStmt(hir::LetStmt {
754752
pat: hir::Pat { kind: hir::PatKind::Ref(_, _), .. },
@@ -867,8 +865,8 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
867865
}
868866
}
869867
}
870-
if let Some(body_id) = hir_map.maybe_body_owned_by(self.mir_def_id())
871-
&& let Block(block, _) = hir_map.body(body_id).value.kind
868+
if let Some(body) = hir_map.maybe_body_owned_by(self.mir_def_id())
869+
&& let Block(block, _) = body.value.kind
872870
{
873871
// `span` corresponds to the expression being iterated, find the `for`-loop desugared
874872
// expression with that span in order to identify potential fixes when encountering a
@@ -1189,10 +1187,9 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
11891187
Some((false, err_label_span, message, _)) => {
11901188
let def_id = self.body.source.def_id();
11911189
let hir_id = if let Some(local_def_id) = def_id.as_local()
1192-
&& let Some(body_id) = self.infcx.tcx.hir().maybe_body_owned_by(local_def_id)
1190+
&& let Some(body) = self.infcx.tcx.hir().maybe_body_owned_by(local_def_id)
11931191
{
1194-
let body = self.infcx.tcx.hir().body(body_id);
1195-
BindingFinder { span: err_label_span }.visit_body(body).break_value()
1192+
BindingFinder { span: err_label_span }.visit_body(&body).break_value()
11961193
} else {
11971194
None
11981195
};

compiler/rustc_borrowck/src/diagnostics/region_errors.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1183,8 +1183,8 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
11831183
#[allow(rustc::untranslatable_diagnostic)] // FIXME: make this translatable
11841184
fn suggest_move_on_borrowing_closure(&self, diag: &mut Diag<'_>) {
11851185
let map = self.infcx.tcx.hir();
1186-
let body_id = map.body_owned_by(self.mir_def_id());
1187-
let expr = &map.body(body_id).value.peel_blocks();
1186+
let body = map.body_owned_by(self.mir_def_id());
1187+
let expr = &body.value.peel_blocks();
11881188
let mut closure_span = None::<rustc_span::Span>;
11891189
match expr.kind {
11901190
hir::ExprKind::MethodCall(.., args, _) => {

compiler/rustc_driver_impl/src/pretty.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ impl<'tcx> pprust_hir::PpAnn for HirTypedAnn<'tcx> {
169169
self.tcx
170170
.hir()
171171
.maybe_body_owned_by(expr.hir_id.owner.def_id)
172-
.map(|body_id| self.tcx.typeck_body(body_id))
172+
.map(|body_id| self.tcx.typeck_body(body_id.id()))
173173
});
174174

175175
if let Some(typeck_results) = typeck_results {

compiler/rustc_hir_analysis/src/check/region.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -896,7 +896,7 @@ pub fn region_scope_tree(tcx: TyCtxt<'_>, def_id: DefId) -> &ScopeTree {
896896
return tcx.region_scope_tree(typeck_root_def_id);
897897
}
898898

899-
let scope_tree = if let Some(body_id) = tcx.hir().maybe_body_owned_by(def_id.expect_local()) {
899+
let scope_tree = if let Some(body) = tcx.hir().maybe_body_owned_by(def_id.expect_local()) {
900900
let mut visitor = RegionResolutionVisitor {
901901
tcx,
902902
scope_tree: ScopeTree::default(),
@@ -907,9 +907,8 @@ pub fn region_scope_tree(tcx: TyCtxt<'_>, def_id: DefId) -> &ScopeTree {
907907
fixup_scopes: vec![],
908908
};
909909

910-
let body = tcx.hir().body(body_id);
911910
visitor.scope_tree.root_body = Some(body.value.hir_id);
912-
visitor.visit_body(body);
911+
visitor.visit_body(&body);
913912
visitor.scope_tree
914913
} else {
915914
ScopeTree::default()

compiler/rustc_hir_typeck/src/_match.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -207,10 +207,9 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
207207
let hir = self.tcx.hir();
208208

209209
// First, check that we're actually in the tail of a function.
210-
let Some(body_id) = hir.maybe_body_owned_by(self.body_id) else {
210+
let Some(body) = hir.maybe_body_owned_by(self.body_id) else {
211211
return;
212212
};
213-
let body = hir.body(body_id);
214213
let hir::ExprKind::Block(block, _) = body.value.kind else {
215214
return;
216215
};

compiler/rustc_hir_typeck/src/demand.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -327,8 +327,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
327327
}
328328

329329
let mut expr_finder = FindExprs { hir_id: local_hir_id, uses: init.into_iter().collect() };
330-
let body =
331-
hir.body(hir.maybe_body_owned_by(self.body_id).expect("expected item to have body"));
330+
let body = hir.body_owned_by(self.body_id);
332331
expr_finder.visit_expr(body.value);
333332

334333
// Replaces all of the variables in the given type with a fresh inference variable.

compiler/rustc_hir_typeck/src/expr.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -909,8 +909,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
909909
// the first place.
910910
assert_ne!(encl_item_id.def_id, encl_body_owner_id);
911911

912-
let encl_body_id = self.tcx.hir().body_owned_by(encl_body_owner_id);
913-
let encl_body = self.tcx.hir().body(encl_body_id);
912+
let encl_body = self.tcx.hir().body_owned_by(encl_body_owner_id);
914913

915914
err.encl_body_span = Some(encl_body.value.span);
916915
err.encl_fn_span = Some(*encl_fn_span);

compiler/rustc_hir_typeck/src/fallback.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -544,9 +544,8 @@ fn compute_unsafe_infer_vars<'a, 'tcx>(
544544
root_ctxt: &'a TypeckRootCtxt<'tcx>,
545545
body_id: LocalDefId,
546546
) -> UnordMap<ty::TyVid, (HirId, Span, UnsafeUseReason)> {
547-
let body_id =
547+
let body =
548548
root_ctxt.tcx.hir().maybe_body_owned_by(body_id).expect("body id must have an owner");
549-
let body = root_ctxt.tcx.hir().body(body_id);
550549
let mut res = UnordMap::default();
551550

552551
struct UnsafeInferVarsVisitor<'a, 'tcx, 'r> {

compiler/rustc_hir_typeck/src/fn_ctxt/suggestions.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1973,8 +1973,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
19731973
*expr
19741974
} else {
19751975
let body_def_id = hir.enclosing_body_owner(expr.hir_id);
1976-
let body_id = hir.body_owned_by(body_def_id);
1977-
let body = hir.body(body_id);
1976+
let body = hir.body_owned_by(body_def_id);
19781977

19791978
// Get tail expr of the body
19801979
match body.value.kind {

0 commit comments

Comments
 (0)