Skip to content

Commit 4c1a186

Browse files
authored
Rollup merge of #138354 - lcnr:goodbye-TypeVerifier, r=compiler-errors
remove redundant `body` arguments it's already stored in the `TypeChecker` itself
2 parents 4feb866 + ba6c406 commit 4c1a186

File tree

4 files changed

+153
-173
lines changed

4 files changed

+153
-173
lines changed

compiler/rustc_borrowck/src/type_check/input_output.rs

+26-28
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,9 @@ use crate::universal_regions::DefiningTy;
2424
impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
2525
/// Check explicit closure signature annotation,
2626
/// e.g., `|x: FxIndexMap<_, &'static u32>| ...`.
27-
#[instrument(skip(self, body), level = "debug")]
28-
pub(super) fn check_signature_annotation(&mut self, body: &Body<'tcx>) {
29-
let mir_def_id = body.source.def_id().expect_local();
27+
#[instrument(skip(self), level = "debug")]
28+
pub(super) fn check_signature_annotation(&mut self) {
29+
let mir_def_id = self.body.source.def_id().expect_local();
3030

3131
if !self.tcx().is_closure_like(mir_def_id.to_def_id()) {
3232
return;
@@ -38,9 +38,9 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
3838
// (e.g., the `_` in the code above) with fresh variables.
3939
// Then replace the bound items in the fn sig with fresh variables,
4040
// so that they represent the view from "inside" the closure.
41-
let user_provided_sig = self.instantiate_canonical(body.span, &user_provided_poly_sig);
41+
let user_provided_sig = self.instantiate_canonical(self.body.span, &user_provided_poly_sig);
4242
let mut user_provided_sig = self.infcx.instantiate_binder_with_fresh_vars(
43-
body.span,
43+
self.body.span,
4444
BoundRegionConversionTime::FnCall,
4545
user_provided_sig,
4646
);
@@ -66,12 +66,13 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
6666
Ty::new_tup(self.tcx(), user_provided_sig.inputs()),
6767
args.tupled_upvars_ty(),
6868
args.coroutine_captures_by_ref_ty(),
69-
self.infcx.next_region_var(RegionVariableOrigin::MiscVariable(body.span), || {
70-
RegionCtxt::Unknown
71-
}),
69+
self.infcx
70+
.next_region_var(RegionVariableOrigin::MiscVariable(self.body.span), || {
71+
RegionCtxt::Unknown
72+
}),
7273
);
7374

74-
let next_ty_var = || self.infcx.next_ty_var(body.span);
75+
let next_ty_var = || self.infcx.next_ty_var(self.body.span);
7576
let output_ty = Ty::new_coroutine(
7677
self.tcx(),
7778
self.tcx().coroutine_for_closure(mir_def_id),
@@ -107,9 +108,10 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
107108
for (&user_ty, arg_decl) in user_provided_sig.inputs().iter().zip_eq(
108109
// In MIR, closure args begin with an implicit `self`.
109110
// Also, coroutines have a resume type which may be implicitly `()`.
110-
body.args_iter()
111+
self.body
112+
.args_iter()
111113
.skip(1 + if is_coroutine_with_implicit_resume_ty { 1 } else { 0 })
112-
.map(|local| &body.local_decls[local]),
114+
.map(|local| &self.body.local_decls[local]),
113115
) {
114116
self.ascribe_user_type_skip_wf(
115117
arg_decl.ty,
@@ -119,20 +121,16 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
119121
}
120122

121123
// If the user explicitly annotated the output type, enforce it.
122-
let output_decl = &body.local_decls[RETURN_PLACE];
124+
let output_decl = &self.body.local_decls[RETURN_PLACE];
123125
self.ascribe_user_type_skip_wf(
124126
output_decl.ty,
125127
ty::UserType::new(ty::UserTypeKind::Ty(user_provided_sig.output())),
126128
output_decl.source_info.span,
127129
);
128130
}
129131

130-
#[instrument(skip(self, body), level = "debug")]
131-
pub(super) fn equate_inputs_and_outputs(
132-
&mut self,
133-
body: &Body<'tcx>,
134-
normalized_inputs_and_output: &[Ty<'tcx>],
135-
) {
132+
#[instrument(skip(self), level = "debug")]
133+
pub(super) fn equate_inputs_and_outputs(&mut self, normalized_inputs_and_output: &[Ty<'tcx>]) {
136134
let (&normalized_output_ty, normalized_input_tys) =
137135
normalized_inputs_and_output.split_last().unwrap();
138136

@@ -141,36 +139,36 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
141139

142140
// Equate expected input tys with those in the MIR.
143141
for (argument_index, &normalized_input_ty) in normalized_input_tys.iter().enumerate() {
144-
if argument_index + 1 >= body.local_decls.len() {
142+
if argument_index + 1 >= self.body.local_decls.len() {
145143
self.tcx()
146144
.dcx()
147-
.span_bug(body.span, "found more normalized_input_ty than local_decls");
145+
.span_bug(self.body.span, "found more normalized_input_ty than local_decls");
148146
}
149147

150148
// In MIR, argument N is stored in local N+1.
151149
let local = Local::from_usize(argument_index + 1);
152150

153-
let mir_input_ty = body.local_decls[local].ty;
151+
let mir_input_ty = self.body.local_decls[local].ty;
154152

155-
let mir_input_span = body.local_decls[local].source_info.span;
153+
let mir_input_span = self.body.local_decls[local].source_info.span;
156154
self.equate_normalized_input_or_output(
157155
normalized_input_ty,
158156
mir_input_ty,
159157
mir_input_span,
160158
);
161159
}
162160

163-
if let Some(mir_yield_ty) = body.yield_ty() {
164-
let yield_span = body.local_decls[RETURN_PLACE].source_info.span;
161+
if let Some(mir_yield_ty) = self.body.yield_ty() {
162+
let yield_span = self.body.local_decls[RETURN_PLACE].source_info.span;
165163
self.equate_normalized_input_or_output(
166164
self.universal_regions.yield_ty.unwrap(),
167165
mir_yield_ty,
168166
yield_span,
169167
);
170168
}
171169

172-
if let Some(mir_resume_ty) = body.resume_ty() {
173-
let yield_span = body.local_decls[RETURN_PLACE].source_info.span;
170+
if let Some(mir_resume_ty) = self.body.resume_ty() {
171+
let yield_span = self.body.local_decls[RETURN_PLACE].source_info.span;
174172
self.equate_normalized_input_or_output(
175173
self.universal_regions.resume_ty.unwrap(),
176174
mir_resume_ty,
@@ -179,8 +177,8 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
179177
}
180178

181179
// Return types are a bit more complex. They may contain opaque `impl Trait` types.
182-
let mir_output_ty = body.local_decls[RETURN_PLACE].ty;
183-
let output_span = body.local_decls[RETURN_PLACE].source_info.span;
180+
let mir_output_ty = self.body.local_decls[RETURN_PLACE].ty;
181+
let output_span = self.body.local_decls[RETURN_PLACE].source_info.span;
184182
self.equate_normalized_input_or_output(normalized_output_ty, mir_output_ty, output_span);
185183
}
186184

compiler/rustc_borrowck/src/type_check/liveness/mod.rs

+6-14
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ mod trace;
3131
/// performed before
3232
pub(super) fn generate<'a, 'tcx>(
3333
typeck: &mut TypeChecker<'_, 'tcx>,
34-
body: &Body<'tcx>,
3534
location_map: &DenseLocationMap,
3635
flow_inits: ResultsCursor<'a, 'tcx, MaybeInitializedPlaces<'a, 'tcx>>,
3736
move_data: &MoveData<'tcx>,
@@ -51,23 +50,16 @@ pub(super) fn generate<'a, 'tcx>(
5150
// We do record these regions in the polonius context, since they're used to differentiate
5251
// relevant and boring locals, which is a key distinction used later in diagnostics.
5352
if typeck.tcx().sess.opts.unstable_opts.polonius.is_next_enabled() {
54-
let (_, boring_locals) = compute_relevant_live_locals(typeck.tcx(), &free_regions, body);
53+
let (_, boring_locals) =
54+
compute_relevant_live_locals(typeck.tcx(), &free_regions, typeck.body);
5555
typeck.polonius_liveness.as_mut().unwrap().boring_nll_locals =
5656
boring_locals.into_iter().collect();
5757
free_regions = typeck.universal_regions.universal_regions_iter().collect();
5858
}
5959
let (relevant_live_locals, boring_locals) =
60-
compute_relevant_live_locals(typeck.tcx(), &free_regions, body);
61-
62-
trace::trace(
63-
typeck,
64-
body,
65-
location_map,
66-
flow_inits,
67-
move_data,
68-
relevant_live_locals,
69-
boring_locals,
70-
);
60+
compute_relevant_live_locals(typeck.tcx(), &free_regions, typeck.body);
61+
62+
trace::trace(typeck, location_map, flow_inits, move_data, relevant_live_locals, boring_locals);
7163

7264
// Mark regions that should be live where they appear within rvalues or within a call: like
7365
// args, regions, and types.
@@ -76,7 +68,7 @@ pub(super) fn generate<'a, 'tcx>(
7668
&mut typeck.constraints.liveness_constraints,
7769
&typeck.universal_regions,
7870
&mut typeck.polonius_liveness,
79-
body,
71+
typeck.body,
8072
);
8173
}
8274

compiler/rustc_borrowck/src/type_check/liveness/trace.rs

+21-21
Original file line numberDiff line numberDiff line change
@@ -39,17 +39,15 @@ use crate::type_check::{NormalizeLocation, TypeChecker};
3939
/// this respects `#[may_dangle]` annotations).
4040
pub(super) fn trace<'a, 'tcx>(
4141
typeck: &mut TypeChecker<'_, 'tcx>,
42-
body: &Body<'tcx>,
4342
location_map: &DenseLocationMap,
4443
flow_inits: ResultsCursor<'a, 'tcx, MaybeInitializedPlaces<'a, 'tcx>>,
4544
move_data: &MoveData<'tcx>,
4645
relevant_live_locals: Vec<Local>,
4746
boring_locals: Vec<Local>,
4847
) {
49-
let local_use_map = &LocalUseMap::build(&relevant_live_locals, location_map, body);
48+
let local_use_map = &LocalUseMap::build(&relevant_live_locals, location_map, typeck.body);
5049
let cx = LivenessContext {
5150
typeck,
52-
body,
5351
flow_inits,
5452
location_map,
5553
local_use_map,
@@ -69,14 +67,13 @@ pub(super) fn trace<'a, 'tcx>(
6967
/// Contextual state for the type-liveness coroutine.
7068
struct LivenessContext<'a, 'typeck, 'b, 'tcx> {
7169
/// Current type-checker, giving us our inference context etc.
70+
///
71+
/// This also stores the body we're currently analyzing.
7272
typeck: &'a mut TypeChecker<'typeck, 'tcx>,
7373

7474
/// Defines the `PointIndex` mapping
7575
location_map: &'a DenseLocationMap,
7676

77-
/// MIR we are analyzing.
78-
body: &'a Body<'tcx>,
79-
8077
/// Mapping to/from the various indices used for initialization tracking.
8178
move_data: &'a MoveData<'tcx>,
8279

@@ -139,7 +136,7 @@ impl<'a, 'typeck, 'b, 'tcx> LivenessResults<'a, 'typeck, 'b, 'tcx> {
139136
self.compute_use_live_points_for(local);
140137
self.compute_drop_live_points_for(local);
141138

142-
let local_ty = self.cx.body.local_decls[local].ty;
139+
let local_ty = self.cx.body().local_decls[local].ty;
143140

144141
if !self.use_live_at.is_empty() {
145142
self.cx.add_use_live_facts_for(local_ty, &self.use_live_at);
@@ -164,16 +161,16 @@ impl<'a, 'typeck, 'b, 'tcx> LivenessResults<'a, 'typeck, 'b, 'tcx> {
164161
/// and can therefore safely be dropped.
165162
fn dropck_boring_locals(&mut self, boring_locals: Vec<Local>) {
166163
for local in boring_locals {
167-
let local_ty = self.cx.body.local_decls[local].ty;
168-
let local_span = self.cx.body.local_decls[local].source_info.span;
164+
let local_ty = self.cx.body().local_decls[local].ty;
165+
let local_span = self.cx.body().local_decls[local].source_info.span;
169166
let drop_data = self.cx.drop_data.entry(local_ty).or_insert_with({
170167
let typeck = &self.cx.typeck;
171168
move || LivenessContext::compute_drop_data(typeck, local_ty, local_span)
172169
});
173170

174171
drop_data.dropck_result.report_overflows(
175172
self.cx.typeck.infcx.tcx,
176-
self.cx.body.local_decls[local].source_info.span,
173+
self.cx.typeck.body.local_decls[local].source_info.span,
177174
local_ty,
178175
);
179176
}
@@ -202,7 +199,7 @@ impl<'a, 'typeck, 'b, 'tcx> LivenessResults<'a, 'typeck, 'b, 'tcx> {
202199
.var_dropped_at
203200
.iter()
204201
.filter_map(|&(local, location_index)| {
205-
let local_ty = self.cx.body.local_decls[local].ty;
202+
let local_ty = self.cx.body().local_decls[local].ty;
206203
if relevant_live_locals.contains(&local) || !local_ty.has_free_regions() {
207204
return None;
208205
}
@@ -278,9 +275,9 @@ impl<'a, 'typeck, 'b, 'tcx> LivenessResults<'a, 'typeck, 'b, 'tcx> {
278275

279276
let block = self.cx.location_map.to_location(block_start).block;
280277
self.stack.extend(
281-
self.cx.body.basic_blocks.predecessors()[block]
278+
self.cx.body().basic_blocks.predecessors()[block]
282279
.iter()
283-
.map(|&pred_bb| self.cx.body.terminator_loc(pred_bb))
280+
.map(|&pred_bb| self.cx.body().terminator_loc(pred_bb))
284281
.map(|pred_loc| self.cx.location_map.point_from_location(pred_loc)),
285282
);
286283
}
@@ -305,7 +302,7 @@ impl<'a, 'typeck, 'b, 'tcx> LivenessResults<'a, 'typeck, 'b, 'tcx> {
305302
// Find the drops where `local` is initialized.
306303
for drop_point in self.cx.local_use_map.drops(local) {
307304
let location = self.cx.location_map.to_location(drop_point);
308-
debug_assert_eq!(self.cx.body.terminator_loc(location.block), location,);
305+
debug_assert_eq!(self.cx.body().terminator_loc(location.block), location,);
309306

310307
if self.cx.initialized_at_terminator(location.block, mpi)
311308
&& self.drop_live_at.insert(drop_point)
@@ -351,7 +348,7 @@ impl<'a, 'typeck, 'b, 'tcx> LivenessResults<'a, 'typeck, 'b, 'tcx> {
351348
// block. One of them may be either a definition or use
352349
// live point.
353350
let term_location = self.cx.location_map.to_location(term_point);
354-
debug_assert_eq!(self.cx.body.terminator_loc(term_location.block), term_location,);
351+
debug_assert_eq!(self.cx.body().terminator_loc(term_location.block), term_location,);
355352
let block = term_location.block;
356353
let entry_point = self.cx.location_map.entry_point(term_location.block);
357354
for p in (entry_point..term_point).rev() {
@@ -376,7 +373,7 @@ impl<'a, 'typeck, 'b, 'tcx> LivenessResults<'a, 'typeck, 'b, 'tcx> {
376373
}
377374
}
378375

379-
let body = self.cx.body;
376+
let body = self.cx.typeck.body;
380377
for &pred_block in body.basic_blocks.predecessors()[block].iter() {
381378
debug!("compute_drop_live_points_for_block: pred_block = {:?}", pred_block,);
382379

@@ -403,7 +400,7 @@ impl<'a, 'typeck, 'b, 'tcx> LivenessResults<'a, 'typeck, 'b, 'tcx> {
403400
continue;
404401
}
405402

406-
let pred_term_loc = self.cx.body.terminator_loc(pred_block);
403+
let pred_term_loc = self.cx.body().terminator_loc(pred_block);
407404
let pred_term_point = self.cx.location_map.point_from_location(pred_term_loc);
408405

409406
// If the terminator of this predecessor either *assigns*
@@ -463,6 +460,9 @@ impl<'a, 'typeck, 'b, 'tcx> LivenessResults<'a, 'typeck, 'b, 'tcx> {
463460
}
464461

465462
impl<'tcx> LivenessContext<'_, '_, '_, 'tcx> {
463+
fn body(&self) -> &Body<'tcx> {
464+
self.typeck.body
465+
}
466466
/// Returns `true` if the local variable (or some part of it) is initialized at the current
467467
/// cursor position. Callers should call one of the `seek` methods immediately before to point
468468
/// the cursor to the desired location.
@@ -481,7 +481,7 @@ impl<'tcx> LivenessContext<'_, '_, '_, 'tcx> {
481481
/// DROP of some local variable will have an effect -- note that
482482
/// drops, as they may unwind, are always terminators.
483483
fn initialized_at_terminator(&mut self, block: BasicBlock, mpi: MovePathIndex) -> bool {
484-
self.flow_inits.seek_before_primary_effect(self.body.terminator_loc(block));
484+
self.flow_inits.seek_before_primary_effect(self.body().terminator_loc(block));
485485
self.initialized_at_curr_loc(mpi)
486486
}
487487

@@ -491,7 +491,7 @@ impl<'tcx> LivenessContext<'_, '_, '_, 'tcx> {
491491
/// **Warning:** Does not account for the result of `Call`
492492
/// instructions.
493493
fn initialized_at_exit(&mut self, block: BasicBlock, mpi: MovePathIndex) -> bool {
494-
self.flow_inits.seek_after_primary_effect(self.body.terminator_loc(block));
494+
self.flow_inits.seek_after_primary_effect(self.body().terminator_loc(block));
495495
self.initialized_at_curr_loc(mpi)
496496
}
497497

@@ -526,7 +526,7 @@ impl<'tcx> LivenessContext<'_, '_, '_, 'tcx> {
526526
values::pretty_print_points(self.location_map, live_at.iter()),
527527
);
528528

529-
let local_span = self.body.local_decls()[dropped_local].source_info.span;
529+
let local_span = self.body().local_decls()[dropped_local].source_info.span;
530530
let drop_data = self.drop_data.entry(dropped_ty).or_insert_with({
531531
let typeck = &self.typeck;
532532
move || Self::compute_drop_data(typeck, dropped_ty, local_span)
@@ -544,7 +544,7 @@ impl<'tcx> LivenessContext<'_, '_, '_, 'tcx> {
544544

545545
drop_data.dropck_result.report_overflows(
546546
self.typeck.infcx.tcx,
547-
self.body.source_info(*drop_locations.first().unwrap()).span,
547+
self.typeck.body.source_info(*drop_locations.first().unwrap()).span,
548548
dropped_ty,
549549
);
550550

0 commit comments

Comments
 (0)