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