]> git.lizzy.rs Git - rust.git/blob - src/test/ui/borrowck/borrowck-closures-two-mut.rs
Auto merge of #53002 - QuietMisdreavus:brother-may-i-have-some-loops, r=pnkfelix
[rust.git] / src / test / ui / borrowck / borrowck-closures-two-mut.rs
1 // Copyright 2017 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 // compile-flags: -Z borrowck=compare
16
17 #![feature(box_syntax)]
18
19 fn to_fn_mut<F: FnMut()>(f: F) -> F { f }
20
21 fn a() {
22     let mut x = 3;
23     let c1 = to_fn_mut(|| x = 4);
24     let c2 = to_fn_mut(|| x = 5); //~ ERROR cannot borrow `x` as mutable more than once
25     //~| ERROR cannot borrow `x` as mutable more than once
26     drop((c1, c2));
27 }
28
29 fn set(x: &mut isize) {
30     *x = 4;
31 }
32
33 fn b() {
34     let mut x = 3;
35     let c1 = to_fn_mut(|| set(&mut x));
36     let c2 = to_fn_mut(|| set(&mut x)); //~ ERROR cannot borrow `x` as mutable more than once
37     //~| ERROR cannot borrow `x` as mutable more than once
38     drop((c1, c2));
39 }
40
41 fn c() {
42     let mut x = 3;
43     let c1 = to_fn_mut(|| x = 5);
44     let c2 = to_fn_mut(|| set(&mut x)); //~ ERROR cannot borrow `x` as mutable more than once
45     //~| ERROR cannot borrow `x` as mutable more than once
46     drop((c1, c2));
47 }
48
49 fn d() {
50     let mut x = 3;
51     let c1 = to_fn_mut(|| x = 5);
52     let c2 = to_fn_mut(|| { let _y = to_fn_mut(|| set(&mut x)); }); // (nested closure)
53     //~^ ERROR cannot borrow `x` as mutable more than once
54     //~| ERROR cannot borrow `x` as mutable more than once
55     drop((c1, c2));
56 }
57
58 fn g() {
59     struct Foo {
60         f: Box<isize>
61     }
62
63     let mut x: Box<_> = box Foo { f: box 3 };
64     let c1 = to_fn_mut(|| set(&mut *x.f));
65     let c2 = to_fn_mut(|| set(&mut *x.f));
66     //~^ ERROR cannot borrow `x` as mutable more than once
67     //~| ERROR cannot borrow `x` as mutable more than once
68     drop((c1, c2));
69 }
70
71 fn main() {
72 }