]> git.lizzy.rs Git - rust.git/blob - src/test/compile-fail/borrowck-lend-flow-if.rs
auto merge of #13967 : richo/rust/features/ICE-fails, r=alexcrichton
[rust.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
18 fn borrow(_v: &int) {}
19 fn borrow_mut(_v: &mut int) {}
20 fn cond() -> bool { fail!() }
21 fn for_func(_f: || -> bool) { fail!() }
22 fn produce<T>() -> T { fail!(); }
23
24 fn inc(v: &mut Box<int>) {
25     *v = box() (**v + 1);
26 }
27
28 fn pre_freeze_cond() {
29     // In this instance, the freeze is conditional and starts before
30     // the mut borrow.
31
32     let mut v = box 3;
33     let _w;
34     if cond() {
35         _w = &v;
36     }
37     borrow_mut(v); //~ ERROR cannot borrow
38 }
39
40 fn pre_freeze_else() {
41     // In this instance, the freeze and mut borrow are on separate sides
42     // of the if.
43
44     let mut v = box 3;
45     let _w;
46     if cond() {
47         _w = &v;
48     } else {
49         borrow_mut(v);
50     }
51 }
52
53 fn main() {}