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