Skip to content

Commit

Permalink
Rollup merge of #72103 - lcnr:borrowck-localdefid, r=jonas-schievink
Browse files Browse the repository at this point in the history
borrowck `DefId` -> `LocalDefId`

Replaces some `DefId`s which must always be local with `LocalDefId` in `librustc_mir/borrowck`.

cc @marmeladema
  • Loading branch information
RalfJung authored May 29, 2020
2 parents 5b1490d + 3f661d2 commit 3185491
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 66 deletions.
89 changes: 42 additions & 47 deletions src/librustc_mir/borrow_check/diagnostics/conflict_errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
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))
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 @@ -865,49 +865,42 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
format!("`{}` would have to be valid for `{}`...", name, region_name),
);

if let Some(def_id) = self.mir_def_id.as_local() {
let fn_hir_id = self.infcx.tcx.hir().as_local_hir_id(def_id);
err.span_label(
drop_span,
format!(
"...but `{}` will be dropped here, when the {} returns",
name,
self.infcx
.tcx
.hir()
.opt_name(fn_hir_id)
.map(|name| format!("function `{}`", name))
.unwrap_or_else(|| {
match &self
.infcx
.tcx
.typeck_tables_of(def_id)
.node_type(fn_hir_id)
.kind
{
ty::Closure(..) => "enclosing closure",
ty::Generator(..) => "enclosing generator",
kind => bug!("expected closure or generator, found {:?}", kind),
}
.to_string()
})
),
);
let fn_hir_id = self.infcx.tcx.hir().as_local_hir_id(self.mir_def_id);
err.span_label(
drop_span,
format!(
"...but `{}` will be dropped here, when the {} returns",
name,
self.infcx
.tcx
.hir()
.opt_name(fn_hir_id)
.map(|name| format!("function `{}`", name))
.unwrap_or_else(|| {
match &self
.infcx
.tcx
.typeck_tables_of(self.mir_def_id)
.node_type(fn_hir_id)
.kind
{
ty::Closure(..) => "enclosing closure",
ty::Generator(..) => "enclosing generator",
kind => bug!("expected closure or generator, found {:?}", kind),
}
.to_string()
})
),
);

err.note(
"functions cannot return a borrow to data owned within the function's scope, \
functions can only return borrows to data passed as arguments",
);
err.note(
"to learn more, visit <https://doc.rust-lang.org/book/ch04-02-\
references-and-borrowing.html#dangling-references>",
);
} else {
err.span_label(
drop_span,
format!("...but `{}` dropped here while still borrowed", name),
);
}
err.note(
"functions cannot return a borrow to data owned within the function's scope, \
functions can only return borrows to data passed as arguments",
);
err.note(
"to learn more, visit <https://doc.rust-lang.org/book/ch04-02-\
references-and-borrowing.html#dangling-references>",
);

if let BorrowExplanation::MustBeValidFor { .. } = explanation {
} else {
Expand Down Expand Up @@ -1237,7 +1230,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);
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 @@ -1572,14 +1565,16 @@ 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);
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);
match ty.kind {
ty::FnDef(_, _) | ty::FnPtr(_) => self
.annotate_fn_sig(self.mir_def_id, self.infcx.tcx.fn_sig(self.mir_def_id)),
ty::FnDef(_, _) | ty::FnPtr(_) => self.annotate_fn_sig(
self.mir_def_id.to_def_id(),
self.infcx.tcx.fn_sig(self.mir_def_id),
),
_ => None,
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/librustc_mir/borrow_check/diagnostics/move_errors.rs
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 == 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.as_local_hir_id(self.mir_def_id.expect_local());
let closure_id = hir.as_local_hir_id(self.mir_def_id);
let fn_call_id = hir.get_parent_node(closure_id);
let node = hir.get(fn_call_id);
let item_id = hir.get_parent_item(fn_call_id);
Expand Down
2 changes: 1 addition & 1 deletion src/librustc_mir/borrow_check/diagnostics/region_errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -498,7 +498,7 @@ 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);
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
9 changes: 4 additions & 5 deletions src/librustc_mir/borrow_check/diagnostics/region_name.rs
Original file line number Diff line number Diff line change
Expand Up @@ -237,8 +237,7 @@ impl<'tcx> MirBorrowckCtxt<'_, 'tcx> {
}

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

if let DefiningTy::Closure(_, substs) = def_ty {
Expand Down Expand Up @@ -323,7 +322,7 @@ impl<'tcx> MirBorrowckCtxt<'_, 'tcx> {
argument_ty: Ty<'tcx>,
argument_index: usize,
) -> Option<RegionName> {
let mir_hir_id = self.infcx.tcx.hir().as_local_hir_id(self.mir_def_id.as_local()?);
let mir_hir_id = self.infcx.tcx.hir().as_local_hir_id(self.mir_def_id);
let fn_decl = self.infcx.tcx.hir().fn_decl_by_hir_id(mir_hir_id)?;
let argument_hir_ty: &hir::Ty<'_> = fn_decl.inputs.get(argument_index)?;
match argument_hir_ty.kind {
Expand Down Expand Up @@ -634,7 +633,7 @@ impl<'tcx> MirBorrowckCtxt<'_, 'tcx> {
highlight.highlighting_region_vid(fr, *self.next_region_name.try_borrow().unwrap());
let type_name = self.infcx.extract_type_name(&return_ty, Some(highlight)).0;

let mir_hir_id = tcx.hir().as_local_hir_id(self.mir_def_id.expect_local());
let mir_hir_id = tcx.hir().as_local_hir_id(self.mir_def_id);

let (return_span, mir_description) = match tcx.hir().get(mir_hir_id) {
hir::Node::Expr(hir::Expr {
Expand Down Expand Up @@ -686,7 +685,7 @@ impl<'tcx> MirBorrowckCtxt<'_, 'tcx> {
highlight.highlighting_region_vid(fr, *self.next_region_name.try_borrow().unwrap());
let type_name = self.infcx.extract_type_name(&yield_ty, Some(highlight)).0;

let mir_hir_id = tcx.hir().as_local_hir_id(self.mir_def_id.expect_local());
let mir_hir_id = tcx.hir().as_local_hir_id(self.mir_def_id);

let yield_span = match tcx.hir().get(mir_hir_id) {
hir::Node::Expr(hir::Expr {
Expand Down
14 changes: 6 additions & 8 deletions src/librustc_mir/borrow_check/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,8 @@ use rustc_data_structures::fx::{FxHashMap, FxHashSet};
use rustc_data_structures::graph::dominators::Dominators;
use rustc_errors::{Applicability, Diagnostic, DiagnosticBuilder, ErrorReported};
use rustc_hir as hir;
use rustc_hir::{
def_id::{DefId, LocalDefId},
HirId, Node,
};
use rustc_hir::def_id::LocalDefId;
use rustc_hir::{HirId, Node};
use rustc_index::bit_set::BitSet;
use rustc_index::vec::IndexVec;
use rustc_infer::infer::{InferCtxt, TyCtxtInferExt};
Expand Down Expand Up @@ -174,7 +172,7 @@ fn do_mir_borrowck<'a, 'tcx>(
let mut body = input_body.clone();
let mut promoted = input_promoted.clone();
let free_regions =
nll::replace_regions_in_mir(infcx, def_id.to_def_id(), param_env, &mut body, &mut promoted);
nll::replace_regions_in_mir(infcx, def_id, param_env, &mut body, &mut promoted);
let body = &body; // no further changes

let location_table = &LocationTable::new(&body);
Expand Down Expand Up @@ -275,7 +273,7 @@ fn do_mir_borrowck<'a, 'tcx>(
let mut promoted_mbcx = MirBorrowckCtxt {
infcx,
body: promoted_body,
mir_def_id: def_id.to_def_id(),
mir_def_id: def_id,
move_data: &move_data,
location_table: &LocationTable::new(promoted_body),
movable_generator,
Expand Down Expand Up @@ -307,7 +305,7 @@ fn do_mir_borrowck<'a, 'tcx>(
let mut mbcx = MirBorrowckCtxt {
infcx,
body,
mir_def_id: def_id.to_def_id(),
mir_def_id: def_id,
move_data: &mdpe.move_data,
location_table,
movable_generator,
Expand Down Expand Up @@ -459,7 +457,7 @@ fn do_mir_borrowck<'a, 'tcx>(
crate struct MirBorrowckCtxt<'cx, 'tcx> {
crate infcx: &'cx InferCtxt<'cx, 'tcx>,
body: &'cx Body<'tcx>,
mir_def_id: DefId,
mir_def_id: LocalDefId,
move_data: &'cx MoveData<'tcx>,

/// Map from MIR `Location` to `LocationIndex`; created
Expand Down
6 changes: 3 additions & 3 deletions src/librustc_mir/borrow_check/nll.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,20 +58,20 @@ crate struct NllOutput<'tcx> {
/// `compute_regions`.
pub(in crate::borrow_check) fn replace_regions_in_mir<'cx, 'tcx>(
infcx: &InferCtxt<'cx, 'tcx>,
def_id: DefId,
def_id: LocalDefId,
param_env: ty::ParamEnv<'tcx>,
body: &mut Body<'tcx>,
promoted: &mut IndexVec<Promoted, Body<'tcx>>,
) -> UniversalRegions<'tcx> {
debug!("replace_regions_in_mir(def_id={:?})", def_id);

// Compute named region information. This also renumbers the inputs/outputs.
let universal_regions = UniversalRegions::new(infcx, def_id.expect_local(), param_env);
let universal_regions = UniversalRegions::new(infcx, def_id, param_env);

// Replace all remaining regions with fresh inference variables.
renumber::renumber_mir(infcx, body, promoted);

let source = MirSource::item(def_id);
let source = MirSource::item(def_id.to_def_id());
mir_util::dump_mir(infcx.tcx, None, "renumber", &0, source, body, |_, _| Ok(()));

universal_regions
Expand Down

0 comments on commit 3185491

Please sign in to comment.