Skip to content

Commit d43930d

Browse files
committed
Auto merge of #125711 - oli-obk:const_block_ice2, r=Nadrieril
Make `body_owned_by` return the `Body` instead of just the `BodyId` fixes #125677 Almost all `body_owned_by` callers immediately called `body`, too, so just return `Body` directly. This makes the inline-const query feeding more robust, as all calls to `body_owned_by` will now yield a body for inline consts, too. I have not yet figured out a good way to make `tcx.hir().body()` return an inline-const body, but that can be done as a follow-up
2 parents 32a3ed2 + a34c26e commit d43930d

File tree

54 files changed

+163
-162
lines changed

Some content is hidden

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

54 files changed

+163
-162
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/src/intravisit.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -299,7 +299,7 @@ pub trait Visitor<'v>: Sized {
299299
walk_item(self, i)
300300
}
301301

302-
fn visit_body(&mut self, b: &'v Body<'v>) -> Self::Result {
302+
fn visit_body(&mut self, b: &Body<'v>) -> Self::Result {
303303
walk_body(self, b)
304304
}
305305

@@ -578,7 +578,7 @@ pub fn walk_item<'v, V: Visitor<'v>>(visitor: &mut V, item: &'v Item<'v>) -> V::
578578
V::Result::output()
579579
}
580580

581-
pub fn walk_body<'v, V: Visitor<'v>>(visitor: &mut V, body: &'v Body<'v>) -> V::Result {
581+
pub fn walk_body<'v, V: Visitor<'v>>(visitor: &mut V, body: &Body<'v>) -> V::Result {
582582
walk_list!(visitor, visit_param, body.params);
583583
visitor.visit_expr(body.value)
584584
}

compiler/rustc_hir_analysis/src/check/region.rs

+3-4
Original file line numberDiff line numberDiff line change
@@ -818,7 +818,7 @@ impl<'tcx> Visitor<'tcx> for RegionResolutionVisitor<'tcx> {
818818
resolve_block(self, b);
819819
}
820820

821-
fn visit_body(&mut self, body: &'tcx hir::Body<'tcx>) {
821+
fn visit_body(&mut self, body: &hir::Body<'tcx>) {
822822
let body_id = body.id();
823823
let owner_id = self.tcx.hir().body_owner_def_id(body_id);
824824

@@ -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

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

@@ -1163,7 +1163,7 @@ impl<'a, 'tcx> Visitor<'tcx> for FindInferSourceVisitor<'a, 'tcx> {
11631163

11641164
/// For closures, we first visit the parameters and then the content,
11651165
/// as we prefer those.
1166-
fn visit_body(&mut self, body: &'tcx Body<'tcx>) {
1166+
fn visit_body(&mut self, body: &Body<'tcx>) {
11671167
for param in body.params {
11681168
debug!(
11691169
"param: span {:?}, ty_span {:?}, pat.span {:?}",

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-7
Original file line numberDiff line numberDiff line change
@@ -578,16 +578,11 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
578578
walk_stmt(self, ex)
579579
}
580580
}
581-
582-
fn visit_body(&mut self, body: &'v hir::Body<'v>) -> Self::Result {
583-
hir::intravisit::walk_body(self, body)
584-
}
585581
}
586582

587-
self.tcx.hir().maybe_body_owned_by(cause.body_id).and_then(|body_id| {
588-
let body = self.tcx.hir().body(body_id);
583+
self.tcx.hir().maybe_body_owned_by(cause.body_id).and_then(|body| {
589584
IfVisitor { err_span: span, found_if: false }
590-
.visit_body(body)
585+
.visit_body(&body)
591586
.is_break()
592587
.then(|| TypeErrorAdditionalDiags::AddLetForLetChains { span: span.shrink_to_lo() })
593588
})

compiler/rustc_lint/src/late.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ impl<'tcx, T: LateLintPass<'tcx>> hir_visit::Visitor<'tcx> for LateContextAndPas
125125
});
126126
}
127127

128-
fn visit_body(&mut self, body: &'tcx hir::Body<'tcx>) {
128+
fn visit_body(&mut self, body: &hir::Body<'tcx>) {
129129
lint_callback!(self, check_body, body);
130130
hir_visit::walk_body(self, body);
131131
lint_callback!(self, check_body_post, body);

compiler/rustc_lint/src/non_local_def.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -68,11 +68,11 @@ impl_lint_pass!(NonLocalDefinitions => [NON_LOCAL_DEFINITIONS]);
6868
// instead check_mod is called after every body has been handled.
6969

7070
impl<'tcx> LateLintPass<'tcx> for NonLocalDefinitions {
71-
fn check_body(&mut self, _cx: &LateContext<'tcx>, _body: &'tcx Body<'tcx>) {
71+
fn check_body(&mut self, _cx: &LateContext<'tcx>, _body: &Body<'tcx>) {
7272
self.body_depth += 1;
7373
}
7474

75-
fn check_body_post(&mut self, _cx: &LateContext<'tcx>, _body: &'tcx Body<'tcx>) {
75+
fn check_body_post(&mut self, _cx: &LateContext<'tcx>, _body: &Body<'tcx>) {
7676
self.body_depth -= 1;
7777
}
7878

compiler/rustc_lint/src/passes.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ use rustc_session::lint::LintPass;
77
macro_rules! late_lint_methods {
88
($macro:path, $args:tt) => (
99
$macro!($args, [
10-
fn check_body(a: &'tcx rustc_hir::Body<'tcx>);
11-
fn check_body_post(a: &'tcx rustc_hir::Body<'tcx>);
10+
fn check_body(a: &rustc_hir::Body<'tcx>);
11+
fn check_body_post(a: &rustc_hir::Body<'tcx>);
1212
fn check_crate();
1313
fn check_crate_post();
1414
fn check_mod(a: &'tcx rustc_hir::Mod<'tcx>, b: rustc_hir::HirId);

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()

0 commit comments

Comments
 (0)