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.
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 emit-end-regions -Z borrowck-mir
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
27 fn set(x: &mut isize) {
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
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
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
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