]> git.lizzy.rs Git - rust.git/blob - src/test/compile-fail/borrowck-closures-two-mut.rs
Rollup merge of #21964 - semarie:openbsd-env, r=alexcrichton
[rust.git] / src / test / compile-fail / borrowck-closures-two-mut.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 // access to the variable, whether that mutable access be used
13 // for direct assignment or for taking mutable ref. Issue #6801.
14
15 #![feature(box_syntax)]
16
17 fn to_fn_mut<F: FnMut()>(f: F) -> F { f }
18
19 fn a() {
20     let mut x = 3;
21     let c1 = to_fn_mut(|| x = 4);
22     let c2 = to_fn_mut(|| x = 5); //~ ERROR cannot borrow `x` as mutable more than once
23 }
24
25 fn set(x: &mut isize) {
26     *x = 4;
27 }
28
29 fn b() {
30     let mut x = 3;
31     let c1 = to_fn_mut(|| set(&mut x));
32     let c2 = to_fn_mut(|| set(&mut x)); //~ ERROR cannot borrow `x` as mutable more than once
33 }
34
35 fn c() {
36     let mut x = 3;
37     let c1 = to_fn_mut(|| x = 5);
38     let c2 = to_fn_mut(|| set(&mut x)); //~ ERROR cannot borrow `x` as mutable more than once
39 }
40
41 fn d() {
42     let mut x = 3;
43     let c1 = to_fn_mut(|| x = 5);
44     let c2 = to_fn_mut(|| { let _y = to_fn_mut(|| set(&mut x)); }); // (nested closure)
45     //~^ ERROR cannot borrow `x` as mutable more than once
46 }
47
48 fn g() {
49     struct Foo {
50         f: Box<isize>
51     }
52
53     let mut x = box Foo { f: box 3 };
54     let c1 = to_fn_mut(|| set(&mut *x.f));
55     let c2 = to_fn_mut(|| set(&mut *x.f));
56     //~^ ERROR cannot borrow `x` as mutable more than once
57 }
58
59 fn main() {
60 }