]> git.proxmox.com Git - rustc.git/blob - src/test/compile-fail/borrowck-lend-flow-if.rs
0efe8622621785c0e762210c11463bd3b51db05c
[rustc.git] / src / test / compile-fail / borrowck-lend-flow-if.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 for_func<F>(_f: F) where F: FnOnce() -> bool { panic!() }
23 fn produce<T>() -> T { panic!(); }
24
25 fn inc(v: &mut Box<isize>) {
26 *v = box() (**v + 1);
27 }
28
29 fn pre_freeze_cond() {
30 // In this instance, the freeze is conditional and starts before
31 // the mut borrow.
32
33 let mut v: Box<_> = box 3;
34 let _w;
35 if cond() {
36 _w = &v;
37 }
38 borrow_mut(&mut *v); //~ ERROR cannot borrow
39 }
40
41 fn pre_freeze_else() {
42 // In this instance, the freeze and mut borrow are on separate sides
43 // of the if.
44
45 let mut v: Box<_> = box 3;
46 let _w;
47 if cond() {
48 _w = &v;
49 } else {
50 borrow_mut(&mut *v);
51 }
52 }
53
54 fn main() {}