Skip to content

Commit

Permalink
Make killing of out-of-scope borrows a pre-statement effect
Browse files Browse the repository at this point in the history
Fixes #46875.
Fixes #46917.
Fixes #46935.
  • Loading branch information
arielb1 committed Dec 24, 2017
1 parent 063b998 commit 17d4e9b
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 2 deletions.
3 changes: 2 additions & 1 deletion src/librustc_mir/borrow_check/flows.rs
Expand Up @@ -88,7 +88,8 @@ impl<'b, 'gcx, 'tcx> fmt::Display for Flows<'b, 'gcx, 'tcx> {
};
saw_one = true;
let borrow_data = &self.borrows.operator().borrows()[borrow.borrow_index()];
s.push_str(&format!("{}", borrow_data));
s.push_str(&format!("{}{}", borrow_data,
if borrow.is_activation() { "@active" } else { "" }));
});
s.push_str("] ");

Expand Down
2 changes: 2 additions & 0 deletions src/librustc_mir/borrow_check/mod.rs
Expand Up @@ -2011,6 +2011,8 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> {
let borrowed = &data[i.borrow_index()];

if self.places_conflict(&borrowed.borrowed_place, place, access) {
debug!("each_borrow_involving_path: {:?} @ {:?} vs. {:?}/{:?}",
i, borrowed, place, access);
let ctrl = op(self, i, borrowed);
if ctrl == Control::Break {
return;
Expand Down
28 changes: 28 additions & 0 deletions src/librustc_mir/dataflow/impls/borrows.rs
Expand Up @@ -635,13 +635,27 @@ impl<'a, 'gcx, 'tcx> BitDenotation for Reservations<'a, 'gcx, 'tcx> {
// `_sets`.
}

fn before_statement_effect(&self,
sets: &mut BlockSets<ReserveOrActivateIndex>,
location: Location) {
debug!("Reservations::before_statement_effect sets: {:?} location: {:?}", sets, location);
self.0.kill_loans_out_of_scope_at_location(sets, location, false);
}

fn statement_effect(&self,
sets: &mut BlockSets<ReserveOrActivateIndex>,
location: Location) {
debug!("Reservations::statement_effect sets: {:?} location: {:?}", sets, location);
self.0.statement_effect_on_borrows(sets, location, false);
}

fn before_terminator_effect(&self,
sets: &mut BlockSets<ReserveOrActivateIndex>,
location: Location) {
debug!("Reservations::before_terminator_effect sets: {:?} location: {:?}", sets, location);
self.0.kill_loans_out_of_scope_at_location(sets, location, false);
}

fn terminator_effect(&self,
sets: &mut BlockSets<ReserveOrActivateIndex>,
location: Location) {
Expand Down Expand Up @@ -682,13 +696,27 @@ impl<'a, 'gcx, 'tcx> BitDenotation for ActiveBorrows<'a, 'gcx, 'tcx> {
// `_sets`.
}

fn before_statement_effect(&self,
sets: &mut BlockSets<ReserveOrActivateIndex>,
location: Location) {
debug!("ActiveBorrows::before_statement_effect sets: {:?} location: {:?}", sets, location);
self.0.kill_loans_out_of_scope_at_location(sets, location, true);
}

fn statement_effect(&self,
sets: &mut BlockSets<ReserveOrActivateIndex>,
location: Location) {
debug!("ActiveBorrows::statement_effect sets: {:?} location: {:?}", sets, location);
self.0.statement_effect_on_borrows(sets, location, true);
}

fn before_terminator_effect(&self,
sets: &mut BlockSets<ReserveOrActivateIndex>,
location: Location) {
debug!("ActiveBorrows::before_terminator_effect sets: {:?} location: {:?}", sets, location);
self.0.kill_loans_out_of_scope_at_location(sets, location, true);
}

fn terminator_effect(&self,
sets: &mut BlockSets<ReserveOrActivateIndex>,
location: Location) {
Expand Down
Expand Up @@ -56,7 +56,6 @@ fn should_also_eventually_be_ok_with_nll() {
let _z = &x;
*y += 1;
//[lxl]~^ ERROR cannot borrow `x` as mutable because it is also borrowed as immutable
//[nll]~^^ ERROR cannot borrow `x` as mutable because it is also borrowed as immutable
}

fn main() { }
30 changes: 30 additions & 0 deletions src/test/ui/nll/borrow-use-issue-46875.rs
@@ -0,0 +1,30 @@
// Copyright 2017 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

#![feature(nll)]

// run-pass

fn vec() {
let mut _x = vec!['c'];
let _y = &_x;
_x = Vec::new();
}

fn int() {
let mut _x = 5;
let _y = &_x;
_x = 7;
}

fn main() {
vec();
int();
}

0 comments on commit 17d4e9b

Please sign in to comment.