mirror of
https://github.com/rust-lang/rust.git
synced 2025-04-28 11:07:42 +00:00
Enforce stronger guarantees for mutable borrows
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]
This commit is contained in:
parent
159e27aebb
commit
d7de4e9aff
@ -438,8 +438,7 @@ impl<'a> CheckLoanCtxt<'a> {
|
|||||||
Some(lp) => {
|
Some(lp) => {
|
||||||
let moved_value_use_kind = match mode {
|
let moved_value_use_kind = match mode {
|
||||||
euv::Copy => {
|
euv::Copy => {
|
||||||
// FIXME(#12624) -- If we are copying the value,
|
self.check_for_copy_of_frozen_path(id, span, &*lp);
|
||||||
// we don't care if it's borrowed.
|
|
||||||
MovedInUse
|
MovedInUse
|
||||||
}
|
}
|
||||||
euv::Move(_) => {
|
euv::Move(_) => {
|
||||||
@ -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,
|
fn check_for_move_of_borrowed_path(&self,
|
||||||
id: ast::NodeId,
|
id: ast::NodeId,
|
||||||
span: Span,
|
span: Span,
|
||||||
|
@ -12,7 +12,7 @@ fn a() {
|
|||||||
let mut v = vec!(1, 2, 3);
|
let mut v = vec!(1, 2, 3);
|
||||||
let vb: &mut [int] = v.as_mut_slice();
|
let vb: &mut [int] = v.as_mut_slice();
|
||||||
match vb {
|
match vb {
|
||||||
[_a, ..tail] => {
|
[_a, ..tail] => { //~ ERROR cannot use `vb[..]` because it was mutably borrowed
|
||||||
v.push(tail[0] + tail[1]); //~ ERROR cannot borrow
|
v.push(tail[0] + tail[1]); //~ ERROR cannot borrow
|
||||||
}
|
}
|
||||||
_ => {}
|
_ => {}
|
||||||
|
@ -12,8 +12,8 @@
|
|||||||
fn broken() {
|
fn broken() {
|
||||||
let mut x = 3;
|
let mut x = 3;
|
||||||
let mut _y = vec!(&mut x);
|
let mut _y = vec!(&mut x);
|
||||||
while x < 10 {
|
while x < 10 { //~ ERROR cannot use `x` because it was mutably borrowed
|
||||||
let mut z = x;
|
let mut z = x; //~ ERROR cannot use `x` because it was mutably borrowed
|
||||||
_y.push(&mut z); //~ ERROR `z` does not live long enough
|
_y.push(&mut z); //~ ERROR `z` does not live long enough
|
||||||
x += 1; //~ ERROR cannot assign
|
x += 1; //~ ERROR cannot assign
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user