Skip to content

Commit e72e43c

Browse files
Replace (Body, DefId) with Body where possible
A `Body` now contains its `MirSource`, which in turn contains the `DefId` of the item associated with the `Body`.
1 parent 4ccf5f7 commit e72e43c

25 files changed

+159
-232
lines changed

compiler/rustc_mir/src/borrow_check/mod.rs

+4-6
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ fn do_mir_borrowck<'a, 'tcx>(
203203
let mdpe = MoveDataParamEnv { move_data, param_env };
204204

205205
let mut flow_inits = MaybeInitializedPlaces::new(tcx, &body, &mdpe)
206-
.into_engine(tcx, &body, def.did.to_def_id())
206+
.into_engine(tcx, &body)
207207
.pass_name("borrowck")
208208
.iterate_to_fixpoint()
209209
.into_results_cursor(&body);
@@ -221,7 +221,6 @@ fn do_mir_borrowck<'a, 'tcx>(
221221
nll_errors,
222222
} = nll::compute_regions(
223223
infcx,
224-
def.did,
225224
free_regions,
226225
body,
227226
&promoted,
@@ -242,7 +241,6 @@ fn do_mir_borrowck<'a, 'tcx>(
242241
nll::dump_annotation(
243242
infcx,
244243
&body,
245-
def.did.to_def_id(),
246244
&regioncx,
247245
&opt_closure_req,
248246
&opaque_type_values,
@@ -257,15 +255,15 @@ fn do_mir_borrowck<'a, 'tcx>(
257255
let regioncx = Rc::new(regioncx);
258256

259257
let flow_borrows = Borrows::new(tcx, &body, regioncx.clone(), &borrow_set)
260-
.into_engine(tcx, &body, def.did.to_def_id())
258+
.into_engine(tcx, &body)
261259
.pass_name("borrowck")
262260
.iterate_to_fixpoint();
263261
let flow_uninits = MaybeUninitializedPlaces::new(tcx, &body, &mdpe)
264-
.into_engine(tcx, &body, def.did.to_def_id())
262+
.into_engine(tcx, &body)
265263
.pass_name("borrowck")
266264
.iterate_to_fixpoint();
267265
let flow_ever_inits = EverInitializedPlaces::new(tcx, &body, &mdpe)
268-
.into_engine(tcx, &body, def.did.to_def_id())
266+
.into_engine(tcx, &body)
269267
.pass_name("borrowck")
270268
.iterate_to_fixpoint();
271269

compiler/rustc_mir/src/borrow_check/nll.rs

+5-6
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,6 @@ fn populate_polonius_move_facts(
156156
/// This may result in errors being reported.
157157
pub(in crate::borrow_check) fn compute_regions<'cx, 'tcx>(
158158
infcx: &InferCtxt<'cx, 'tcx>,
159-
def_id: LocalDefId,
160159
universal_regions: UniversalRegions<'tcx>,
161160
body: &Body<'tcx>,
162161
promoted: &IndexVec<Promoted, Body<'tcx>>,
@@ -180,7 +179,6 @@ pub(in crate::borrow_check) fn compute_regions<'cx, 'tcx>(
180179
param_env,
181180
body,
182181
promoted,
183-
def_id,
184182
&universal_regions,
185183
location_table,
186184
borrow_set,
@@ -270,10 +268,12 @@ pub(in crate::borrow_check) fn compute_regions<'cx, 'tcx>(
270268
// Generate various additional constraints.
271269
invalidation::generate_invalidates(infcx.tcx, &mut all_facts, location_table, body, borrow_set);
272270

271+
let def_id = body.source.def_id();
272+
273273
// Dump facts if requested.
274274
let polonius_output = all_facts.and_then(|all_facts| {
275275
if infcx.tcx.sess.opts.debugging_opts.nll_facts {
276-
let def_path = infcx.tcx.def_path(def_id.to_def_id());
276+
let def_path = infcx.tcx.def_path(def_id);
277277
let dir_path =
278278
PathBuf::from("nll-facts").join(def_path.to_filename_friendly_no_crate());
279279
all_facts.write_to_dir(dir_path, location_table).unwrap();
@@ -293,7 +293,7 @@ pub(in crate::borrow_check) fn compute_regions<'cx, 'tcx>(
293293

294294
// Solve the region constraints.
295295
let (closure_region_requirements, nll_errors) =
296-
regioncx.solve(infcx, &body, def_id.to_def_id(), polonius_output.clone());
296+
regioncx.solve(infcx, &body, def_id, polonius_output.clone());
297297

298298
if !nll_errors.is_empty() {
299299
// Suppress unhelpful extra errors in `infer_opaque_types`.
@@ -364,14 +364,13 @@ pub(super) fn dump_mir_results<'a, 'tcx>(
364364
pub(super) fn dump_annotation<'a, 'tcx>(
365365
infcx: &InferCtxt<'a, 'tcx>,
366366
body: &Body<'tcx>,
367-
mir_def_id: DefId,
368367
regioncx: &RegionInferenceContext<'tcx>,
369368
closure_region_requirements: &Option<ClosureRegionRequirements<'_>>,
370369
opaque_type_values: &FxHashMap<DefId, ty::ResolvedOpaqueTy<'tcx>>,
371370
errors_buffer: &mut Vec<Diagnostic>,
372371
) {
373372
let tcx = infcx.tcx;
374-
let base_def_id = tcx.closure_base_def_id(mir_def_id);
373+
let base_def_id = tcx.closure_base_def_id(body.source.def_id());
375374
if !tcx.has_attr(base_def_id, sym::rustc_regions) {
376375
return;
377376
}

compiler/rustc_mir/src/borrow_check/type_check/input_output.rs

+30-29
Original file line numberDiff line numberDiff line change
@@ -28,42 +28,43 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
2828
let (&normalized_output_ty, normalized_input_tys) =
2929
normalized_inputs_and_output.split_last().unwrap();
3030

31+
let mir_def_id = body.source.def_id().expect_local();
32+
3133
// If the user explicitly annotated the input types, extract
3234
// those.
3335
//
3436
// e.g., `|x: FxHashMap<_, &'static u32>| ...`
3537
let user_provided_sig;
36-
if !self.tcx().is_closure(self.mir_def_id.to_def_id()) {
38+
if !self.tcx().is_closure(mir_def_id.to_def_id()) {
3739
user_provided_sig = None;
3840
} else {
39-
let typeck_results = self.tcx().typeck(self.mir_def_id);
40-
user_provided_sig =
41-
match typeck_results.user_provided_sigs.get(&self.mir_def_id.to_def_id()) {
42-
None => None,
43-
Some(user_provided_poly_sig) => {
44-
// Instantiate the canonicalized variables from
45-
// user-provided signature (e.g., the `_` in the code
46-
// above) with fresh variables.
47-
let (poly_sig, _) =
48-
self.infcx.instantiate_canonical_with_fresh_inference_vars(
41+
let typeck_results = self.tcx().typeck(mir_def_id);
42+
user_provided_sig = match typeck_results.user_provided_sigs.get(&mir_def_id.to_def_id())
43+
{
44+
None => None,
45+
Some(user_provided_poly_sig) => {
46+
// Instantiate the canonicalized variables from
47+
// user-provided signature (e.g., the `_` in the code
48+
// above) with fresh variables.
49+
let (poly_sig, _) = self.infcx.instantiate_canonical_with_fresh_inference_vars(
50+
body.span,
51+
&user_provided_poly_sig,
52+
);
53+
54+
// Replace the bound items in the fn sig with fresh
55+
// variables, so that they represent the view from
56+
// "inside" the closure.
57+
Some(
58+
self.infcx
59+
.replace_bound_vars_with_fresh_vars(
4960
body.span,
50-
&user_provided_poly_sig,
51-
);
52-
53-
// Replace the bound items in the fn sig with fresh
54-
// variables, so that they represent the view from
55-
// "inside" the closure.
56-
Some(
57-
self.infcx
58-
.replace_bound_vars_with_fresh_vars(
59-
body.span,
60-
LateBoundRegionConversionTime::FnCall,
61-
&poly_sig,
62-
)
63-
.0,
64-
)
65-
}
61+
LateBoundRegionConversionTime::FnCall,
62+
&poly_sig,
63+
)
64+
.0,
65+
)
6666
}
67+
}
6768
};
6869

6970
debug!(
@@ -122,7 +123,7 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
122123
if let Err(terr) = self.eq_opaque_type_and_type(
123124
mir_output_ty,
124125
normalized_output_ty,
125-
self.mir_def_id,
126+
mir_def_id,
126127
Locations::All(output_span),
127128
ConstraintCategory::BoringNoLocation,
128129
) {
@@ -145,7 +146,7 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
145146
if let Err(err) = self.eq_opaque_type_and_type(
146147
mir_output_ty,
147148
user_provided_output_ty,
148-
self.mir_def_id,
149+
mir_def_id,
149150
Locations::All(output_span),
150151
ConstraintCategory::BoringNoLocation,
151152
) {

compiler/rustc_mir/src/borrow_check/type_check/mod.rs

+7-27
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ macro_rules! span_mirbug {
7373
$context.last_span,
7474
&format!(
7575
"broken MIR in {:?} ({:?}): {}",
76-
$context.mir_def_id,
76+
$context.body.source.def_id(),
7777
$elem,
7878
format_args!($($message)*),
7979
),
@@ -113,7 +113,6 @@ mod relate_tys;
113113
/// - `param_env` -- parameter environment to use for trait solving
114114
/// - `body` -- MIR body to type-check
115115
/// - `promoted` -- map of promoted constants within `body`
116-
/// - `mir_def_id` -- `LocalDefId` from which the MIR is derived
117116
/// - `universal_regions` -- the universal regions from `body`s function signature
118117
/// - `location_table` -- MIR location map of `body`
119118
/// - `borrow_set` -- information about borrows occurring in `body`
@@ -126,7 +125,6 @@ pub(crate) fn type_check<'mir, 'tcx>(
126125
param_env: ty::ParamEnv<'tcx>,
127126
body: &Body<'tcx>,
128127
promoted: &IndexVec<Promoted, Body<'tcx>>,
129-
mir_def_id: LocalDefId,
130128
universal_regions: &Rc<UniversalRegions<'tcx>>,
131129
location_table: &LocationTable,
132130
borrow_set: &BorrowSet<'tcx>,
@@ -170,7 +168,6 @@ pub(crate) fn type_check<'mir, 'tcx>(
170168

171169
let opaque_type_values = type_check_internal(
172170
infcx,
173-
mir_def_id,
174171
param_env,
175172
body,
176173
promoted,
@@ -192,7 +189,6 @@ pub(crate) fn type_check<'mir, 'tcx>(
192189

193190
fn type_check_internal<'a, 'tcx, R>(
194191
infcx: &'a InferCtxt<'a, 'tcx>,
195-
mir_def_id: LocalDefId,
196192
param_env: ty::ParamEnv<'tcx>,
197193
body: &'a Body<'tcx>,
198194
promoted: &'a IndexVec<Promoted, Body<'tcx>>,
@@ -205,7 +201,6 @@ fn type_check_internal<'a, 'tcx, R>(
205201
let mut checker = TypeChecker::new(
206202
infcx,
207203
body,
208-
mir_def_id,
209204
param_env,
210205
region_bound_pairs,
211206
implicit_region_bound,
@@ -272,7 +267,6 @@ struct TypeVerifier<'a, 'b, 'tcx> {
272267
body: &'b Body<'tcx>,
273268
promoted: &'b IndexVec<Promoted, Body<'tcx>>,
274269
last_span: Span,
275-
mir_def_id: LocalDefId,
276270
errors_reported: bool,
277271
}
278272

@@ -460,14 +454,7 @@ impl<'a, 'b, 'tcx> TypeVerifier<'a, 'b, 'tcx> {
460454
body: &'b Body<'tcx>,
461455
promoted: &'b IndexVec<Promoted, Body<'tcx>>,
462456
) -> Self {
463-
TypeVerifier {
464-
body,
465-
promoted,
466-
mir_def_id: cx.mir_def_id,
467-
cx,
468-
last_span: body.span,
469-
errors_reported: false,
470-
}
457+
TypeVerifier { body, promoted, cx, last_span: body.span, errors_reported: false }
471458
}
472459

473460
fn tcx(&self) -> TyCtxt<'tcx> {
@@ -816,7 +803,6 @@ struct TypeChecker<'a, 'tcx> {
816803
/// User type annotations are shared between the main MIR and the MIR of
817804
/// all of the promoted items.
818805
user_type_annotations: &'a CanonicalUserTypeAnnotations<'tcx>,
819-
mir_def_id: LocalDefId,
820806
region_bound_pairs: &'a RegionBoundPairs<'tcx>,
821807
implicit_region_bound: ty::Region<'tcx>,
822808
reported_errors: FxHashSet<(Ty<'tcx>, Span)>,
@@ -965,7 +951,6 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
965951
fn new(
966952
infcx: &'a InferCtxt<'a, 'tcx>,
967953
body: &'a Body<'tcx>,
968-
mir_def_id: LocalDefId,
969954
param_env: ty::ParamEnv<'tcx>,
970955
region_bound_pairs: &'a RegionBoundPairs<'tcx>,
971956
implicit_region_bound: ty::Region<'tcx>,
@@ -975,7 +960,6 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
975960
let mut checker = Self {
976961
infcx,
977962
last_span: DUMMY_SP,
978-
mir_def_id,
979963
body,
980964
user_type_annotations: &body.user_type_annotations,
981965
param_env,
@@ -1145,7 +1129,7 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
11451129
// the resulting inferend values are stored with the
11461130
// def-id of the base function.
11471131
let parent_def_id =
1148-
self.tcx().closure_base_def_id(self.mir_def_id.to_def_id()).expect_local();
1132+
self.tcx().closure_base_def_id(self.body.source.def_id()).expect_local();
11491133
return self.eq_opaque_type_and_type(sub, sup, parent_def_id, locations, category);
11501134
} else {
11511135
return Err(terr);
@@ -1242,7 +1226,7 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
12421226
let concrete_opaque_types = &tcx.typeck(anon_owner_def_id).concrete_opaque_types;
12431227
let mut opaque_type_values = Vec::new();
12441228

1245-
debug!("eq_opaque_type_and_type: mir_def_id={:?}", self.mir_def_id);
1229+
debug!("eq_opaque_type_and_type: mir_def_id={:?}", body.source.def_id());
12461230
let opaque_type_map = self.fully_perform_op(
12471231
locations,
12481232
category,
@@ -2001,12 +1985,7 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
20011985
let span = body.source_info(location).span;
20021986
let ty = operand.ty(body, tcx);
20031987
if !self.infcx.type_is_copy_modulo_regions(self.param_env, ty, span) {
2004-
let ccx = ConstCx::new_with_param_env(
2005-
tcx,
2006-
self.mir_def_id,
2007-
body,
2008-
self.param_env,
2009-
);
1988+
let ccx = ConstCx::new_with_param_env(tcx, body, self.param_env);
20101989
// To determine if `const_in_array_repeat_expressions` feature gate should
20111990
// be mentioned, need to check if the rvalue is promotable.
20121991
let should_suggest =
@@ -2015,11 +1994,12 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
20151994
);
20161995
debug!("check_rvalue: should_suggest={:?}", should_suggest);
20171996

1997+
let def_id = body.source.def_id().expect_local();
20181998
self.infcx.report_selection_error(
20191999
&traits::Obligation::new(
20202000
ObligationCause::new(
20212001
span,
2022-
self.tcx().hir().local_def_id_to_hir_id(self.mir_def_id),
2002+
self.tcx().hir().local_def_id_to_hir_id(def_id),
20232003
traits::ObligationCauseCode::RepeatVec(should_suggest),
20242004
),
20252005
self.param_env,

0 commit comments

Comments
 (0)