Skip to content

Commit 1c0cd3f

Browse files
authored
Rollup merge of rust-lang#72103 - lcnr:borrowck-localdefid, r=jonas-schievink
borrowck `DefId` -> `LocalDefId` Replaces some `DefId`s which must always be local with `LocalDefId` in `librustc_mir/borrowck`. cc @marmeladema
2 parents abe39a0 + 3f661d2 commit 1c0cd3f

File tree

7 files changed

+58
-66
lines changed

7 files changed

+58
-66
lines changed

src/librustc_mir/borrow_check/diagnostics/conflict_errors.rs

+42-47
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,7 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
214214
let generics = tcx.generics_of(self.mir_def_id);
215215
let param = generics.type_param(&param_ty, tcx);
216216
if let Some(generics) =
217-
tcx.hir().get_generics(tcx.closure_base_def_id(self.mir_def_id))
217+
tcx.hir().get_generics(tcx.closure_base_def_id(self.mir_def_id.to_def_id()))
218218
{
219219
suggest_constraining_type_param(
220220
tcx,
@@ -865,49 +865,42 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
865865
format!("`{}` would have to be valid for `{}`...", name, region_name),
866866
);
867867

868-
if let Some(def_id) = self.mir_def_id.as_local() {
869-
let fn_hir_id = self.infcx.tcx.hir().as_local_hir_id(def_id);
870-
err.span_label(
871-
drop_span,
872-
format!(
873-
"...but `{}` will be dropped here, when the {} returns",
874-
name,
875-
self.infcx
876-
.tcx
877-
.hir()
878-
.opt_name(fn_hir_id)
879-
.map(|name| format!("function `{}`", name))
880-
.unwrap_or_else(|| {
881-
match &self
882-
.infcx
883-
.tcx
884-
.typeck_tables_of(def_id)
885-
.node_type(fn_hir_id)
886-
.kind
887-
{
888-
ty::Closure(..) => "enclosing closure",
889-
ty::Generator(..) => "enclosing generator",
890-
kind => bug!("expected closure or generator, found {:?}", kind),
891-
}
892-
.to_string()
893-
})
894-
),
895-
);
868+
let fn_hir_id = self.infcx.tcx.hir().as_local_hir_id(self.mir_def_id);
869+
err.span_label(
870+
drop_span,
871+
format!(
872+
"...but `{}` will be dropped here, when the {} returns",
873+
name,
874+
self.infcx
875+
.tcx
876+
.hir()
877+
.opt_name(fn_hir_id)
878+
.map(|name| format!("function `{}`", name))
879+
.unwrap_or_else(|| {
880+
match &self
881+
.infcx
882+
.tcx
883+
.typeck_tables_of(self.mir_def_id)
884+
.node_type(fn_hir_id)
885+
.kind
886+
{
887+
ty::Closure(..) => "enclosing closure",
888+
ty::Generator(..) => "enclosing generator",
889+
kind => bug!("expected closure or generator, found {:?}", kind),
890+
}
891+
.to_string()
892+
})
893+
),
894+
);
896895

897-
err.note(
898-
"functions cannot return a borrow to data owned within the function's scope, \
899-
functions can only return borrows to data passed as arguments",
900-
);
901-
err.note(
902-
"to learn more, visit <https://doc.rust-lang.org/book/ch04-02-\
903-
references-and-borrowing.html#dangling-references>",
904-
);
905-
} else {
906-
err.span_label(
907-
drop_span,
908-
format!("...but `{}` dropped here while still borrowed", name),
909-
);
910-
}
896+
err.note(
897+
"functions cannot return a borrow to data owned within the function's scope, \
898+
functions can only return borrows to data passed as arguments",
899+
);
900+
err.note(
901+
"to learn more, visit <https://doc.rust-lang.org/book/ch04-02-\
902+
references-and-borrowing.html#dangling-references>",
903+
);
911904

912905
if let BorrowExplanation::MustBeValidFor { .. } = explanation {
913906
} else {
@@ -1237,7 +1230,7 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
12371230
) -> DiagnosticBuilder<'cx> {
12381231
let tcx = self.infcx.tcx;
12391232

1240-
let (_, escapes_from) = tcx.article_and_description(self.mir_def_id);
1233+
let (_, escapes_from) = tcx.article_and_description(self.mir_def_id.to_def_id());
12411234

12421235
let mut err =
12431236
borrowck_errors::borrowed_data_escapes_closure(tcx, escape_span, escapes_from);
@@ -1572,14 +1565,16 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
15721565
) -> Option<AnnotatedBorrowFnSignature<'tcx>> {
15731566
// Define a fallback for when we can't match a closure.
15741567
let fallback = || {
1575-
let is_closure = self.infcx.tcx.is_closure(self.mir_def_id);
1568+
let is_closure = self.infcx.tcx.is_closure(self.mir_def_id.to_def_id());
15761569
if is_closure {
15771570
None
15781571
} else {
15791572
let ty = self.infcx.tcx.type_of(self.mir_def_id);
15801573
match ty.kind {
1581-
ty::FnDef(_, _) | ty::FnPtr(_) => self
1582-
.annotate_fn_sig(self.mir_def_id, self.infcx.tcx.fn_sig(self.mir_def_id)),
1574+
ty::FnDef(_, _) | ty::FnPtr(_) => self.annotate_fn_sig(
1575+
self.mir_def_id.to_def_id(),
1576+
self.infcx.tcx.fn_sig(self.mir_def_id),
1577+
),
15831578
_ => None,
15841579
}
15851580
}

src/librustc_mir/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 == 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();

src/librustc_mir/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.as_local_hir_id(self.mir_def_id.expect_local());
495+
let closure_id = hir.as_local_hir_id(self.mir_def_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.get_parent_item(fn_call_id);

src/librustc_mir/borrow_check/diagnostics/region_errors.rs

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

501-
let (_, mir_def_name) = self.infcx.tcx.article_and_description(self.mir_def_id);
501+
let (_, mir_def_name) = self.infcx.tcx.article_and_description(self.mir_def_id.to_def_id());
502502

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

src/librustc_mir/borrow_check/diagnostics/region_name.rs

+4-5
Original file line numberDiff line numberDiff line change
@@ -237,8 +237,7 @@ impl<'tcx> MirBorrowckCtxt<'_, 'tcx> {
237237
}
238238

239239
ty::BoundRegion::BrEnv => {
240-
let mir_hir_id =
241-
self.infcx.tcx.hir().as_local_hir_id(self.mir_def_id.expect_local());
240+
let mir_hir_id = self.infcx.tcx.hir().as_local_hir_id(self.mir_def_id);
242241
let def_ty = self.regioncx.universal_regions().defining_ty;
243242

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

637-
let mir_hir_id = tcx.hir().as_local_hir_id(self.mir_def_id.expect_local());
636+
let mir_hir_id = tcx.hir().as_local_hir_id(self.mir_def_id);
638637

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

689-
let mir_hir_id = tcx.hir().as_local_hir_id(self.mir_def_id.expect_local());
688+
let mir_hir_id = tcx.hir().as_local_hir_id(self.mir_def_id);
690689

691690
let yield_span = match tcx.hir().get(mir_hir_id) {
692691
hir::Node::Expr(hir::Expr {

src/librustc_mir/borrow_check/mod.rs

+6-8
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,8 @@ use rustc_data_structures::fx::{FxHashMap, FxHashSet};
44
use rustc_data_structures::graph::dominators::Dominators;
55
use rustc_errors::{Applicability, Diagnostic, DiagnosticBuilder, ErrorReported};
66
use rustc_hir as hir;
7-
use rustc_hir::{
8-
def_id::{DefId, LocalDefId},
9-
HirId, Node,
10-
};
7+
use rustc_hir::def_id::LocalDefId;
8+
use rustc_hir::{HirId, Node};
119
use rustc_index::bit_set::BitSet;
1210
use rustc_index::vec::IndexVec;
1311
use rustc_infer::infer::{InferCtxt, TyCtxtInferExt};
@@ -174,7 +172,7 @@ fn do_mir_borrowck<'a, 'tcx>(
174172
let mut body = input_body.clone();
175173
let mut promoted = input_promoted.clone();
176174
let free_regions =
177-
nll::replace_regions_in_mir(infcx, def_id.to_def_id(), param_env, &mut body, &mut promoted);
175+
nll::replace_regions_in_mir(infcx, def_id, param_env, &mut body, &mut promoted);
178176
let body = &body; // no further changes
179177

180178
let location_table = &LocationTable::new(&body);
@@ -275,7 +273,7 @@ fn do_mir_borrowck<'a, 'tcx>(
275273
let mut promoted_mbcx = MirBorrowckCtxt {
276274
infcx,
277275
body: promoted_body,
278-
mir_def_id: def_id.to_def_id(),
276+
mir_def_id: def_id,
279277
move_data: &move_data,
280278
location_table: &LocationTable::new(promoted_body),
281279
movable_generator,
@@ -307,7 +305,7 @@ fn do_mir_borrowck<'a, 'tcx>(
307305
let mut mbcx = MirBorrowckCtxt {
308306
infcx,
309307
body,
310-
mir_def_id: def_id.to_def_id(),
308+
mir_def_id: def_id,
311309
move_data: &mdpe.move_data,
312310
location_table,
313311
movable_generator,
@@ -459,7 +457,7 @@ fn do_mir_borrowck<'a, 'tcx>(
459457
crate struct MirBorrowckCtxt<'cx, 'tcx> {
460458
crate infcx: &'cx InferCtxt<'cx, 'tcx>,
461459
body: &'cx Body<'tcx>,
462-
mir_def_id: DefId,
460+
mir_def_id: LocalDefId,
463461
move_data: &'cx MoveData<'tcx>,
464462

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

src/librustc_mir/borrow_check/nll.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -58,20 +58,20 @@ 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_id: DefId,
61+
def_id: LocalDefId,
6262
param_env: ty::ParamEnv<'tcx>,
6363
body: &mut Body<'tcx>,
6464
promoted: &mut IndexVec<Promoted, Body<'tcx>>,
6565
) -> UniversalRegions<'tcx> {
6666
debug!("replace_regions_in_mir(def_id={:?})", def_id);
6767

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

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

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

7777
universal_regions

0 commit comments

Comments
 (0)