2013-03-15 14:24:24 -05:00
|
|
|
// Copyright 2012 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.
|
|
|
|
|
|
|
|
// Note: the borrowck analysis is currently flow-insensitive.
|
|
|
|
// Therefore, some of these errors are marked as spurious and could be
|
|
|
|
// corrected by a simple change to the analysis. The others are
|
|
|
|
// either genuine or would require more advanced changes. The latter
|
|
|
|
// cases are noted.
|
|
|
|
|
2014-05-05 20:56:44 -05:00
|
|
|
|
2013-03-15 14:24:24 -05:00
|
|
|
fn borrow(_v: &int) {}
|
|
|
|
fn borrow_mut(_v: &mut int) {}
|
2013-10-21 15:08:31 -05:00
|
|
|
fn cond() -> bool { fail!() }
|
|
|
|
fn produce<T>() -> T { fail!(); }
|
2013-03-15 14:24:24 -05:00
|
|
|
|
2014-05-05 20:56:44 -05:00
|
|
|
fn inc(v: &mut Box<int>) {
|
|
|
|
*v = box() (**v + 1);
|
2013-03-15 14:24:24 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
fn loop_overarching_alias_mut() {
|
|
|
|
// In this instance, the borrow encompasses the entire loop.
|
|
|
|
|
2014-05-05 20:56:44 -05:00
|
|
|
let mut v = box 3;
|
2013-03-15 14:24:24 -05:00
|
|
|
let mut x = &mut v;
|
|
|
|
**x += 1;
|
|
|
|
loop {
|
|
|
|
borrow(v); //~ ERROR cannot borrow
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn block_overarching_alias_mut() {
|
|
|
|
// In this instance, the borrow encompasses the entire closure call.
|
|
|
|
|
2014-05-05 20:56:44 -05:00
|
|
|
let mut v = box 3;
|
2013-03-15 14:24:24 -05:00
|
|
|
let mut x = &mut v;
|
2014-01-29 18:20:34 -06:00
|
|
|
for _ in range(0, 3) {
|
2013-03-15 14:24:24 -05:00
|
|
|
borrow(v); //~ ERROR cannot borrow
|
2014-01-29 18:20:34 -06:00
|
|
|
}
|
2014-05-05 20:56:44 -05:00
|
|
|
*x = box 5;
|
2013-03-15 14:24:24 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
fn loop_aliased_mut() {
|
|
|
|
// In this instance, the borrow is carried through the loop.
|
|
|
|
|
2014-05-05 20:56:44 -05:00
|
|
|
let mut v = box 3;
|
|
|
|
let mut w = box 4;
|
2013-03-15 14:24:24 -05:00
|
|
|
let mut _x = &w;
|
|
|
|
loop {
|
|
|
|
borrow_mut(v); //~ ERROR cannot borrow
|
|
|
|
_x = &v;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn while_aliased_mut() {
|
|
|
|
// In this instance, the borrow is carried through the loop.
|
|
|
|
|
2014-05-05 20:56:44 -05:00
|
|
|
let mut v = box 3;
|
|
|
|
let mut w = box 4;
|
2013-03-15 14:24:24 -05:00
|
|
|
let mut _x = &w;
|
|
|
|
while cond() {
|
|
|
|
borrow_mut(v); //~ ERROR cannot borrow
|
|
|
|
_x = &v;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
fn loop_aliased_mut_break() {
|
|
|
|
// In this instance, the borrow is carried through the loop.
|
|
|
|
|
2014-05-05 20:56:44 -05:00
|
|
|
let mut v = box 3;
|
|
|
|
let mut w = box 4;
|
2013-03-15 14:24:24 -05:00
|
|
|
let mut _x = &w;
|
|
|
|
loop {
|
|
|
|
borrow_mut(v);
|
|
|
|
_x = &v;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
borrow_mut(v); //~ ERROR cannot borrow
|
|
|
|
}
|
|
|
|
|
|
|
|
fn while_aliased_mut_break() {
|
|
|
|
// In this instance, the borrow is carried through the loop.
|
|
|
|
|
2014-05-05 20:56:44 -05:00
|
|
|
let mut v = box 3;
|
|
|
|
let mut w = box 4;
|
2013-03-15 14:24:24 -05:00
|
|
|
let mut _x = &w;
|
|
|
|
while cond() {
|
|
|
|
borrow_mut(v);
|
|
|
|
_x = &v;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
borrow_mut(v); //~ ERROR cannot borrow
|
|
|
|
}
|
|
|
|
|
|
|
|
fn while_aliased_mut_cond(cond: bool, cond2: bool) {
|
2014-05-05 20:56:44 -05:00
|
|
|
let mut v = box 3;
|
|
|
|
let mut w = box 4;
|
2013-03-15 14:24:24 -05:00
|
|
|
let mut x = &mut w;
|
|
|
|
while cond {
|
|
|
|
**x += 1;
|
|
|
|
borrow(v); //~ ERROR cannot borrow
|
|
|
|
if cond2 {
|
|
|
|
x = &mut v; //~ ERROR cannot borrow
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-11-19 18:34:19 -06:00
|
|
|
fn loop_break_pops_scopes<'r>(_v: &'r mut [uint], f: |&'r mut uint| -> bool) {
|
2013-03-15 14:24:24 -05:00
|
|
|
// Here we check that when you break out of an inner loop, the
|
|
|
|
// borrows that go out of scope as you exit the inner loop are
|
|
|
|
// removed from the bitset.
|
|
|
|
|
|
|
|
while cond() {
|
|
|
|
while cond() {
|
|
|
|
// this borrow is limited to the scope of `r`...
|
|
|
|
let r: &'r mut uint = produce();
|
|
|
|
if !f(&mut *r) {
|
|
|
|
break; // ...so it is not live as exit the `while` loop here
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-11-19 18:34:19 -06:00
|
|
|
fn loop_loop_pops_scopes<'r>(_v: &'r mut [uint], f: |&'r mut uint| -> bool) {
|
2013-03-15 14:24:24 -05:00
|
|
|
// Similar to `loop_break_pops_scopes` but for the `loop` keyword
|
|
|
|
|
|
|
|
while cond() {
|
|
|
|
while cond() {
|
|
|
|
// this borrow is limited to the scope of `r`...
|
|
|
|
let r: &'r mut uint = produce();
|
|
|
|
if !f(&mut *r) {
|
2013-10-01 16:31:03 -05:00
|
|
|
continue; // ...so it is not live as exit (and re-enter) the `while` loop here
|
2013-03-15 14:24:24 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn main() {}
|