2018-11-05 12:59:40 +01:00
|
|
|
// revisions: ast nll
|
|
|
|
|
|
|
|
// Since we are testing nll migration explicitly as a separate
|
|
|
|
// revision, don't worry about the --compare-mode=nll on this test.
|
|
|
|
|
|
|
|
// ignore-compare-mode-nll
|
|
|
|
|
|
|
|
//[ast]compile-flags: -Z borrowck=ast
|
|
|
|
//[nll]compile-flags: -Z borrowck=migrate -Z two-phase-borrows
|
|
|
|
|
|
|
|
// Note: the borrowck analysis was originally a flow-insensitive pass
|
|
|
|
// over the AST. Therefore, some of these (AST) errors are marked as
|
|
|
|
// spurious and are corrected by the flow-sensitive (NLL) analysis.
|
|
|
|
// The others are either genuine or would require more advanced
|
|
|
|
// changes. The latter cases are noted.
|
2013-03-15 15:24:24 -04:00
|
|
|
|
2015-01-07 18:53:58 -08:00
|
|
|
#![feature(box_syntax)]
|
2014-05-05 18:56:44 -07:00
|
|
|
|
2015-01-08 21:54:35 +11:00
|
|
|
fn borrow(_v: &isize) {}
|
|
|
|
fn borrow_mut(_v: &mut isize) {}
|
2014-10-09 15:17:22 -04:00
|
|
|
fn cond() -> bool { panic!() }
|
|
|
|
fn produce<T>() -> T { panic!(); }
|
2013-03-15 15:24:24 -04:00
|
|
|
|
2015-01-08 21:54:35 +11:00
|
|
|
fn inc(v: &mut Box<isize>) {
|
2015-09-24 18:00:08 +03:00
|
|
|
*v = box (**v + 1);
|
2013-03-15 15:24:24 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
fn loop_overarching_alias_mut() {
|
|
|
|
// In this instance, the borrow encompasses the entire loop.
|
|
|
|
|
2015-02-17 21:41:32 +01:00
|
|
|
let mut v: Box<_> = box 3;
|
2013-03-15 15:24:24 -04:00
|
|
|
let mut x = &mut v;
|
|
|
|
**x += 1;
|
|
|
|
loop {
|
2018-11-05 12:59:40 +01:00
|
|
|
borrow(&*v); //[ast]~ ERROR cannot borrow
|
2013-03-15 15:24:24 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn block_overarching_alias_mut() {
|
|
|
|
// In this instance, the borrow encompasses the entire closure call.
|
|
|
|
|
2015-02-17 21:41:32 +01:00
|
|
|
let mut v: Box<_> = box 3;
|
2013-03-15 15:24:24 -04:00
|
|
|
let mut x = &mut v;
|
2015-01-31 17:23:42 +01:00
|
|
|
for _ in 0..3 {
|
2018-11-05 12:59:40 +01:00
|
|
|
borrow(&*v); //[ast]~ ERROR cannot borrow
|
|
|
|
//[nll]~^ ERROR cannot borrow
|
2014-01-30 11:20:34 +11:00
|
|
|
}
|
2014-05-05 18:56:44 -07:00
|
|
|
*x = box 5;
|
2013-03-15 15:24:24 -04:00
|
|
|
}
|
|
|
|
fn loop_aliased_mut() {
|
|
|
|
// In this instance, the borrow is carried through the loop.
|
|
|
|
|
2015-02-17 21:41:32 +01:00
|
|
|
let mut v: Box<_> = box 3;
|
|
|
|
let mut w: Box<_> = box 4;
|
2013-03-15 15:24:24 -04:00
|
|
|
let mut _x = &w;
|
|
|
|
loop {
|
2018-11-05 12:59:40 +01:00
|
|
|
borrow_mut(&mut *v); //[ast]~ ERROR cannot borrow
|
2013-03-15 15:24:24 -04:00
|
|
|
_x = &v;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn while_aliased_mut() {
|
|
|
|
// In this instance, the borrow is carried through the loop.
|
|
|
|
|
2015-02-17 21:41:32 +01:00
|
|
|
let mut v: Box<_> = box 3;
|
|
|
|
let mut w: Box<_> = box 4;
|
2013-03-15 15:24:24 -04:00
|
|
|
let mut _x = &w;
|
|
|
|
while cond() {
|
2018-11-05 12:59:40 +01:00
|
|
|
borrow_mut(&mut *v); //[ast]~ ERROR cannot borrow
|
2013-03-15 15:24:24 -04:00
|
|
|
_x = &v;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
fn loop_aliased_mut_break() {
|
|
|
|
// In this instance, the borrow is carried through the loop.
|
|
|
|
|
2015-02-17 21:41:32 +01:00
|
|
|
let mut v: Box<_> = box 3;
|
|
|
|
let mut w: Box<_> = box 4;
|
2013-03-15 15:24:24 -04:00
|
|
|
let mut _x = &w;
|
|
|
|
loop {
|
2014-06-24 23:11:57 -07:00
|
|
|
borrow_mut(&mut *v);
|
2013-03-15 15:24:24 -04:00
|
|
|
_x = &v;
|
|
|
|
break;
|
|
|
|
}
|
2018-11-05 12:59:40 +01:00
|
|
|
borrow_mut(&mut *v); //[ast]~ ERROR cannot borrow
|
2013-03-15 15:24:24 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
fn while_aliased_mut_break() {
|
|
|
|
// In this instance, the borrow is carried through the loop.
|
|
|
|
|
2015-02-17 21:41:32 +01:00
|
|
|
let mut v: Box<_> = box 3;
|
|
|
|
let mut w: Box<_> = box 4;
|
2013-03-15 15:24:24 -04:00
|
|
|
let mut _x = &w;
|
|
|
|
while cond() {
|
2014-06-24 23:11:57 -07:00
|
|
|
borrow_mut(&mut *v);
|
2013-03-15 15:24:24 -04:00
|
|
|
_x = &v;
|
|
|
|
break;
|
|
|
|
}
|
2018-11-05 12:59:40 +01:00
|
|
|
borrow_mut(&mut *v); //[ast]~ ERROR cannot borrow
|
2013-03-15 15:24:24 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
fn while_aliased_mut_cond(cond: bool, cond2: bool) {
|
2015-02-17 21:41:32 +01:00
|
|
|
let mut v: Box<_> = box 3;
|
|
|
|
let mut w: Box<_> = box 4;
|
2013-03-15 15:24:24 -04:00
|
|
|
let mut x = &mut w;
|
|
|
|
while cond {
|
|
|
|
**x += 1;
|
2018-11-05 12:59:40 +01:00
|
|
|
borrow(&*v); //[ast]~ ERROR cannot borrow
|
|
|
|
//[nll]~^ ERROR cannot borrow
|
2013-03-15 15:24:24 -04:00
|
|
|
if cond2 {
|
2018-11-05 12:59:40 +01:00
|
|
|
x = &mut v; //[ast]~ ERROR cannot borrow
|
2013-03-15 15:24:24 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-01-08 22:02:42 +11:00
|
|
|
fn loop_break_pops_scopes<'r, F>(_v: &'r mut [usize], mut f: F) where
|
|
|
|
F: FnMut(&'r mut usize) -> bool,
|
2015-01-03 10:45:00 -05:00
|
|
|
{
|
2013-03-15 15:24:24 -04: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`...
|
2015-01-08 22:02:42 +11:00
|
|
|
let r: &'r mut usize = produce();
|
2013-03-15 15:24:24 -04:00
|
|
|
if !f(&mut *r) {
|
|
|
|
break; // ...so it is not live as exit the `while` loop here
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-08 10:59:40 -05:00
|
|
|
fn loop_loop_pops_scopes<'r, F>(_v: &'r mut [usize], mut f: F)
|
|
|
|
where F: FnMut(&'r mut usize) -> bool
|
|
|
|
{
|
2013-03-15 15:24:24 -04: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`...
|
2015-01-08 22:02:42 +11:00
|
|
|
let r: &'r mut usize = produce();
|
2013-03-15 15:24:24 -04:00
|
|
|
if !f(&mut *r) {
|
2013-10-01 14:31:03 -07:00
|
|
|
continue; // ...so it is not live as exit (and re-enter) the `while` loop here
|
2013-03-15 15:24:24 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn main() {}
|