]> git.lizzy.rs Git - rust.git/blob - src/test/ui/borrowck/borrowck-closures-two-mut.rs
Fix comments
[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 emit-end-regions -Z borrowck-mir
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 }
26
27 fn set(x: &mut isize) {
28     *x = 4;
29 }
30
31 fn b() {
32     let mut x = 3;
33     let c1 = to_fn_mut(|| set(&mut x));
34     let c2 = to_fn_mut(|| set(&mut x)); //~ ERROR cannot borrow `x` as mutable more than once
35 }
36
37 fn c() {
38     let mut x = 3;
39     let c1 = to_fn_mut(|| x = 5);
40     let c2 = to_fn_mut(|| set(&mut x)); //~ ERROR cannot borrow `x` as mutable more than once
41 }
42
43 fn d() {
44     let mut x = 3;
45     let c1 = to_fn_mut(|| x = 5);
46     let c2 = to_fn_mut(|| { let _y = to_fn_mut(|| set(&mut x)); }); // (nested closure)
47     //~^ ERROR cannot borrow `x` as mutable more than once
48 }
49
50 fn g() {
51     struct Foo {
52         f: Box<isize>
53     }
54
55     let mut x: Box<_> = box Foo { f: box 3 };
56     let c1 = to_fn_mut(|| set(&mut *x.f));
57     let c2 = to_fn_mut(|| set(&mut *x.f));
58     //~^ ERROR cannot borrow `x` as mutable more than once
59 }
60
61 fn main() {
62 }