Skip to content

Commit

Permalink
Auto merge of #77552 - ecstatic-morse:body-def-id, r=lcnr
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
bors committed Oct 5, 2020
2 parents efbaa41 + 04a94ab commit 62bfcfd
Show file tree
Hide file tree
Showing 34 changed files with 201 additions and 273 deletions.
21 changes: 11 additions & 10 deletions compiler/rustc_mir/src/borrow_check/diagnostics/conflict_errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -336,10 +336,11 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
};
if let ty::Param(param_ty) = ty.kind() {
let tcx = self.infcx.tcx;
let generics = tcx.generics_of(self.mir_def_id);
let generics = tcx.generics_of(self.mir_def_id());
let param = generics.type_param(&param_ty, tcx);
if let Some(generics) =
tcx.hir().get_generics(tcx.closure_base_def_id(self.mir_def_id.to_def_id()))
if let Some(generics) = tcx
.hir()
.get_generics(tcx.closure_base_def_id(self.mir_def_id().to_def_id()))
{
suggest_constraining_type_param(
tcx,
Expand Down Expand Up @@ -1004,7 +1005,7 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
format!("`{}` would have to be valid for `{}`...", name, region_name),
);

let fn_hir_id = self.infcx.tcx.hir().local_def_id_to_hir_id(self.mir_def_id);
let fn_hir_id = self.mir_hir_id();
err.span_label(
drop_span,
format!(
Expand All @@ -1019,7 +1020,7 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
match &self
.infcx
.tcx
.typeck(self.mir_def_id)
.typeck(self.mir_def_id())
.node_type(fn_hir_id)
.kind()
{
Expand Down Expand Up @@ -1369,7 +1370,7 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
) -> DiagnosticBuilder<'cx> {
let tcx = self.infcx.tcx;

let (_, escapes_from) = tcx.article_and_description(self.mir_def_id.to_def_id());
let (_, escapes_from) = tcx.article_and_description(self.mir_def_id().to_def_id());

let mut err =
borrowck_errors::borrowed_data_escapes_closure(tcx, escape_span, escapes_from);
Expand Down Expand Up @@ -1708,15 +1709,15 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
) -> Option<AnnotatedBorrowFnSignature<'tcx>> {
// Define a fallback for when we can't match a closure.
let fallback = || {
let is_closure = self.infcx.tcx.is_closure(self.mir_def_id.to_def_id());
let is_closure = self.infcx.tcx.is_closure(self.mir_def_id().to_def_id());
if is_closure {
None
} else {
let ty = self.infcx.tcx.type_of(self.mir_def_id);
let ty = self.infcx.tcx.type_of(self.mir_def_id());
match ty.kind() {
ty::FnDef(_, _) | ty::FnPtr(_) => self.annotate_fn_sig(
self.mir_def_id.to_def_id(),
self.infcx.tcx.fn_sig(self.mir_def_id),
self.mir_def_id().to_def_id(),
self.infcx.tcx.fn_sig(self.mir_def_id()),
),
_ => None,
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,7 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
self.cannot_move_out_of_interior_noncopy(span, ty, None)
}
ty::Closure(def_id, closure_substs)
if def_id.as_local() == Some(self.mir_def_id) && upvar_field.is_some() =>
if def_id.as_local() == Some(self.mir_def_id()) && upvar_field.is_some() =>
{
let closure_kind_ty = closure_substs.as_closure().kind_ty();
let closure_kind = closure_kind_ty.to_opt_closure_kind();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -492,7 +492,7 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
err.span_label(sp, format!("cannot {}", act));

let hir = self.infcx.tcx.hir();
let closure_id = hir.local_def_id_to_hir_id(self.mir_def_id);
let closure_id = self.mir_hir_id();
let fn_call_id = hir.get_parent_node(closure_id);
let node = hir.get(fn_call_id);
let item_id = hir.enclosing_body_owner(fn_call_id);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -515,7 +515,8 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
let mut diag =
self.infcx.tcx.sess.struct_span_err(*span, "lifetime may not live long enough");

let (_, mir_def_name) = self.infcx.tcx.article_and_description(self.mir_def_id.to_def_id());
let (_, mir_def_name) =
self.infcx.tcx.article_and_description(self.mir_def_id().to_def_id());

let fr_name = self.give_region_a_name(*fr).unwrap();
fr_name.highlight_region_name(&mut diag);
Expand Down
22 changes: 12 additions & 10 deletions compiler/rustc_mir/src/borrow_check/diagnostics/region_name.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,14 @@ impl Display for RegionName {
}

impl<'tcx> MirBorrowckCtxt<'_, 'tcx> {
crate fn mir_def_id(&self) -> hir::def_id::LocalDefId {
self.body.source.def_id().as_local().unwrap()
}

crate fn mir_hir_id(&self) -> hir::HirId {
self.infcx.tcx.hir().local_def_id_to_hir_id(self.mir_def_id())
}

/// Generate a synthetic region named `'N`, where `N` is the next value of the counter. Then,
/// increment the counter.
///
Expand Down Expand Up @@ -266,12 +274,11 @@ impl<'tcx> MirBorrowckCtxt<'_, 'tcx> {
}

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

if let DefiningTy::Closure(_, substs) = def_ty {
let args_span = if let hir::ExprKind::Closure(_, _, _, span, _) =
tcx.hir().expect_expr(mir_hir_id).kind
tcx.hir().expect_expr(self.mir_hir_id()).kind
{
span
} else {
Expand Down Expand Up @@ -361,8 +368,7 @@ impl<'tcx> MirBorrowckCtxt<'_, 'tcx> {
&self,
argument_index: usize,
) -> Option<&hir::Ty<'tcx>> {
let mir_hir_id = self.infcx.tcx.hir().local_def_id_to_hir_id(self.mir_def_id);
let fn_decl = self.infcx.tcx.hir().fn_decl_by_hir_id(mir_hir_id)?;
let fn_decl = self.infcx.tcx.hir().fn_decl_by_hir_id(self.mir_hir_id())?;
let argument_hir_ty: &hir::Ty<'_> = fn_decl.inputs.get(argument_index)?;
match argument_hir_ty.kind {
// This indicates a variable with no type annotation, like
Expand Down Expand Up @@ -649,9 +655,7 @@ impl<'tcx> MirBorrowckCtxt<'_, 'tcx> {
let type_name =
self.infcx.extract_inference_diagnostics_data(return_ty.into(), Some(highlight)).name;

let mir_hir_id = tcx.hir().local_def_id_to_hir_id(self.mir_def_id);

let (return_span, mir_description) = match tcx.hir().get(mir_hir_id) {
let (return_span, mir_description) = match tcx.hir().get(self.mir_hir_id()) {
hir::Node::Expr(hir::Expr {
kind: hir::ExprKind::Closure(_, return_ty, _, span, gen_move),
..
Expand Down Expand Up @@ -702,9 +706,7 @@ impl<'tcx> MirBorrowckCtxt<'_, 'tcx> {
let type_name =
self.infcx.extract_inference_diagnostics_data(yield_ty.into(), Some(highlight)).name;

let mir_hir_id = tcx.hir().local_def_id_to_hir_id(self.mir_def_id);

let yield_span = match tcx.hir().get(mir_hir_id) {
let yield_span = match tcx.hir().get(self.mir_hir_id()) {
hir::Node::Expr(hir::Expr {
kind: hir::ExprKind::Closure(_, _, _, span, _), ..
}) => (tcx.sess.source_map().end_point(*span)),
Expand Down
20 changes: 8 additions & 12 deletions compiler/rustc_mir/src/borrow_check/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ fn mir_borrowck<'tcx>(
let opt_closure_req = tcx.infer_ctxt().enter(|infcx| {
let input_body: &Body<'_> = &input_body.borrow();
let promoted: &IndexVec<_, _> = &promoted.borrow();
do_mir_borrowck(&infcx, input_body, promoted, def)
do_mir_borrowck(&infcx, input_body, promoted)
});
debug!("mir_borrowck done");

Expand All @@ -122,8 +122,9 @@ fn do_mir_borrowck<'a, 'tcx>(
infcx: &InferCtxt<'a, 'tcx>,
input_body: &Body<'tcx>,
input_promoted: &IndexVec<Promoted, Body<'tcx>>,
def: ty::WithOptConstParam<LocalDefId>,
) -> BorrowCheckResult<'tcx> {
let def = input_body.source.with_opt_param().as_local().unwrap();

debug!("do_mir_borrowck(def = {:?})", def);

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

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

let mut flow_inits = MaybeInitializedPlaces::new(tcx, &body, &mdpe)
.into_engine(tcx, &body, def.did.to_def_id())
.into_engine(tcx, &body)
.pass_name("borrowck")
.iterate_to_fixpoint()
.into_results_cursor(&body);
Expand All @@ -221,7 +222,6 @@ fn do_mir_borrowck<'a, 'tcx>(
nll_errors,
} = nll::compute_regions(
infcx,
def.did,
free_regions,
body,
&promoted,
Expand All @@ -242,7 +242,6 @@ fn do_mir_borrowck<'a, 'tcx>(
nll::dump_annotation(
infcx,
&body,
def.did.to_def_id(),
&regioncx,
&opt_closure_req,
&opaque_type_values,
Expand All @@ -257,15 +256,15 @@ fn do_mir_borrowck<'a, 'tcx>(
let regioncx = Rc::new(regioncx);

let flow_borrows = Borrows::new(tcx, &body, regioncx.clone(), &borrow_set)
.into_engine(tcx, &body, def.did.to_def_id())
.into_engine(tcx, &body)
.pass_name("borrowck")
.iterate_to_fixpoint();
let flow_uninits = MaybeUninitializedPlaces::new(tcx, &body, &mdpe)
.into_engine(tcx, &body, def.did.to_def_id())
.into_engine(tcx, &body)
.pass_name("borrowck")
.iterate_to_fixpoint();
let flow_ever_inits = EverInitializedPlaces::new(tcx, &body, &mdpe)
.into_engine(tcx, &body, def.did.to_def_id())
.into_engine(tcx, &body)
.pass_name("borrowck")
.iterate_to_fixpoint();

Expand All @@ -286,7 +285,6 @@ fn do_mir_borrowck<'a, 'tcx>(
infcx,
param_env,
body: promoted_body,
mir_def_id: def.did,
move_data: &move_data,
location_table: &LocationTable::new(promoted_body),
movable_generator,
Expand Down Expand Up @@ -320,7 +318,6 @@ fn do_mir_borrowck<'a, 'tcx>(
infcx,
param_env,
body,
mir_def_id: def.did,
move_data: &mdpe.move_data,
location_table,
movable_generator,
Expand Down Expand Up @@ -474,7 +471,6 @@ crate struct MirBorrowckCtxt<'cx, 'tcx> {
crate infcx: &'cx InferCtxt<'cx, 'tcx>,
param_env: ParamEnv<'tcx>,
body: &'cx Body<'tcx>,
mir_def_id: LocalDefId,
move_data: &'cx MoveData<'tcx>,

/// Map from MIR `Location` to `LocationIndex`; created
Expand Down
16 changes: 8 additions & 8 deletions compiler/rustc_mir/src/borrow_check/nll.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
use rustc_data_structures::fx::FxHashMap;
use rustc_errors::Diagnostic;
use rustc_hir::def_id::{DefId, LocalDefId};
use rustc_hir::def_id::DefId;
use rustc_index::vec::IndexVec;
use rustc_infer::infer::InferCtxt;
use rustc_middle::mir::{
Expand Down Expand Up @@ -58,11 +58,12 @@ crate struct NllOutput<'tcx> {
/// `compute_regions`.
pub(in crate::borrow_check) fn replace_regions_in_mir<'cx, 'tcx>(
infcx: &InferCtxt<'cx, 'tcx>,
def: ty::WithOptConstParam<LocalDefId>,
param_env: ty::ParamEnv<'tcx>,
body: &mut Body<'tcx>,
promoted: &mut IndexVec<Promoted, Body<'tcx>>,
) -> UniversalRegions<'tcx> {
let def = body.source.with_opt_param().as_local().unwrap();

debug!("replace_regions_in_mir(def={:?})", def);

// Compute named region information. This also renumbers the inputs/outputs.
Expand Down Expand Up @@ -156,7 +157,6 @@ fn populate_polonius_move_facts(
/// This may result in errors being reported.
pub(in crate::borrow_check) fn compute_regions<'cx, 'tcx>(
infcx: &InferCtxt<'cx, 'tcx>,
def_id: LocalDefId,
universal_regions: UniversalRegions<'tcx>,
body: &Body<'tcx>,
promoted: &IndexVec<Promoted, Body<'tcx>>,
Expand All @@ -180,7 +180,6 @@ pub(in crate::borrow_check) fn compute_regions<'cx, 'tcx>(
param_env,
body,
promoted,
def_id,
&universal_regions,
location_table,
borrow_set,
Expand Down Expand Up @@ -270,10 +269,12 @@ pub(in crate::borrow_check) fn compute_regions<'cx, 'tcx>(
// Generate various additional constraints.
invalidation::generate_invalidates(infcx.tcx, &mut all_facts, location_table, body, borrow_set);

let def_id = body.source.def_id();

// Dump facts if requested.
let polonius_output = all_facts.and_then(|all_facts| {
if infcx.tcx.sess.opts.debugging_opts.nll_facts {
let def_path = infcx.tcx.def_path(def_id.to_def_id());
let def_path = infcx.tcx.def_path(def_id);
let dir_path =
PathBuf::from("nll-facts").join(def_path.to_filename_friendly_no_crate());
all_facts.write_to_dir(dir_path, location_table).unwrap();
Expand All @@ -293,7 +294,7 @@ pub(in crate::borrow_check) fn compute_regions<'cx, 'tcx>(

// Solve the region constraints.
let (closure_region_requirements, nll_errors) =
regioncx.solve(infcx, &body, def_id.to_def_id(), polonius_output.clone());
regioncx.solve(infcx, &body, polonius_output.clone());

if !nll_errors.is_empty() {
// Suppress unhelpful extra errors in `infer_opaque_types`.
Expand Down Expand Up @@ -364,14 +365,13 @@ pub(super) fn dump_mir_results<'a, 'tcx>(
pub(super) fn dump_annotation<'a, 'tcx>(
infcx: &InferCtxt<'a, 'tcx>,
body: &Body<'tcx>,
mir_def_id: DefId,
regioncx: &RegionInferenceContext<'tcx>,
closure_region_requirements: &Option<ClosureRegionRequirements<'_>>,
opaque_type_values: &FxHashMap<DefId, ty::ResolvedOpaqueTy<'tcx>>,
errors_buffer: &mut Vec<Diagnostic>,
) {
let tcx = infcx.tcx;
let base_def_id = tcx.closure_base_def_id(mir_def_id);
let base_def_id = tcx.closure_base_def_id(body.source.def_id());
if !tcx.has_attr(base_def_id, sym::rustc_regions) {
return;
}
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_mir/src/borrow_check/region_infer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -548,9 +548,9 @@ impl<'tcx> RegionInferenceContext<'tcx> {
&mut self,
infcx: &InferCtxt<'_, 'tcx>,
body: &Body<'tcx>,
mir_def_id: DefId,
polonius_output: Option<Rc<PoloniusOutput>>,
) -> (Option<ClosureRegionRequirements<'tcx>>, RegionErrors<'tcx>) {
let mir_def_id = body.source.def_id();
self.propagate_constraints(body, infcx.tcx);

let mut errors_buffer = RegionErrors::new();
Expand Down
Loading

0 comments on commit 62bfcfd

Please sign in to comment.