Skip to content

Commit

Permalink
Enforce stronger guarantees for mutable borrows
Browse files Browse the repository at this point in the history
Implement the stronger guarantees for mutable borrows from #12624. This
removes the ability to read from a mutably borrowed path for the
duration of the borrow, and enforces a unique access path for any
mutable borrow, for both reads and writes.

This makes mutable borrows work better with concurrent accesses from
multiple threads, and it opens the door for allowing moves out of
mutably borrowed values, as long as a new value is written before the
mutable borrow ends. This also aligns Rust more closely with academic
languages based on substructural types and separation logic.

The most common situation triggering an error after this change is a
call to a function mutably borrowing self with self.field as one of the
arguments. The workaround is to bind self.field to a temporary, but the
need for these temporaries will hopefully go away after #6268 is fixed.

Another situation that triggers an error is using the head expression of
a match in an arm that binds a variable with a mutable reference. The
use of the head expression needs to be replaced with an expression that
reconstructs it from match-bound variables.

This fixes #12624.

[breaking-change]
  • Loading branch information
Cameron Zwarich committed Jun 14, 2014
1 parent 159e27a commit d7de4e9
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 5 deletions.
24 changes: 22 additions & 2 deletions src/librustc/middle/borrowck/check_loans.rs
Expand Up @@ -438,8 +438,7 @@ impl<'a> CheckLoanCtxt<'a> {
Some(lp) => {
let moved_value_use_kind = match mode {
euv::Copy => {
// FIXME(#12624) -- If we are copying the value,
// we don't care if it's borrowed.
self.check_for_copy_of_frozen_path(id, span, &*lp);
MovedInUse
}
euv::Move(_) => {
Expand Down Expand Up @@ -471,6 +470,27 @@ impl<'a> CheckLoanCtxt<'a> {
}
}

fn check_for_copy_of_frozen_path(&self,
id: ast::NodeId,
span: Span,
copy_path: &LoanPath) {
match self.analyze_restrictions_on_use(id, copy_path, ty::ImmBorrow) {
UseOk => { }
UseWhileBorrowed(loan_path, loan_span) => {
self.bccx.span_err(
span,
format!("cannot use `{}` because it was mutably borrowed",
self.bccx.loan_path_to_str(copy_path).as_slice())
.as_slice());
self.bccx.span_note(
loan_span,
format!("borrow of `{}` occurs here",
self.bccx.loan_path_to_str(&*loan_path).as_slice())
.as_slice());
}
}
}

fn check_for_move_of_borrowed_path(&self,
id: ast::NodeId,
span: Span,
Expand Down
Expand Up @@ -12,7 +12,7 @@ fn a() {
let mut v = vec!(1, 2, 3);
let vb: &mut [int] = v.as_mut_slice();
match vb {
[_a, ..tail] => {
[_a, ..tail] => { //~ ERROR cannot use `vb[..]` because it was mutably borrowed
v.push(tail[0] + tail[1]); //~ ERROR cannot borrow
}
_ => {}
Expand Down
4 changes: 2 additions & 2 deletions src/test/compile-fail/regions-escape-loop-via-vec.rs
Expand Up @@ -12,8 +12,8 @@
fn broken() {
let mut x = 3;
let mut _y = vec!(&mut x);
while x < 10 {
let mut z = x;
while x < 10 { //~ ERROR cannot use `x` because it was mutably borrowed
let mut z = x; //~ ERROR cannot use `x` because it was mutably borrowed
_y.push(&mut z); //~ ERROR `z` does not live long enough
x += 1; //~ ERROR cannot assign
}
Expand Down

0 comments on commit d7de4e9

Please sign in to comment.