]> git.lizzy.rs Git - rust.git/blob - src/test/compile-fail/borrowck-lend-flow-loop.rs
fix cfail tests
[rust.git] / 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.
4 //
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.
10
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
15 // cases are noted.
16
17
18 fn borrow(_v: &int) {}
19 fn borrow_mut(_v: &mut int) {}
20 fn cond() -> bool { panic!() }
21 fn produce<T>() -> T { panic!(); }
22
23 fn inc(v: &mut Box<int>) {
24     *v = box() (**v + 1);
25 }
26
27 fn loop_overarching_alias_mut() {
28     // In this instance, the borrow encompasses the entire loop.
29
30     let mut v = box 3;
31     let mut x = &mut v;
32     **x += 1;
33     loop {
34         borrow(&*v); //~ ERROR cannot borrow
35     }
36 }
37
38 fn block_overarching_alias_mut() {
39     // In this instance, the borrow encompasses the entire closure call.
40
41     let mut v = box 3;
42     let mut x = &mut v;
43     for _ in range(0i, 3) {
44         borrow(&*v); //~ ERROR cannot borrow
45     }
46     *x = box 5;
47 }
48
49 fn loop_aliased_mut() {
50     // In this instance, the borrow is carried through the loop.
51
52     let mut v = box 3;
53     let mut w = box 4;
54     let mut _x = &w;
55     loop {
56         borrow_mut(&mut *v); //~ ERROR cannot borrow
57         _x = &v;
58     }
59 }
60
61 fn while_aliased_mut() {
62     // In this instance, the borrow is carried through the loop.
63
64     let mut v = box 3;
65     let mut w = box 4;
66     let mut _x = &w;
67     while cond() {
68         borrow_mut(&mut *v); //~ ERROR cannot borrow
69         _x = &v;
70     }
71 }
72
73
74 fn loop_aliased_mut_break() {
75     // In this instance, the borrow is carried through the loop.
76
77     let mut v = box 3;
78     let mut w = box 4;
79     let mut _x = &w;
80     loop {
81         borrow_mut(&mut *v);
82         _x = &v;
83         break;
84     }
85     borrow_mut(&mut *v); //~ ERROR cannot borrow
86 }
87
88 fn while_aliased_mut_break() {
89     // In this instance, the borrow is carried through the loop.
90
91     let mut v = box 3;
92     let mut w = box 4;
93     let mut _x = &w;
94     while cond() {
95         borrow_mut(&mut *v);
96         _x = &v;
97         break;
98     }
99     borrow_mut(&mut *v); //~ ERROR cannot borrow
100 }
101
102 fn while_aliased_mut_cond(cond: bool, cond2: bool) {
103     let mut v = box 3;
104     let mut w = box 4;
105     let mut x = &mut w;
106     while cond {
107         **x += 1;
108         borrow(&*v); //~ ERROR cannot borrow
109         if cond2 {
110             x = &mut v; //~ ERROR cannot borrow
111         }
112     }
113 }
114
115 fn loop_break_pops_scopes<'r, F>(_v: &'r mut [uint], mut f: F) where
116     F: FnMut(&'r mut uint) -> bool,
117 {
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.
121
122     while cond() {
123         while cond() {
124             // this borrow is limited to the scope of `r`...
125             let r: &'r mut uint = produce();
126             if !f(&mut *r) {
127                 break; // ...so it is not live as exit the `while` loop here
128             }
129         }
130     }
131 }
132
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
135
136     while cond() {
137         while cond() {
138             // this borrow is limited to the scope of `r`...
139             let r: &'r mut uint = produce();
140             if !f(&mut *r) {
141                 continue; // ...so it is not live as exit (and re-enter) the `while` loop here
142             }
143         }
144     }
145 }
146
147 fn main() {}