]> git.lizzy.rs Git - rust.git/blob - src/test/compile-fail/borrowck-lend-flow-match.rs
auto merge of #15069 : luqmana/rust/cia, r=pcwalton
[rust.git] / src / test / compile-fail / borrowck-lend-flow-match.rs
1 // Copyright 2012-2014 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 #![allow(unused_variable)]
12 #![allow(dead_assignment)]
13
14 fn cond() -> bool { fail!() }
15 fn link<'a>(v: &'a uint, w: &mut &'a uint) -> bool { *w = v; true }
16
17 fn separate_arms() {
18     // Here both arms perform assignments, but only is illegal.
19
20     let mut x = None;
21     match x {
22         None => {
23             // It is ok to reassign x here, because there is in
24             // fact no outstanding loan of x!
25             x = Some(0i);
26         }
27         Some(ref _i) => {
28             x = Some(1i); //~ ERROR cannot assign
29         }
30     }
31     x.clone(); // just to prevent liveness warnings
32 }
33
34 fn guard() {
35     // Here the guard performs a borrow. This borrow "infects" all
36     // subsequent arms (but not the prior ones).
37
38     let mut a = box 3u;
39     let mut b = box 4u;
40     let mut w = &*a;
41     match 22i {
42         _ if cond() => {
43             b = box 5u;
44         }
45
46         _ if link(&*b, &mut w) => {
47             b = box 6u; //~ ERROR cannot assign
48         }
49
50         _ => {
51             b = box 7u; //~ ERROR cannot assign
52         }
53     }
54
55     b = box 8; //~ ERROR cannot assign
56 }
57
58 fn main() {}