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