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.
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.
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.
15 // compile-flags: -Z borrowck=compare
17 #![feature(box_syntax)]
19 fn to_fn_mut<F: FnMut()>(f: F) -> F { f }
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
29 fn set(x: &mut isize) {
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
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
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
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