]> git.lizzy.rs Git - rust.git/blob - tests/ui/borrowck/borrowck-closures-two-mut-fail.rs
Rollup merge of #106726 - cmorin6:fix-comment-typos, r=Nilstrieb
[rust.git] / tests / ui / borrowck / borrowck-closures-two-mut-fail.rs
1 // Tests that two closures cannot simultaneously have mutable
2 // access to the variable, whether that mutable access be used
3 // for direct assignment or for taking mutable ref. Issue #6801.
4
5
6
7
8
9
10
11 fn to_fn_mut<F: FnMut()>(f: F) -> F { f }
12
13 fn a() {
14     let mut x = 3;
15     let c1 = to_fn_mut(|| x = 4);
16     let c2 = to_fn_mut(|| x = 5); //~ ERROR cannot borrow `x` as mutable more than once
17     c1;
18 }
19
20 fn set(x: &mut isize) {
21     *x = 4;
22 }
23
24 fn b() {
25     let mut x = 3;
26     let c1 = to_fn_mut(|| set(&mut x));
27     let c2 = to_fn_mut(|| set(&mut x)); //~ ERROR cannot borrow `x` as mutable more than once
28     c1;
29 }
30
31 fn c() {
32     let mut x = 3;
33     let c1 = to_fn_mut(|| x = 5);
34     let c2 = to_fn_mut(|| set(&mut x)); //~ ERROR cannot borrow `x` as mutable more than once
35     c1;
36 }
37
38 fn d() {
39     let mut x = 3;
40     let c1 = to_fn_mut(|| x = 5);
41     let c2 = to_fn_mut(|| { let _y = to_fn_mut(|| set(&mut x)); }); // (nested closure)
42     //~^ ERROR cannot borrow `x` as mutable more than once
43     c1;
44 }
45
46 fn g() {
47     struct Foo {
48         f: Box<isize>
49     }
50
51     let mut x: Box<_> = Box::new(Foo { f: Box::new(3) });
52     let c1 = to_fn_mut(|| set(&mut *x.f));
53     let c2 = to_fn_mut(|| set(&mut *x.f));
54     //~^ ERROR cannot borrow `x` as mutable more than once
55     c1;
56 }
57
58 fn main() {
59 }