2014-02-10 06:44:21 -06:00
|
|
|
// Tests that two closures cannot simultaneously have mutable
|
|
|
|
// and immutable access to the variable. Issue #6801.
|
|
|
|
|
2015-01-08 04:54:35 -06:00
|
|
|
fn get(x: &isize) -> isize {
|
2014-02-10 06:44:21 -06:00
|
|
|
*x
|
|
|
|
}
|
|
|
|
|
2015-01-08 04:54:35 -06:00
|
|
|
fn set(x: &mut isize) {
|
2014-02-10 06:44:21 -06:00
|
|
|
*x = 4;
|
|
|
|
}
|
|
|
|
|
2021-08-24 19:39:40 -05:00
|
|
|
|
|
|
|
|
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 = || x = 4;
|
2019-04-22 02:40:08 -05:00
|
|
|
let c2 = || x * 5;
|
|
|
|
//~^ ERROR cannot borrow `x` as immutable because it is also borrowed as mutable
|
2018-04-09 04:28:00 -05:00
|
|
|
drop(c1);
|
2014-02-10 06:44:21 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
fn b() {
|
2015-01-31 10:23:42 -06:00
|
|
|
let mut x = 3;
|
2015-02-01 11:44:15 -06:00
|
|
|
let c1 = || set(&mut x);
|
2019-04-22 02:40:08 -05:00
|
|
|
let c2 = || get(&x);
|
|
|
|
//~^ ERROR cannot borrow `x` as immutable because it is also borrowed as mutable
|
2018-04-09 04:28:00 -05:00
|
|
|
drop(c1);
|
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 = || set(&mut x);
|
2019-04-22 02:40:08 -05:00
|
|
|
let c2 = || x * 5;
|
|
|
|
//~^ ERROR cannot borrow `x` as immutable because it is also borrowed as mutable
|
2018-04-09 04:28:00 -05:00
|
|
|
drop(c1);
|
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 c2 = || x * 5;
|
2019-04-22 02:40:08 -05:00
|
|
|
x = 5;
|
|
|
|
//~^ ERROR cannot assign to `x` because it is borrowed
|
2018-04-09 04:28:00 -05:00
|
|
|
drop(c2);
|
2014-02-10 06:44:21 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
fn e() {
|
2015-01-31 10:23:42 -06:00
|
|
|
let mut x = 3;
|
2015-02-01 11:44:15 -06:00
|
|
|
let c1 = || get(&x);
|
2019-04-22 02:40:08 -05:00
|
|
|
x = 5;
|
|
|
|
//~^ ERROR cannot assign to `x` because it is borrowed
|
2018-04-09 04:28:00 -05:00
|
|
|
drop(c1);
|
2014-02-10 06:44:21 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
fn f() {
|
2021-08-24 19:39:40 -05:00
|
|
|
let mut x: Box<_> = Box::new(3);
|
2015-02-01 11:44:15 -06:00
|
|
|
let c1 = || get(&*x);
|
2019-04-22 02:40:08 -05:00
|
|
|
*x = 5;
|
|
|
|
//~^ ERROR cannot assign to `*x` because it is borrowed
|
2018-04-09 04:28:00 -05:00
|
|
|
drop(c1);
|
2014-02-10 06:44:21 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
fn g() {
|
|
|
|
struct Foo {
|
2015-01-08 04:54:35 -06:00
|
|
|
f: Box<isize>
|
2014-02-10 06:44:21 -06:00
|
|
|
}
|
|
|
|
|
2021-08-24 19:39:40 -05:00
|
|
|
let mut x: Box<_> = Box::new(Foo { f: Box::new(3) });
|
2015-02-01 11:44:15 -06:00
|
|
|
let c1 = || get(&*x.f);
|
2019-04-22 02:40:08 -05:00
|
|
|
*x.f = 5;
|
|
|
|
//~^ ERROR cannot assign to `*x.f` because it is borrowed
|
2018-04-09 04:28:00 -05:00
|
|
|
drop(c1);
|
2014-02-10 06:44:21 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
fn h() {
|
|
|
|
struct Foo {
|
2015-01-08 04:54:35 -06:00
|
|
|
f: Box<isize>
|
2014-02-10 06:44:21 -06:00
|
|
|
}
|
|
|
|
|
2021-08-24 19:39:40 -05:00
|
|
|
let mut x: Box<_> = Box::new(Foo { f: Box::new(3) });
|
2015-02-01 11:44:15 -06:00
|
|
|
let c1 = || get(&*x.f);
|
2019-04-22 02:40:08 -05:00
|
|
|
let c2 = || *x.f = 5;
|
|
|
|
//~^ ERROR cannot borrow `x` as mutable because it is also borrowed as immutable
|
2018-04-09 04:28:00 -05:00
|
|
|
drop(c1);
|
2014-02-10 06:44:21 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
}
|