]>
git.proxmox.com Git - rustc.git/blob - 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.
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.
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
17 #![feature(box_syntax)]
19 fn borrow(_v
: &isize) {}
20 fn borrow_mut(_v
: &mut isize) {}
21 fn cond() -> bool { panic!() }
22 fn produce
<T
>() -> T { panic!(); }
24 fn inc(v
: &mut Box
<isize>) {
26 //~^ WARN deprecated syntax
29 fn loop_overarching_alias_mut() {
30 // In this instance, the borrow encompasses the entire loop.
32 let mut v
: Box
<_
> = box 3;
36 borrow(&*v
); //~ ERROR cannot borrow
40 fn block_overarching_alias_mut() {
41 // In this instance, the borrow encompasses the entire closure call.
43 let mut v
: Box
<_
> = box 3;
46 borrow(&*v
); //~ ERROR cannot borrow
51 fn loop_aliased_mut() {
52 // In this instance, the borrow is carried through the loop.
54 let mut v
: Box
<_
> = box 3;
55 let mut w
: Box
<_
> = box 4;
58 borrow_mut(&mut *v
); //~ ERROR cannot borrow
63 fn while_aliased_mut() {
64 // In this instance, the borrow is carried through the loop.
66 let mut v
: Box
<_
> = box 3;
67 let mut w
: Box
<_
> = box 4;
70 borrow_mut(&mut *v
); //~ ERROR cannot borrow
76 fn loop_aliased_mut_break() {
77 // In this instance, the borrow is carried through the loop.
79 let mut v
: Box
<_
> = box 3;
80 let mut w
: Box
<_
> = box 4;
87 borrow_mut(&mut *v
); //~ ERROR cannot borrow
90 fn while_aliased_mut_break() {
91 // In this instance, the borrow is carried through the loop.
93 let mut v
: Box
<_
> = box 3;
94 let mut w
: Box
<_
> = box 4;
101 borrow_mut(&mut *v
); //~ ERROR cannot borrow
104 fn while_aliased_mut_cond(cond
: bool
, cond2
: bool
) {
105 let mut v
: Box
<_
> = box 3;
106 let mut w
: Box
<_
> = box 4;
110 borrow(&*v
); //~ ERROR cannot borrow
112 x
= &mut v
; //~ ERROR cannot borrow
117 fn loop_break_pops_scopes
<'r
, F
>(_v
: &'r
mut [usize], mut f
: F
) where
118 F
: FnMut(&'r
mut usize) -> bool
,
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.
126 // this borrow is limited to the scope of `r`...
127 let r
: &'r
mut usize = produce();
129 break; // ...so it is not live as exit the `while` loop here
135 fn loop_loop_pops_scopes
<'r
, F
>(_v
: &'r
mut [usize], mut f
: F
)
136 where F
: FnMut(&'r
mut usize) -> bool
138 // Similar to `loop_break_pops_scopes` but for the `loop` keyword
142 // this borrow is limited to the scope of `r`...
143 let r
: &'r
mut usize = produce();
145 continue; // ...so it is not live as exit (and re-enter) the `while` loop here