Skip to content

Commit 62bfcfd

Browse files
committed
Auto merge of #77552 - ecstatic-morse:body-def-id, r=lcnr
Replace `(Body, DefId)` with `Body` where possible Follow-up to #77430. I `grep`-ed for parameter lists in which a `Body` appeared within a few lines of a `DefId`, so it's possible that I missed some cases, but this should be pretty complete. Most of these changes were mechanical, but there's a few places where I started calling things "caller" and "callee" when multiple `DefId`s were in-scope at once. Also, we should probably have a helper function on `Body` that returns a `LocalDefId`. I can do that in this PR or in a follow-up.
2 parents efbaa41 + 04a94ab commit 62bfcfd

34 files changed

+201
-273
lines changed

compiler/rustc_mir/src/borrow_check/diagnostics/conflict_errors.rs

+11-10
Original file line numberDiff line numberDiff line change
@@ -336,10 +336,11 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
336336
};
337337
if let ty::Param(param_ty) = ty.kind() {
338338
let tcx = self.infcx.tcx;
339-
let generics = tcx.generics_of(self.mir_def_id);
339+
let generics = tcx.generics_of(self.mir_def_id());
340340
let param = generics.type_param(&param_ty, tcx);
341-
if let Some(generics) =
342-
tcx.hir().get_generics(tcx.closure_base_def_id(self.mir_def_id.to_def_id()))
341+
if let Some(generics) = tcx
342+
.hir()
343+
.get_generics(tcx.closure_base_def_id(self.mir_def_id().to_def_id()))
343344
{
344345
suggest_constraining_type_param(
345346
tcx,
@@ -1004,7 +1005,7 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
10041005
format!("`{}` would have to be valid for `{}`...", name, region_name),
10051006
);
10061007

1007-
let fn_hir_id = self.infcx.tcx.hir().local_def_id_to_hir_id(self.mir_def_id);
1008+
let fn_hir_id = self.mir_hir_id();
10081009
err.span_label(
10091010
drop_span,
10101011
format!(
@@ -1019,7 +1020,7 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
10191020
match &self
10201021
.infcx
10211022
.tcx
1022-
.typeck(self.mir_def_id)
1023+
.typeck(self.mir_def_id())
10231024
.node_type(fn_hir_id)
10241025
.kind()
10251026
{
@@ -1369,7 +1370,7 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
13691370
) -> DiagnosticBuilder<'cx> {
13701371
let tcx = self.infcx.tcx;
13711372

1372-
let (_, escapes_from) = tcx.article_and_description(self.mir_def_id.to_def_id());
1373+
let (_, escapes_from) = tcx.article_and_description(self.mir_def_id().to_def_id());
13731374

13741375
let mut err =
13751376
borrowck_errors::borrowed_data_escapes_closure(tcx, escape_span, escapes_from);
@@ -1708,15 +1709,15 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
17081709
) -> Option<AnnotatedBorrowFnSignature<'tcx>> {
17091710
// Define a fallback for when we can't match a closure.
17101711
let fallback = || {
1711-
let is_closure = self.infcx.tcx.is_closure(self.mir_def_id.to_def_id());
1712+
let is_closure = self.infcx.tcx.is_closure(self.mir_def_id().to_def_id());
17121713
if is_closure {
17131714
None
17141715
} else {
1715-
let ty = self.infcx.tcx.type_of(self.mir_def_id);
1716+
let ty = self.infcx.tcx.type_of(self.mir_def_id());
17161717
match ty.kind() {
17171718
ty::FnDef(_, _) | ty::FnPtr(_) => self.annotate_fn_sig(
1718-
self.mir_def_id.to_def_id(),
1719-
self.infcx.tcx.fn_sig(self.mir_def_id),
1719+
self.mir_def_id().to_def_id(),
1720+
self.infcx.tcx.fn_sig(self.mir_def_id()),
17201721
),
17211722
_ => None,
17221723
}

compiler/rustc_mir/src/borrow_check/diagnostics/move_errors.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -331,7 +331,7 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
331331
self.cannot_move_out_of_interior_noncopy(span, ty, None)
332332
}
333333
ty::Closure(def_id, closure_substs)
334-
if def_id.as_local() == Some(self.mir_def_id) && upvar_field.is_some() =>
334+
if def_id.as_local() == Some(self.mir_def_id()) && upvar_field.is_some() =>
335335
{
336336
let closure_kind_ty = closure_substs.as_closure().kind_ty();
337337
let closure_kind = closure_kind_ty.to_opt_closure_kind();

compiler/rustc_mir/src/borrow_check/diagnostics/mutability_errors.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -492,7 +492,7 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
492492
err.span_label(sp, format!("cannot {}", act));
493493

494494
let hir = self.infcx.tcx.hir();
495-
let closure_id = hir.local_def_id_to_hir_id(self.mir_def_id);
495+
let closure_id = self.mir_hir_id();
496496
let fn_call_id = hir.get_parent_node(closure_id);
497497
let node = hir.get(fn_call_id);
498498
let item_id = hir.enclosing_body_owner(fn_call_id);

compiler/rustc_mir/src/borrow_check/diagnostics/region_errors.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -515,7 +515,8 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
515515
let mut diag =
516516
self.infcx.tcx.sess.struct_span_err(*span, "lifetime may not live long enough");
517517

518-
let (_, mir_def_name) = self.infcx.tcx.article_and_description(self.mir_def_id.to_def_id());
518+
let (_, mir_def_name) =
519+
self.infcx.tcx.article_and_description(self.mir_def_id().to_def_id());
519520

520521
let fr_name = self.give_region_a_name(*fr).unwrap();
521522
fr_name.highlight_region_name(&mut diag);

compiler/rustc_mir/src/borrow_check/diagnostics/region_name.rs

+12-10
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,14 @@ impl Display for RegionName {
147147
}
148148

149149
impl<'tcx> MirBorrowckCtxt<'_, 'tcx> {
150+
crate fn mir_def_id(&self) -> hir::def_id::LocalDefId {
151+
self.body.source.def_id().as_local().unwrap()
152+
}
153+
154+
crate fn mir_hir_id(&self) -> hir::HirId {
155+
self.infcx.tcx.hir().local_def_id_to_hir_id(self.mir_def_id())
156+
}
157+
150158
/// Generate a synthetic region named `'N`, where `N` is the next value of the counter. Then,
151159
/// increment the counter.
152160
///
@@ -266,12 +274,11 @@ impl<'tcx> MirBorrowckCtxt<'_, 'tcx> {
266274
}
267275

268276
ty::BoundRegion::BrEnv => {
269-
let mir_hir_id = self.infcx.tcx.hir().local_def_id_to_hir_id(self.mir_def_id);
270277
let def_ty = self.regioncx.universal_regions().defining_ty;
271278

272279
if let DefiningTy::Closure(_, substs) = def_ty {
273280
let args_span = if let hir::ExprKind::Closure(_, _, _, span, _) =
274-
tcx.hir().expect_expr(mir_hir_id).kind
281+
tcx.hir().expect_expr(self.mir_hir_id()).kind
275282
{
276283
span
277284
} else {
@@ -361,8 +368,7 @@ impl<'tcx> MirBorrowckCtxt<'_, 'tcx> {
361368
&self,
362369
argument_index: usize,
363370
) -> Option<&hir::Ty<'tcx>> {
364-
let mir_hir_id = self.infcx.tcx.hir().local_def_id_to_hir_id(self.mir_def_id);
365-
let fn_decl = self.infcx.tcx.hir().fn_decl_by_hir_id(mir_hir_id)?;
371+
let fn_decl = self.infcx.tcx.hir().fn_decl_by_hir_id(self.mir_hir_id())?;
366372
let argument_hir_ty: &hir::Ty<'_> = fn_decl.inputs.get(argument_index)?;
367373
match argument_hir_ty.kind {
368374
// This indicates a variable with no type annotation, like
@@ -649,9 +655,7 @@ impl<'tcx> MirBorrowckCtxt<'_, 'tcx> {
649655
let type_name =
650656
self.infcx.extract_inference_diagnostics_data(return_ty.into(), Some(highlight)).name;
651657

652-
let mir_hir_id = tcx.hir().local_def_id_to_hir_id(self.mir_def_id);
653-
654-
let (return_span, mir_description) = match tcx.hir().get(mir_hir_id) {
658+
let (return_span, mir_description) = match tcx.hir().get(self.mir_hir_id()) {
655659
hir::Node::Expr(hir::Expr {
656660
kind: hir::ExprKind::Closure(_, return_ty, _, span, gen_move),
657661
..
@@ -702,9 +706,7 @@ impl<'tcx> MirBorrowckCtxt<'_, 'tcx> {
702706
let type_name =
703707
self.infcx.extract_inference_diagnostics_data(yield_ty.into(), Some(highlight)).name;
704708

705-
let mir_hir_id = tcx.hir().local_def_id_to_hir_id(self.mir_def_id);
706-
707-
let yield_span = match tcx.hir().get(mir_hir_id) {
709+
let yield_span = match tcx.hir().get(self.mir_hir_id()) {
708710
hir::Node::Expr(hir::Expr {
709711
kind: hir::ExprKind::Closure(_, _, _, span, _), ..
710712
}) => (tcx.sess.source_map().end_point(*span)),

compiler/rustc_mir/src/borrow_check/mod.rs

+8-12
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ fn mir_borrowck<'tcx>(
111111
let opt_closure_req = tcx.infer_ctxt().enter(|infcx| {
112112
let input_body: &Body<'_> = &input_body.borrow();
113113
let promoted: &IndexVec<_, _> = &promoted.borrow();
114-
do_mir_borrowck(&infcx, input_body, promoted, def)
114+
do_mir_borrowck(&infcx, input_body, promoted)
115115
});
116116
debug!("mir_borrowck done");
117117

@@ -122,8 +122,9 @@ fn do_mir_borrowck<'a, 'tcx>(
122122
infcx: &InferCtxt<'a, 'tcx>,
123123
input_body: &Body<'tcx>,
124124
input_promoted: &IndexVec<Promoted, Body<'tcx>>,
125-
def: ty::WithOptConstParam<LocalDefId>,
126125
) -> BorrowCheckResult<'tcx> {
126+
let def = input_body.source.with_opt_param().as_local().unwrap();
127+
127128
debug!("do_mir_borrowck(def = {:?})", def);
128129

129130
let tcx = infcx.tcx;
@@ -185,7 +186,7 @@ fn do_mir_borrowck<'a, 'tcx>(
185186
// will have a lifetime tied to the inference context.
186187
let mut body = input_body.clone();
187188
let mut promoted = input_promoted.clone();
188-
let free_regions = nll::replace_regions_in_mir(infcx, def, param_env, &mut body, &mut promoted);
189+
let free_regions = nll::replace_regions_in_mir(infcx, param_env, &mut body, &mut promoted);
189190
let body = &body; // no further changes
190191

191192
let location_table = &LocationTable::new(&body);
@@ -203,7 +204,7 @@ fn do_mir_borrowck<'a, 'tcx>(
203204
let mdpe = MoveDataParamEnv { move_data, param_env };
204205

205206
let mut flow_inits = MaybeInitializedPlaces::new(tcx, &body, &mdpe)
206-
.into_engine(tcx, &body, def.did.to_def_id())
207+
.into_engine(tcx, &body)
207208
.pass_name("borrowck")
208209
.iterate_to_fixpoint()
209210
.into_results_cursor(&body);
@@ -221,7 +222,6 @@ fn do_mir_borrowck<'a, 'tcx>(
221222
nll_errors,
222223
} = nll::compute_regions(
223224
infcx,
224-
def.did,
225225
free_regions,
226226
body,
227227
&promoted,
@@ -242,7 +242,6 @@ fn do_mir_borrowck<'a, 'tcx>(
242242
nll::dump_annotation(
243243
infcx,
244244
&body,
245-
def.did.to_def_id(),
246245
&regioncx,
247246
&opt_closure_req,
248247
&opaque_type_values,
@@ -257,15 +256,15 @@ fn do_mir_borrowck<'a, 'tcx>(
257256
let regioncx = Rc::new(regioncx);
258257

259258
let flow_borrows = Borrows::new(tcx, &body, regioncx.clone(), &borrow_set)
260-
.into_engine(tcx, &body, def.did.to_def_id())
259+
.into_engine(tcx, &body)
261260
.pass_name("borrowck")
262261
.iterate_to_fixpoint();
263262
let flow_uninits = MaybeUninitializedPlaces::new(tcx, &body, &mdpe)
264-
.into_engine(tcx, &body, def.did.to_def_id())
263+
.into_engine(tcx, &body)
265264
.pass_name("borrowck")
266265
.iterate_to_fixpoint();
267266
let flow_ever_inits = EverInitializedPlaces::new(tcx, &body, &mdpe)
268-
.into_engine(tcx, &body, def.did.to_def_id())
267+
.into_engine(tcx, &body)
269268
.pass_name("borrowck")
270269
.iterate_to_fixpoint();
271270

@@ -286,7 +285,6 @@ fn do_mir_borrowck<'a, 'tcx>(
286285
infcx,
287286
param_env,
288287
body: promoted_body,
289-
mir_def_id: def.did,
290288
move_data: &move_data,
291289
location_table: &LocationTable::new(promoted_body),
292290
movable_generator,
@@ -320,7 +318,6 @@ fn do_mir_borrowck<'a, 'tcx>(
320318
infcx,
321319
param_env,
322320
body,
323-
mir_def_id: def.did,
324321
move_data: &mdpe.move_data,
325322
location_table,
326323
movable_generator,
@@ -474,7 +471,6 @@ crate struct MirBorrowckCtxt<'cx, 'tcx> {
474471
crate infcx: &'cx InferCtxt<'cx, 'tcx>,
475472
param_env: ParamEnv<'tcx>,
476473
body: &'cx Body<'tcx>,
477-
mir_def_id: LocalDefId,
478474
move_data: &'cx MoveData<'tcx>,
479475

480476
/// Map from MIR `Location` to `LocationIndex`; created

compiler/rustc_mir/src/borrow_check/nll.rs

+8-8
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
33
use rustc_data_structures::fx::FxHashMap;
44
use rustc_errors::Diagnostic;
5-
use rustc_hir::def_id::{DefId, LocalDefId};
5+
use rustc_hir::def_id::DefId;
66
use rustc_index::vec::IndexVec;
77
use rustc_infer::infer::InferCtxt;
88
use rustc_middle::mir::{
@@ -58,11 +58,12 @@ crate struct NllOutput<'tcx> {
5858
/// `compute_regions`.
5959
pub(in crate::borrow_check) fn replace_regions_in_mir<'cx, 'tcx>(
6060
infcx: &InferCtxt<'cx, 'tcx>,
61-
def: ty::WithOptConstParam<LocalDefId>,
6261
param_env: ty::ParamEnv<'tcx>,
6362
body: &mut Body<'tcx>,
6463
promoted: &mut IndexVec<Promoted, Body<'tcx>>,
6564
) -> UniversalRegions<'tcx> {
65+
let def = body.source.with_opt_param().as_local().unwrap();
66+
6667
debug!("replace_regions_in_mir(def={:?})", def);
6768

6869
// Compute named region information. This also renumbers the inputs/outputs.
@@ -156,7 +157,6 @@ fn populate_polonius_move_facts(
156157
/// This may result in errors being reported.
157158
pub(in crate::borrow_check) fn compute_regions<'cx, 'tcx>(
158159
infcx: &InferCtxt<'cx, 'tcx>,
159-
def_id: LocalDefId,
160160
universal_regions: UniversalRegions<'tcx>,
161161
body: &Body<'tcx>,
162162
promoted: &IndexVec<Promoted, Body<'tcx>>,
@@ -180,7 +180,6 @@ pub(in crate::borrow_check) fn compute_regions<'cx, 'tcx>(
180180
param_env,
181181
body,
182182
promoted,
183-
def_id,
184183
&universal_regions,
185184
location_table,
186185
borrow_set,
@@ -270,10 +269,12 @@ pub(in crate::borrow_check) fn compute_regions<'cx, 'tcx>(
270269
// Generate various additional constraints.
271270
invalidation::generate_invalidates(infcx.tcx, &mut all_facts, location_table, body, borrow_set);
272271

272+
let def_id = body.source.def_id();
273+
273274
// Dump facts if requested.
274275
let polonius_output = all_facts.and_then(|all_facts| {
275276
if infcx.tcx.sess.opts.debugging_opts.nll_facts {
276-
let def_path = infcx.tcx.def_path(def_id.to_def_id());
277+
let def_path = infcx.tcx.def_path(def_id);
277278
let dir_path =
278279
PathBuf::from("nll-facts").join(def_path.to_filename_friendly_no_crate());
279280
all_facts.write_to_dir(dir_path, location_table).unwrap();
@@ -293,7 +294,7 @@ pub(in crate::borrow_check) fn compute_regions<'cx, 'tcx>(
293294

294295
// Solve the region constraints.
295296
let (closure_region_requirements, nll_errors) =
296-
regioncx.solve(infcx, &body, def_id.to_def_id(), polonius_output.clone());
297+
regioncx.solve(infcx, &body, polonius_output.clone());
297298

298299
if !nll_errors.is_empty() {
299300
// Suppress unhelpful extra errors in `infer_opaque_types`.
@@ -364,14 +365,13 @@ pub(super) fn dump_mir_results<'a, 'tcx>(
364365
pub(super) fn dump_annotation<'a, 'tcx>(
365366
infcx: &InferCtxt<'a, 'tcx>,
366367
body: &Body<'tcx>,
367-
mir_def_id: DefId,
368368
regioncx: &RegionInferenceContext<'tcx>,
369369
closure_region_requirements: &Option<ClosureRegionRequirements<'_>>,
370370
opaque_type_values: &FxHashMap<DefId, ty::ResolvedOpaqueTy<'tcx>>,
371371
errors_buffer: &mut Vec<Diagnostic>,
372372
) {
373373
let tcx = infcx.tcx;
374-
let base_def_id = tcx.closure_base_def_id(mir_def_id);
374+
let base_def_id = tcx.closure_base_def_id(body.source.def_id());
375375
if !tcx.has_attr(base_def_id, sym::rustc_regions) {
376376
return;
377377
}

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -548,9 +548,9 @@ impl<'tcx> RegionInferenceContext<'tcx> {
548548
&mut self,
549549
infcx: &InferCtxt<'_, 'tcx>,
550550
body: &Body<'tcx>,
551-
mir_def_id: DefId,
552551
polonius_output: Option<Rc<PoloniusOutput>>,
553552
) -> (Option<ClosureRegionRequirements<'tcx>>, RegionErrors<'tcx>) {
553+
let mir_def_id = body.source.def_id();
554554
self.propagate_constraints(body, infcx.tcx);
555555

556556
let mut errors_buffer = RegionErrors::new();

0 commit comments

Comments
 (0)