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

+5-6
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

+8-11
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

+2-2
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

+1-1
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

+2-3
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

+1-2
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

+1-2
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

+1-2
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

+1-2
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

+1-2
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 {

compiler/rustc_hir_typeck/src/method/suggest.rs

+4-8
Original file line numberDiff line numberDiff line change
@@ -505,9 +505,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
505505
&& let hir::def::Res::Local(recv_id) = path.res
506506
&& let Some(segment) = path.segments.first()
507507
{
508-
let map = self.infcx.tcx.hir();
509-
let body_id = self.tcx.hir().body_owned_by(self.body_id);
510-
let body = map.body(body_id);
508+
let body = self.tcx.hir().body_owned_by(self.body_id);
511509

512510
if let Node::Expr(call_expr) = self.tcx.parent_hir_node(rcvr.hir_id) {
513511
let mut let_visitor = LetVisitor {
@@ -518,7 +516,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
518516
method_name,
519517
sugg_let: None,
520518
};
521-
let_visitor.visit_body(body);
519+
let_visitor.visit_body(&body);
522520
if let Some(sugg_let) = let_visitor.sugg_let
523521
&& let Some(self_ty) = self.node_ty_opt(sugg_let.init_hir_id)
524522
{
@@ -2429,9 +2427,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
24292427
seg1.ident.span,
24302428
StashKey::CallAssocMethod,
24312429
|err| {
2432-
let map = self.infcx.tcx.hir();
2433-
let body_id = self.tcx.hir().body_owned_by(self.body_id);
2434-
let body = map.body(body_id);
2430+
let body = self.tcx.hir().body_owned_by(self.body_id);
24352431
struct LetVisitor {
24362432
ident_name: Symbol,
24372433
}
@@ -2453,7 +2449,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
24532449

24542450
if let Node::Expr(call_expr) = self.tcx.parent_hir_node(seg1.hir_id)
24552451
&& let ControlFlow::Break(Some(expr)) =
2456-
(LetVisitor { ident_name: seg1.ident.name }).visit_body(body)
2452+
(LetVisitor { ident_name: seg1.ident.name }).visit_body(&body)
24572453
&& let Some(self_ty) = self.node_ty_opt(expr.hir_id)
24582454
{
24592455
let probe = self.lookup_probe_for_diagnostic(

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -457,10 +457,10 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
457457
};
458458

459459
let mut local_visitor = FindInferSourceVisitor::new(self, typeck_results, arg);
460-
if let Some(body_id) = self.tcx.hir().maybe_body_owned_by(
460+
if let Some(body) = self.tcx.hir().maybe_body_owned_by(
461461
self.tcx.typeck_root_def_id(body_def_id.to_def_id()).expect_local(),
462462
) {
463-
let expr = self.tcx.hir().body(body_id).value;
463+
let expr = body.value;
464464
local_visitor.visit_expr(expr);
465465
}
466466

compiler/rustc_infer/src/infer/error_reporting/nice_region_error/util.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -62,14 +62,13 @@ pub fn find_param_with_region<'tcx>(
6262
_ => {}
6363
}
6464

65-
let body_id = hir.maybe_body_owned_by(def_id)?;
65+
let body = hir.maybe_body_owned_by(def_id)?;
6666

67-
let owner_id = hir.body_owner(body_id);
67+
let owner_id = hir.body_owner(body.id());
6868
let fn_decl = hir.fn_decl_by_hir_id(owner_id)?;
6969
let poly_fn_sig = tcx.fn_sig(id).instantiate_identity();
7070

7171
let fn_sig = tcx.liberate_late_bound_regions(id, poly_fn_sig);
72-
let body = hir.body(body_id);
7372
body.params
7473
.iter()
7574
.take(if fn_sig.c_variadic {

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

+2-3
Original file line numberDiff line numberDiff line change
@@ -580,10 +580,9 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
580580
}
581581
}
582582

583-
self.tcx.hir().maybe_body_owned_by(cause.body_id).and_then(|body_id| {
584-
let body = self.tcx.hir().body(body_id);
583+
self.tcx.hir().maybe_body_owned_by(cause.body_id).and_then(|body| {
585584
IfVisitor { err_span: span, found_if: false }
586-
.visit_body(body)
585+
.visit_body(&body)
587586
.is_break()
588587
.then(|| TypeErrorAdditionalDiags::AddLetForLetChains { span: span.shrink_to_lo() })
589588
})

compiler/rustc_metadata/src/rmeta/encoder.rs

+7-7
Original file line numberDiff line numberDiff line change
@@ -1677,9 +1677,9 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
16771677
if should_encode_const(tcx.def_kind(def_id)) {
16781678
let qualifs = tcx.mir_const_qualif(def_id);
16791679
record!(self.tables.mir_const_qualif[def_id.to_def_id()] <- qualifs);
1680-
let body_id = tcx.hir().maybe_body_owned_by(def_id);
1681-
if let Some(body_id) = body_id {
1682-
let const_data = rendered_const(self.tcx, body_id);
1680+
let body = tcx.hir().maybe_body_owned_by(def_id);
1681+
if let Some(body) = body {
1682+
let const_data = rendered_const(self.tcx, &body, def_id);
16831683
record!(self.tables.rendered_const[def_id.to_def_id()] <- const_data);
16841684
}
16851685
}
@@ -2368,9 +2368,9 @@ pub fn provide(providers: &mut Providers) {
23682368
/// Whenever possible, prefer to evaluate the constant first and try to
23692369
/// use a different method for pretty-printing. Ideally this function
23702370
/// should only ever be used as a fallback.
2371-
pub fn rendered_const<'tcx>(tcx: TyCtxt<'tcx>, body: hir::BodyId) -> String {
2371+
pub fn rendered_const<'tcx>(tcx: TyCtxt<'tcx>, body: &hir::Body<'_>, def_id: LocalDefId) -> String {
23722372
let hir = tcx.hir();
2373-
let value = &hir.body(body).value;
2373+
let value = body.value;
23742374

23752375
#[derive(PartialEq, Eq)]
23762376
enum Classification {
@@ -2426,13 +2426,13 @@ pub fn rendered_const<'tcx>(tcx: TyCtxt<'tcx>, body: hir::BodyId) -> String {
24262426

24272427
// Otherwise we prefer pretty-printing to get rid of extraneous whitespace, comments and
24282428
// other formatting artifacts.
2429-
Literal | Simple => id_to_string(&hir, body.hir_id),
2429+
Literal | Simple => id_to_string(&hir, body.id().hir_id),
24302430

24312431
// FIXME: Omit the curly braces if the enclosing expression is an array literal
24322432
// with a repeated element (an `ExprKind::Repeat`) as in such case it
24332433
// would not actually need any disambiguation.
24342434
Complex => {
2435-
if tcx.def_kind(hir.body_owner_def_id(body).to_def_id()) == DefKind::AnonConst {
2435+
if tcx.def_kind(def_id) == DefKind::AnonConst {
24362436
"{ _ }".to_owned()
24372437
} else {
24382438
"_".to_owned()

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

+18-3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
use std::borrow::Cow;
2+
13
use crate::hir::ModuleItems;
24
use crate::middle::debugger_visualizer::DebuggerVisualizerFile;
35
use crate::query::LocalCrate;
@@ -254,13 +256,26 @@ impl<'hir> Map<'hir> {
254256

255257
/// Given a `LocalDefId`, returns the `BodyId` associated with it,
256258
/// if the node is a body owner, otherwise returns `None`.
257-
pub fn maybe_body_owned_by(self, id: LocalDefId) -> Option<BodyId> {
258-
self.tcx.hir_node_by_def_id(id).body_id()
259+
pub fn maybe_body_owned_by(self, id: LocalDefId) -> Option<Cow<'hir, Body<'hir>>> {
260+
Some(match self.tcx.def_kind(id) {
261+
// Inline consts do not have bodies of their own, so create one to make the follow-up logic simpler.
262+
DefKind::InlineConst => {
263+
let e = self.expect_expr(self.tcx.local_def_id_to_hir_id(id));
264+
Cow::Owned(Body {
265+
params: &[],
266+
value: match e.kind {
267+
ExprKind::ConstBlock(body) => body,
268+
_ => span_bug!(e.span, "InlineConst was not a ConstBlock: {e:#?}"),
269+
},
270+
})
271+
}
272+
_ => Cow::Borrowed(self.body(self.tcx.hir_node_by_def_id(id).body_id()?)),
273+
})
259274
}
260275

261276
/// Given a body owner's id, returns the `BodyId` associated with it.
262277
#[track_caller]
263-
pub fn body_owned_by(self, id: LocalDefId) -> BodyId {
278+
pub fn body_owned_by(self, id: LocalDefId) -> Cow<'hir, Body<'hir>> {
264279
self.maybe_body_owned_by(id).unwrap_or_else(|| {
265280
let hir_id = self.tcx.local_def_id_to_hir_id(id);
266281
span_bug!(

compiler/rustc_middle/src/hir/mod.rs

+7-4
Original file line numberDiff line numberDiff line change
@@ -194,8 +194,7 @@ pub fn provide(providers: &mut Providers) {
194194
};
195195
providers.fn_arg_names = |tcx, def_id| {
196196
let hir = tcx.hir();
197-
let hir_id = tcx.local_def_id_to_hir_id(def_id);
198-
if let Some(body_id) = hir.maybe_body_owned_by(def_id) {
197+
if let Some(body_id) = tcx.hir_node_by_def_id(def_id).body_id() {
199198
tcx.arena.alloc_from_iter(hir.body_param_names(body_id))
200199
} else if let Node::TraitItem(&TraitItem {
201200
kind: TraitItemKind::Fn(_, TraitFn::Required(idents)),
@@ -204,11 +203,15 @@ pub fn provide(providers: &mut Providers) {
204203
| Node::ForeignItem(&ForeignItem {
205204
kind: ForeignItemKind::Fn(_, idents, _),
206205
..
207-
}) = tcx.hir_node(hir_id)
206+
}) = tcx.hir_node(tcx.local_def_id_to_hir_id(def_id))
208207
{
209208
idents
210209
} else {
211-
span_bug!(hir.span(hir_id), "fn_arg_names: unexpected item {:?}", def_id);
210+
span_bug!(
211+
hir.span(tcx.local_def_id_to_hir_id(def_id)),
212+
"fn_arg_names: unexpected item {:?}",
213+
def_id
214+
);
212215
}
213216
};
214217
providers.all_local_trait_impls = |tcx, ()| &tcx.resolutions(()).trait_impls;

compiler/rustc_mir_build/src/build/mod.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -452,7 +452,7 @@ fn construct_fn<'tcx>(
452452
assert_eq!(expr.as_usize(), thir.exprs.len() - 1);
453453

454454
// Figure out what primary body this item has.
455-
let body_id = tcx.hir().body_owned_by(fn_def);
455+
let body = tcx.hir().body_owned_by(fn_def);
456456
let span_with_body = tcx.hir().span_with_body(fn_id);
457457
let return_ty_span = tcx
458458
.hir()
@@ -512,9 +512,9 @@ fn construct_fn<'tcx>(
512512
);
513513

514514
let call_site_scope =
515-
region::Scope { id: body_id.hir_id.local_id, data: region::ScopeData::CallSite };
515+
region::Scope { id: body.id().hir_id.local_id, data: region::ScopeData::CallSite };
516516
let arg_scope =
517-
region::Scope { id: body_id.hir_id.local_id, data: region::ScopeData::Arguments };
517+
region::Scope { id: body.id().hir_id.local_id, data: region::ScopeData::Arguments };
518518
let source_info = builder.source_info(span);
519519
let call_site_s = (call_site_scope, source_info);
520520
unpack!(builder.in_scope(call_site_s, LintLevel::Inherited, |builder| {

0 commit comments

Comments
 (0)