]> git.proxmox.com Git - rustc.git/blob - src/test/compile-fail/borrowck-lend-flow-loop.rs
Imported Upstream version 0.7
[rustc.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 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!(); }
22
23 fn inc(v: &mut ~int) {
24 *v = ~(**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 = ~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 = ~3;
42 let mut x = &mut v;
43 for 3.times {
44 borrow(v); //~ ERROR cannot borrow
45 }
46 *x = ~5;
47 }
48
49 fn loop_aliased_mut() {
50 // In this instance, the borrow is carried through the loop.
51
52 let mut v = ~3;
53 let mut w = ~4;
54 let mut _x = &w;
55 loop {
56 borrow_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 = ~3;
65 let mut w = ~4;
66 let mut _x = &w;
67 while cond() {
68 borrow_mut(v); //~ ERROR cannot borrow
69 _x = &v;
70 }
71 }
72
73 fn for_loop_aliased_mut() {
74 // In this instance, the borrow is carried through the loop.
75
76 let mut v = ~3;
77 let mut w = ~4;
78 let mut _x = &w;
79 for for_func {
80 borrow_mut(v); //~ ERROR cannot borrow
81 _x = &v;
82 }
83 }
84
85 fn loop_aliased_mut_break() {
86 // In this instance, the borrow is carried through the loop.
87
88 let mut v = ~3;
89 let mut w = ~4;
90 let mut _x = &w;
91 loop {
92 borrow_mut(v);
93 _x = &v;
94 break;
95 }
96 borrow_mut(v); //~ ERROR cannot borrow
97 }
98
99 fn while_aliased_mut_break() {
100 // In this instance, the borrow is carried through the loop.
101
102 let mut v = ~3;
103 let mut w = ~4;
104 let mut _x = &w;
105 while cond() {
106 borrow_mut(v);
107 _x = &v;
108 break;
109 }
110 borrow_mut(v); //~ ERROR cannot borrow
111 }
112
113 fn for_aliased_mut_break() {
114 // In this instance, the borrow is carried through the loop.
115
116 let mut v = ~3;
117 let mut w = ~4;
118 let mut _x = &w;
119 for for_func {
120 // here we cannot be sure that `for_func` respects the break below
121 borrow_mut(v); //~ ERROR cannot borrow
122 _x = &v;
123 break;
124 }
125 borrow_mut(v); //~ ERROR cannot borrow
126 }
127
128 fn while_aliased_mut_cond(cond: bool, cond2: bool) {
129 let mut v = ~3;
130 let mut w = ~4;
131 let mut x = &mut w;
132 while cond {
133 **x += 1;
134 borrow(v); //~ ERROR cannot borrow
135 if cond2 {
136 x = &mut v; //~ ERROR cannot borrow
137 }
138 }
139 }
140
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.
145
146 while cond() {
147 while cond() {
148 // this borrow is limited to the scope of `r`...
149 let r: &'r mut uint = produce();
150 if !f(&mut *r) {
151 break; // ...so it is not live as exit the `while` loop here
152 }
153 }
154 }
155 }
156
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
159
160 while cond() {
161 while cond() {
162 // this borrow is limited to the scope of `r`...
163 let r: &'r mut uint = produce();
164 if !f(&mut *r) {
165 loop; // ...so it is not live as exit (and re-enter) the `while` loop here
166 }
167 }
168 }
169 }
170
171 fn main() {}