Skip to content

Commit 7af3ddd

Browse files
csmoespastorino
authored andcommitted
introduce neo_place in borrowck/path_utils
1 parent 2d1dce8 commit 7af3ddd

File tree

3 files changed

+40
-29
lines changed

3 files changed

+40
-29
lines changed

src/librustc_mir/borrow_check/mod.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1381,7 +1381,9 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> {
13811381
fn check_for_local_borrow(&mut self, borrow: &BorrowData<'tcx>, yield_span: Span) {
13821382
debug!("check_for_local_borrow({:?})", borrow);
13831383

1384-
if borrow_of_local_data(&borrow.borrowed_place) {
1384+
let neo_place = self.infcx.tcx
1385+
.as_new_place(&borrow.borrowed_place);
1386+
if borrow_of_local_data(&neo_place) {
13851387
let err = self.infcx.tcx
13861388
.cannot_borrow_across_generator_yield(
13871389
self.retrieve_borrow_spans(borrow).var_or_use(),

src/librustc_mir/borrow_check/nll/invalidation.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,8 @@ impl<'cx, 'tcx, 'gcx> Visitor<'tcx> for InvalidationGenerator<'cx, 'tcx, 'gcx> {
237237
let borrow_set = self.borrow_set.clone();
238238
let resume = self.location_table.start_index(resume.start_location());
239239
for i in borrow_set.borrows.indices() {
240-
if borrow_of_local_data(&borrow_set.borrows[i].borrowed_place) {
240+
let neo_place = self.tcx.as_new_place(&borrow_set.borrows[i].borrowed_place);
241+
if borrow_of_local_data(&neo_place) {
241242
self.all_facts.invalidates.push((resume, i));
242243
}
243244
}
@@ -247,7 +248,8 @@ impl<'cx, 'tcx, 'gcx> Visitor<'tcx> for InvalidationGenerator<'cx, 'tcx, 'gcx> {
247248
let borrow_set = self.borrow_set.clone();
248249
let start = self.location_table.start_index(location);
249250
for i in borrow_set.borrows.indices() {
250-
if borrow_of_local_data(&borrow_set.borrows[i].borrowed_place) {
251+
let neo_place = self.tcx.as_new_place(&borrow_set.borrows[i].borrowed_place);
252+
if borrow_of_local_data(&neo_place) {
251253
self.all_facts.invalidates.push((start, i));
252254
}
253255
}

src/librustc_mir/borrow_check/path_utils.rs

Lines changed: 33 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1-
use borrow_check::borrow_set::{BorrowSet, BorrowData, TwoPhaseActivation};
1+
use borrow_check::borrow_set::{BorrowData, BorrowSet, TwoPhaseActivation};
22
use borrow_check::places_conflict;
3-
use borrow_check::Context;
43
use borrow_check::AccessDepth;
4+
use borrow_check::Context;
55
use dataflow::indexes::BorrowIndex;
66
use rustc::mir::{BasicBlock, Location, Mir, Place};
7-
use rustc::mir::{ProjectionElem, BorrowKind};
7+
use rustc::mir::{BorrowKind, ProjectionElem};
8+
use rustc::mir::{NeoPlace, PlaceBase};
89
use rustc::ty::TyCtxt;
910
use rustc_data_structures::graph::dominators::Dominators;
1011

@@ -13,11 +14,10 @@ use rustc_data_structures::graph::dominators::Dominators;
1314
/// Activation phases.
1415
pub(super) fn allow_two_phase_borrow<'a, 'tcx, 'gcx: 'tcx>(
1516
tcx: &TyCtxt<'a, 'gcx, 'tcx>,
16-
kind: BorrowKind
17+
kind: BorrowKind,
1718
) -> bool {
1819
tcx.two_phase_borrows()
19-
&& (kind.allows_two_phase_borrow()
20-
|| tcx.sess.opts.debugging_opts.two_phase_beyond_autoref)
20+
&& (kind.allows_two_phase_borrow() || tcx.sess.opts.debugging_opts.two_phase_beyond_autoref)
2121
}
2222

2323
/// Control for the path borrow checking code
@@ -28,7 +28,7 @@ pub(super) enum Control {
2828
}
2929

3030
/// Encapsulates the idea of iterating over every borrow that involves a particular path
31-
pub(super) fn each_borrow_involving_path<'a, 'tcx, 'gcx: 'tcx, F, I, S> (
31+
pub(super) fn each_borrow_involving_path<'a, 'tcx, 'gcx: 'tcx, F, I, S>(
3232
s: &mut S,
3333
tcx: TyCtxt<'a, 'gcx, 'tcx>,
3434
mir: &Mir<'tcx>,
@@ -39,7 +39,7 @@ pub(super) fn each_borrow_involving_path<'a, 'tcx, 'gcx: 'tcx, F, I, S> (
3939
mut op: F,
4040
) where
4141
F: FnMut(&mut S, BorrowIndex, &BorrowData<'tcx>) -> Control,
42-
I: Iterator<Item=BorrowIndex>
42+
I: Iterator<Item = BorrowIndex>,
4343
{
4444
let (access, place) = access_place;
4545

@@ -75,9 +75,12 @@ pub(super) fn each_borrow_involving_path<'a, 'tcx, 'gcx: 'tcx, F, I, S> (
7575
pub(super) fn is_active<'tcx>(
7676
dominators: &Dominators<BasicBlock>,
7777
borrow_data: &BorrowData<'tcx>,
78-
location: Location
78+
location: Location,
7979
) -> bool {
80-
debug!("is_active(borrow_data={:?}, location={:?})", borrow_data, location);
80+
debug!(
81+
"is_active(borrow_data={:?}, location={:?})",
82+
borrow_data, location
83+
);
8184

8285
let activation_location = match borrow_data.activation_location {
8386
// If this is not a 2-phase borrow, it is always active.
@@ -136,24 +139,28 @@ pub(super) fn is_active<'tcx>(
136139

137140
/// Determines if a given borrow is borrowing local data
138141
/// This is called for all Yield statements on movable generators
139-
pub(super) fn borrow_of_local_data<'tcx>(place: &Place<'tcx>) -> bool {
140-
match place {
141-
Place::Promoted(_) |
142-
Place::Static(..) => false,
143-
Place::Local(..) => true,
144-
Place::Projection(box proj) => {
145-
match proj.elem {
146-
// Reborrow of already borrowed data is ignored
147-
// Any errors will be caught on the initial borrow
148-
ProjectionElem::Deref => false,
142+
pub(super) fn borrow_of_local_data<'tcx>(place: &NeoPlace<'tcx>) -> bool {
143+
let mut borrow_of_local_data = match place.base {
144+
PlaceBase::Promoted(_) | PlaceBase::Static(..) => false,
145+
PlaceBase::Local(..) => true,
146+
};
149147

150-
// For interior references and downcasts, find out if the base is local
151-
ProjectionElem::Field(..)
152-
| ProjectionElem::Index(..)
153-
| ProjectionElem::ConstantIndex { .. }
154-
| ProjectionElem::Subslice { .. }
155-
| ProjectionElem::Downcast(..) => borrow_of_local_data(&proj.base),
148+
for elem in place.elems.iter().rev() {
149+
match elem {
150+
// Reborrow of already borrowed data is ignored
151+
// Any errors will be caught on the initial borrow
152+
ProjectionElem::Deref => {
153+
borrow_of_local_data = false;
154+
break;
156155
}
156+
157+
// For interior references and downcasts, find out if the base is local
158+
ProjectionElem::Field(..)
159+
| ProjectionElem::Index(..)
160+
| ProjectionElem::ConstantIndex { .. }
161+
| ProjectionElem::Subslice { .. }
162+
| ProjectionElem::Downcast(..) => {}
157163
}
158164
}
165+
borrow_of_local_data
159166
}

0 commit comments

Comments
 (0)