Skip to content

Commit 650a61c

Browse files
committed
Refactor MirBorrowckCtxt to take infcx instead of tcx.
1 parent 9e3889e commit 650a61c

File tree

6 files changed

+113
-108
lines changed

6 files changed

+113
-108
lines changed

src/librustc_mir/borrow_check/error_reporting.rs

Lines changed: 40 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> {
7272
Some(name) => format!("`{}`", name),
7373
None => "value".to_owned(),
7474
};
75-
let mut err = self.tcx.cannot_act_on_uninitialized_variable(
75+
let mut err = self.infcx.tcx.cannot_act_on_uninitialized_variable(
7676
span,
7777
desired_action.as_noun(),
7878
&self
@@ -99,7 +99,7 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> {
9999

100100
let msg = ""; //FIXME: add "partially " or "collaterally "
101101

102-
let mut err = self.tcx.cannot_act_on_moved_value(
102+
let mut err = self.infcx.tcx.cannot_act_on_moved_value(
103103
span,
104104
desired_action.as_noun(),
105105
msg,
@@ -151,9 +151,9 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> {
151151
if let Some(ty) = self.retrieve_type_for_place(place) {
152152
let needs_note = match ty.sty {
153153
ty::Closure(id, _) => {
154-
let tables = self.tcx.typeck_tables_of(id);
155-
let node_id = self.tcx.hir.as_local_node_id(id).unwrap();
156-
let hir_id = self.tcx.hir.node_to_hir_id(node_id);
154+
let tables = self.infcx.tcx.typeck_tables_of(id);
155+
let node_id = self.infcx.tcx.hir.as_local_node_id(id).unwrap();
156+
let hir_id = self.infcx.tcx.hir.node_to_hir_id(node_id);
157157
if tables.closure_kind_origins().get(hir_id).is_some() {
158158
false
159159
} else {
@@ -200,7 +200,7 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> {
200200
(place, _span): (&Place<'tcx>, Span),
201201
borrow: &BorrowData<'tcx>,
202202
) {
203-
let tcx = self.tcx;
203+
let tcx = self.infcx.tcx;
204204
let value_msg = match self.describe_place(place) {
205205
Some(name) => format!("`{}`", name),
206206
None => "value".to_owned(),
@@ -228,7 +228,8 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> {
228228

229229
move_spans.var_span_label(&mut err, "move occurs due to use in closure");
230230

231-
self.explain_why_borrow_contains_point(context, borrow, None).emit(self.tcx, &mut err);
231+
self.explain_why_borrow_contains_point(context, borrow, None)
232+
.emit(self.infcx.tcx, &mut err);
232233
err.buffer(&mut self.errors_buffer);
233234
}
234235

@@ -238,7 +239,7 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> {
238239
(place, _span): (&Place<'tcx>, Span),
239240
borrow: &BorrowData<'tcx>,
240241
) {
241-
let tcx = self.tcx;
242+
let tcx = self.infcx.tcx;
242243

243244
let borrow_spans = self.retrieve_borrow_spans(borrow);
244245
let borrow_span = borrow_spans.args_or_use();
@@ -265,7 +266,8 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> {
265266
format!("borrow occurs due to use of `{}` in closure", desc_place)
266267
});
267268

268-
self.explain_why_borrow_contains_point(context, borrow, None).emit(self.tcx, &mut err);
269+
self.explain_why_borrow_contains_point(context, borrow, None)
270+
.emit(self.infcx.tcx, &mut err);
269271
err.buffer(&mut self.errors_buffer);
270272
}
271273

@@ -283,7 +285,7 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> {
283285
let span = borrow_spans.args_or_use();
284286

285287
let desc_place = self.describe_place(place).unwrap_or("_".to_owned());
286-
let tcx = self.tcx;
288+
let tcx = self.infcx.tcx;
287289

288290
// FIXME: supply non-"" `opt_via` when appropriate
289291
let mut err = match (
@@ -393,7 +395,7 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> {
393395
}
394396

395397
self.explain_why_borrow_contains_point(context, issued_borrow, None)
396-
.emit(self.tcx, &mut err);
398+
.emit(self.infcx.tcx, &mut err);
397399

398400
err.buffer(&mut self.errors_buffer);
399401
}
@@ -420,7 +422,7 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> {
420422
);
421423

422424
let drop_span = place_span.1;
423-
let scope_tree = self.tcx.region_scope_tree(self.mir_def_id);
425+
let scope_tree = self.infcx.tcx.region_scope_tree(self.mir_def_id);
424426
let root_place = self
425427
.prefixes(&borrow.borrowed_place, PrefixSet::All)
426428
.last()
@@ -503,7 +505,7 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> {
503505
context, name, scope_tree, borrow, drop_span, borrow_span
504506
);
505507

506-
let mut err = self.tcx.path_does_not_live_long_enough(
508+
let mut err = self.infcx.tcx.path_does_not_live_long_enough(
507509
borrow_span,
508510
&format!("`{}`", name),
509511
Origin::Mir,
@@ -516,7 +518,7 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> {
516518
);
517519

518520
self.explain_why_borrow_contains_point(context, borrow, kind_place)
519-
.emit(self.tcx, &mut err);
521+
.emit(self.infcx.tcx, &mut err);
520522

521523
err
522524
}
@@ -594,9 +596,9 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> {
594596
drop_span, borrow_span
595597
);
596598

597-
let mut err = self
598-
.tcx
599-
.thread_local_value_does_not_live_long_enough(borrow_span, Origin::Mir);
599+
let mut err = self.infcx.tcx.thread_local_value_does_not_live_long_enough(
600+
borrow_span, Origin::Mir
601+
);
600602

601603
err.span_label(
602604
borrow_span,
@@ -622,7 +624,7 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> {
622624
context, scope_tree, borrow, drop_span, proper_span
623625
);
624626

625-
let tcx = self.tcx;
627+
let tcx = self.infcx.tcx;
626628
let mut err =
627629
tcx.path_does_not_live_long_enough(proper_span, "borrowed value", Origin::Mir);
628630
err.span_label(proper_span, "temporary value does not live long enough");
@@ -638,7 +640,7 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> {
638640
},
639641
_ => {},
640642
}
641-
explanation.emit(self.tcx, &mut err);
643+
explanation.emit(self.infcx.tcx, &mut err);
642644

643645
err
644646
}
@@ -713,7 +715,7 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> {
713715

714716
// check for inits
715717
let mut any_match = false;
716-
drop_flag_effects::for_location_inits(self.tcx, self.mir, self.move_data, l, |m| {
718+
drop_flag_effects::for_location_inits(self.infcx.tcx, self.mir, self.move_data, l, |m| {
717719
if m == mpi {
718720
any_match = true;
719721
}
@@ -737,7 +739,7 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> {
737739
let loan_spans = self.retrieve_borrow_spans(loan);
738740
let loan_span = loan_spans.args_or_use();
739741

740-
let tcx = self.tcx;
742+
let tcx = self.infcx.tcx;
741743
let mut err = tcx.cannot_assign_to_borrowed(
742744
span,
743745
loan_span,
@@ -747,7 +749,7 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> {
747749

748750
loan_spans.var_span_label(&mut err, "borrow occurs due to use in closure");
749751

750-
self.explain_why_borrow_contains_point(context, loan, None).emit(self.tcx, &mut err);
752+
self.explain_why_borrow_contains_point(context, loan, None).emit(self.infcx.tcx, &mut err);
751753

752754
err.buffer(&mut self.errors_buffer);
753755
}
@@ -799,7 +801,7 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> {
799801
Some(decl) => (self.describe_place(err_place), decl.source_info.span),
800802
};
801803

802-
let mut err = self.tcx.cannot_reassign_immutable(
804+
let mut err = self.infcx.tcx.cannot_reassign_immutable(
803805
span,
804806
place_description.as_ref().map(AsRef::as_ref).unwrap_or("_"),
805807
from_arg,
@@ -877,13 +879,13 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> {
877879
self.append_local_to_string(local, buf)?;
878880
}
879881
Place::Static(ref static_) => {
880-
buf.push_str(&self.tcx.item_name(static_.def_id).to_string());
882+
buf.push_str(&self.infcx.tcx.item_name(static_.def_id).to_string());
881883
}
882884
Place::Projection(ref proj) => {
883885
match proj.elem {
884886
ProjectionElem::Deref => {
885887
let upvar_field_projection =
886-
place.is_upvar_field_projection(self.mir, &self.tcx);
888+
place.is_upvar_field_projection(self.mir, &self.infcx.tcx);
887889
if let Some(field) = upvar_field_projection {
888890
let var_index = field.index();
889891
let name = self.mir.upvar_decls[var_index].debug_name.to_string();
@@ -945,7 +947,7 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> {
945947
autoderef = true;
946948

947949
let upvar_field_projection =
948-
place.is_upvar_field_projection(self.mir, &self.tcx);
950+
place.is_upvar_field_projection(self.mir, &self.infcx.tcx);
949951
if let Some(field) = upvar_field_projection {
950952
let var_index = field.index();
951953
let name = self.mir.upvar_decls[var_index].debug_name.to_string();
@@ -1060,10 +1062,10 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> {
10601062
// the local code in the current crate, so this returns an `Option` in case
10611063
// the closure comes from another crate. But in that case we wouldn't
10621064
// be borrowck'ing it, so we can just unwrap:
1063-
let node_id = self.tcx.hir.as_local_node_id(def_id).unwrap();
1064-
let freevar = self.tcx.with_freevars(node_id, |fv| fv[field.index()]);
1065+
let node_id = self.infcx.tcx.hir.as_local_node_id(def_id).unwrap();
1066+
let freevar = self.infcx.tcx.with_freevars(node_id, |fv| fv[field.index()]);
10651067

1066-
self.tcx.hir.name(freevar.var_id()).to_string()
1068+
self.infcx.tcx.hir.name(freevar.var_id()).to_string()
10671069
}
10681070
_ => {
10691071
// Might need a revision when the fields in trait RFC is implemented
@@ -1096,7 +1098,7 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> {
10961098
/// Check if a place is a thread-local static.
10971099
pub fn is_place_thread_local(&self, place: &Place<'tcx>) -> bool {
10981100
if let Place::Static(statik) = place {
1099-
let attrs = self.tcx.get_attrs(statik.def_id);
1101+
let attrs = self.infcx.tcx.get_attrs(statik.def_id);
11001102
let is_thread_local = attrs.iter().any(|attr| attr.check_name("thread_local"));
11011103

11021104
debug!(
@@ -1212,9 +1214,11 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> {
12121214
if let AggregateKind::Closure(def_id, _) = **kind {
12131215
debug!("find_closure_move_span: found closure {:?}", places);
12141216

1215-
if let Some(node_id) = self.tcx.hir.as_local_node_id(def_id) {
1216-
if let Closure(_, _, _, args_span, _) = self.tcx.hir.expect_expr(node_id).node {
1217-
if let Some(var_span) = self.tcx.with_freevars(node_id, |freevars| {
1217+
if let Some(node_id) = self.infcx.tcx.hir.as_local_node_id(def_id) {
1218+
if let Closure(
1219+
_, _, _, args_span, _
1220+
) = self.infcx.tcx.hir.expect_expr(node_id).node {
1221+
if let Some(var_span) = self.infcx.tcx.with_freevars(node_id, |freevars| {
12181222
for (v, place) in freevars.iter().zip(places) {
12191223
match place {
12201224
Operand::Copy(place) | Operand::Move(place)
@@ -1274,16 +1278,16 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> {
12741278
if let AggregateKind::Closure(def_id, _) = **kind {
12751279
debug!("find_closure_borrow_span: found closure {:?}", places);
12761280

1277-
return if let Some(node_id) = self.tcx.hir.as_local_node_id(def_id) {
1281+
return if let Some(node_id) = self.infcx.tcx.hir.as_local_node_id(def_id) {
12781282
let args_span = if let Closure(_, _, _, span, _) =
1279-
self.tcx.hir.expect_expr(node_id).node
1283+
self.infcx.tcx.hir.expect_expr(node_id).node
12801284
{
12811285
span
12821286
} else {
12831287
return OtherUse(use_span);
12841288
};
12851289

1286-
self.tcx
1290+
self.infcx.tcx
12871291
.with_freevars(node_id, |freevars| {
12881292
for (v, place) in freevars.iter().zip(places) {
12891293
match *place {

0 commit comments

Comments
 (0)