]>
git.proxmox.com Git - rustc.git/blob - src/test/compile-fail/borrowck-lend-flow-loop.rs
f841fedf75af913cd03a0e26fe7f17c3b3dfaf5b
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>) {
28 fn loop_overarching_alias_mut() {
29 // In this instance, the borrow encompasses the entire loop.
31 let mut v
: Box
<_
> = box 3;
35 borrow(&*v
); //~ ERROR cannot borrow
39 fn block_overarching_alias_mut() {
40 // In this instance, the borrow encompasses the entire closure call.
42 let mut v
: Box
<_
> = box 3;
45 borrow(&*v
); //~ ERROR cannot borrow
50 fn loop_aliased_mut() {
51 // In this instance, the borrow is carried through the loop.
53 let mut v
: Box
<_
> = box 3;
54 let mut w
: Box
<_
> = box 4;
57 borrow_mut(&mut *v
); //~ ERROR cannot borrow
62 fn while_aliased_mut() {
63 // In this instance, the borrow is carried through the loop.
65 let mut v
: Box
<_
> = box 3;
66 let mut w
: Box
<_
> = box 4;
69 borrow_mut(&mut *v
); //~ ERROR cannot borrow
75 fn loop_aliased_mut_break() {
76 // In this instance, the borrow is carried through the loop.
78 let mut v
: Box
<_
> = box 3;
79 let mut w
: Box
<_
> = box 4;
86 borrow_mut(&mut *v
); //~ ERROR cannot borrow
89 fn while_aliased_mut_break() {
90 // In this instance, the borrow is carried through the loop.
92 let mut v
: Box
<_
> = box 3;
93 let mut w
: Box
<_
> = box 4;
100 borrow_mut(&mut *v
); //~ ERROR cannot borrow
103 fn while_aliased_mut_cond(cond
: bool
, cond2
: bool
) {
104 let mut v
: Box
<_
> = box 3;
105 let mut w
: Box
<_
> = box 4;
109 borrow(&*v
); //~ ERROR cannot borrow
111 x
= &mut v
; //~ ERROR cannot borrow
116 fn loop_break_pops_scopes
<'r
, F
>(_v
: &'r
mut [usize], mut f
: F
) where
117 F
: FnMut(&'r
mut usize) -> bool
,
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.
125 // this borrow is limited to the scope of `r`...
126 let r
: &'r
mut usize = produce();
128 break; // ...so it is not live as exit the `while` loop here
134 fn loop_loop_pops_scopes
<'r
, F
>(_v
: &'r
mut [usize], mut f
: F
)
135 where F
: FnMut(&'r
mut usize) -> bool
137 // Similar to `loop_break_pops_scopes` but for the `loop` keyword
141 // this borrow is limited to the scope of `r`...
142 let r
: &'r
mut usize = produce();
144 continue; // ...so it is not live as exit (and re-enter) the `while` loop here