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