1 // Copyright 2012 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
11 // Note: the borrowck analysis is currently flow-insensitive.
12 // Therefore, some of these errors are marked as spurious and could be
13 // corrected by a simple change to the analysis. The others are
14 // either genuine or would require more advanced changes. The latter
18 fn borrow(_v: &int) {}
19 fn borrow_mut(_v: &mut int) {}
20 fn cond() -> bool { panic!() }
21 fn produce<T>() -> T { panic!(); }
23 fn inc(v: &mut Box<int>) {
27 fn loop_overarching_alias_mut() {
28 // In this instance, the borrow encompasses the entire loop.
34 borrow(&*v); //~ ERROR cannot borrow
38 fn block_overarching_alias_mut() {
39 // In this instance, the borrow encompasses the entire closure call.
43 for _ in range(0i, 3) {
44 borrow(&*v); //~ ERROR cannot borrow
49 fn loop_aliased_mut() {
50 // In this instance, the borrow is carried through the loop.
56 borrow_mut(&mut *v); //~ ERROR cannot borrow
61 fn while_aliased_mut() {
62 // In this instance, the borrow is carried through the loop.
68 borrow_mut(&mut *v); //~ ERROR cannot borrow
74 fn loop_aliased_mut_break() {
75 // In this instance, the borrow is carried through the loop.
85 borrow_mut(&mut *v); //~ ERROR cannot borrow
88 fn while_aliased_mut_break() {
89 // In this instance, the borrow is carried through the loop.
99 borrow_mut(&mut *v); //~ ERROR cannot borrow
102 fn while_aliased_mut_cond(cond: bool, cond2: bool) {
108 borrow(&*v); //~ ERROR cannot borrow
110 x = &mut v; //~ ERROR cannot borrow
115 fn loop_break_pops_scopes<'r, F>(_v: &'r mut [uint], mut f: F) where
116 F: FnMut(&'r mut uint) -> bool,
118 // Here we check that when you break out of an inner loop, the
119 // borrows that go out of scope as you exit the inner loop are
120 // removed from the bitset.
124 // this borrow is limited to the scope of `r`...
125 let r: &'r mut uint = produce();
127 break; // ...so it is not live as exit the `while` loop here
133 fn loop_loop_pops_scopes<'r, F>(_v: &'r mut [uint], mut f: F) where F: FnMut(&'r mut uint) -> bool {
134 // Similar to `loop_break_pops_scopes` but for the `loop` keyword
138 // this borrow is limited to the scope of `r`...
139 let r: &'r mut uint = produce();
141 continue; // ...so it is not live as exit (and re-enter) the `while` loop here