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