2014-02-10 06:44:21 -06:00
|
|
|
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
|
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
|
|
|
// Tests that two closures cannot simultaneously have mutable
|
|
|
|
// access to the variable, whether that mutable access be used
|
|
|
|
// for direct assignment or for taking mutable ref. Issue #6801.
|
|
|
|
|
2015-01-07 20:53:58 -06:00
|
|
|
#![feature(box_syntax)]
|
2014-05-05 20:56:44 -05:00
|
|
|
|
2015-02-01 11:44:15 -06:00
|
|
|
fn to_fn_mut<F: FnMut()>(f: F) -> F { f }
|
|
|
|
|
2014-02-10 06:44:21 -06:00
|
|
|
fn a() {
|
2015-01-31 10:23:42 -06:00
|
|
|
let mut x = 3;
|
2015-02-01 11:44:15 -06:00
|
|
|
let c1 = to_fn_mut(|| x = 4);
|
|
|
|
let c2 = to_fn_mut(|| x = 5); //~ ERROR cannot borrow `x` as mutable more than once
|
2014-02-10 06:44:21 -06:00
|
|
|
}
|
|
|
|
|
2015-01-08 04:54:35 -06:00
|
|
|
fn set(x: &mut isize) {
|
2014-02-10 06:44:21 -06:00
|
|
|
*x = 4;
|
|
|
|
}
|
|
|
|
|
|
|
|
fn b() {
|
2015-01-31 10:23:42 -06:00
|
|
|
let mut x = 3;
|
2015-02-01 11:44:15 -06:00
|
|
|
let c1 = to_fn_mut(|| set(&mut x));
|
|
|
|
let c2 = to_fn_mut(|| set(&mut x)); //~ ERROR cannot borrow `x` as mutable more than once
|
2014-02-10 06:44:21 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
fn c() {
|
2015-01-31 10:23:42 -06:00
|
|
|
let mut x = 3;
|
2015-02-01 11:44:15 -06:00
|
|
|
let c1 = to_fn_mut(|| x = 5);
|
|
|
|
let c2 = to_fn_mut(|| set(&mut x)); //~ ERROR cannot borrow `x` as mutable more than once
|
2014-02-10 06:44:21 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
fn d() {
|
2015-01-31 10:23:42 -06:00
|
|
|
let mut x = 3;
|
2015-02-01 11:44:15 -06:00
|
|
|
let c1 = to_fn_mut(|| x = 5);
|
|
|
|
let c2 = to_fn_mut(|| { let _y = to_fn_mut(|| set(&mut x)); }); // (nested closure)
|
2014-02-10 06:44:21 -06:00
|
|
|
//~^ ERROR cannot borrow `x` as mutable more than once
|
|
|
|
}
|
|
|
|
|
|
|
|
fn g() {
|
|
|
|
struct Foo {
|
2015-01-08 04:54:35 -06:00
|
|
|
f: Box<isize>
|
2014-02-10 06:44:21 -06:00
|
|
|
}
|
|
|
|
|
2015-02-17 14:41:32 -06:00
|
|
|
let mut x: Box<_> = box Foo { f: box 3 };
|
2015-02-01 11:44:15 -06:00
|
|
|
let c1 = to_fn_mut(|| set(&mut *x.f));
|
|
|
|
let c2 = to_fn_mut(|| set(&mut *x.f));
|
2014-02-10 06:44:21 -06:00
|
|
|
//~^ ERROR cannot borrow `x` as mutable more than once
|
|
|
|
}
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
}
|