]> git.lizzy.rs Git - rust.git/blob - src/test/compile-fail/borrowck-closures-mut-and-imm.rs
886026e45d90f1f8c89734a3b04a0f77e2b1b572
[rust.git] / src / test / compile-fail / borrowck-closures-mut-and-imm.rs
1 // Copyright 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 // Tests that two closures cannot simultaneously have mutable
12 // and immutable access to the variable. Issue #6801.
13
14
15 fn get(x: &int) -> int {
16     *x
17 }
18
19 fn set(x: &mut int) {
20     *x = 4;
21 }
22
23 fn a() {
24     let mut x = 3i;
25     let c1 = || x = 4;
26     let c2 = || x * 5; //~ ERROR cannot borrow `x`
27 }
28
29 fn b() {
30     let mut x = 3i;
31     let c1 = || set(&mut x);
32     let c2 = || get(&x); //~ ERROR cannot borrow `x`
33 }
34
35 fn c() {
36     let mut x = 3i;
37     let c1 = || set(&mut x);
38     let c2 = || x * 5; //~ ERROR cannot borrow `x`
39 }
40
41 fn d() {
42     let mut x = 3i;
43     let c2 = || x * 5;
44     x = 5; //~ ERROR cannot assign
45 }
46
47 fn e() {
48     let mut x = 3i;
49     let c1 = || get(&x);
50     x = 5; //~ ERROR cannot assign
51 }
52
53 fn f() {
54     let mut x = box 3i;
55     let c1 = || get(&*x);
56     *x = 5; //~ ERROR cannot assign
57 }
58
59 fn g() {
60     struct Foo {
61         f: Box<int>
62     }
63
64     let mut x = box Foo { f: box 3 };
65     let c1 = || get(&*x.f);
66     *x.f = 5; //~ ERROR cannot assign to `*x.f`
67 }
68
69 fn h() {
70     struct Foo {
71         f: Box<int>
72     }
73
74     let mut x = box Foo { f: box 3 };
75     let c1 = || get(&*x.f);
76     let c2 = || *x.f = 5; //~ ERROR cannot borrow `x` as mutable
77 }
78
79 fn main() {
80 }