]>
git.proxmox.com Git - rustc.git/blob - src/test/compile-fail/borrowck-lend-flow-loop.rs
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
17 fn borrow(_v
: &int
) {}
18 fn borrow_mut(_v
: &mut int
) {}
19 fn cond() -> bool { fail!() }
20 fn for_func(_f
: &fn() -> bool
) -> bool { fail!() }
21 fn produce
<T
>() -> T { fail!(); }
23 fn inc(v
: &mut ~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.
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(v
); //~ ERROR cannot borrow
61 fn while_aliased_mut() {
62 // In this instance, the borrow is carried through the loop.
68 borrow_mut(v
); //~ ERROR cannot borrow
73 fn for_loop_aliased_mut() {
74 // In this instance, the borrow is carried through the loop.
80 borrow_mut(v
); //~ ERROR cannot borrow
85 fn loop_aliased_mut_break() {
86 // In this instance, the borrow is carried through the loop.
96 borrow_mut(v
); //~ ERROR cannot borrow
99 fn while_aliased_mut_break() {
100 // In this instance, the borrow is carried through the loop.
110 borrow_mut(v
); //~ ERROR cannot borrow
113 fn for_aliased_mut_break() {
114 // In this instance, the borrow is carried through the loop.
120 // here we cannot be sure that `for_func` respects the break below
121 borrow_mut(v
); //~ ERROR cannot borrow
125 borrow_mut(v
); //~ ERROR cannot borrow
128 fn while_aliased_mut_cond(cond
: bool
, cond2
: bool
) {
134 borrow(v
); //~ ERROR cannot borrow
136 x
= &mut v
; //~ ERROR cannot borrow
141 fn loop_break_pops_scopes
<'r
>(_v
: &'r
mut [uint
], f
: &fn(&'r
mut uint
) -> bool
) {
142 // Here we check that when you break out of an inner loop, the
143 // borrows that go out of scope as you exit the inner loop are
144 // removed from the bitset.
148 // this borrow is limited to the scope of `r`...
149 let r
: &'r
mut uint
= produce();
151 break; // ...so it is not live as exit the `while` loop here
157 fn loop_loop_pops_scopes
<'r
>(_v
: &'r
mut [uint
], f
: &fn(&'r
mut uint
) -> bool
) {
158 // Similar to `loop_break_pops_scopes` but for the `loop` keyword
162 // this borrow is limited to the scope of `r`...
163 let r
: &'r
mut uint
= produce();
165 loop; // ...so it is not live as exit (and re-enter) the `while` loop here