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