]> git.lizzy.rs Git - rust.git/blob - tests/ui/borrowck/borrowck-closures-mut-and-imm.rs
Rollup merge of #106726 - cmorin6:fix-comment-typos, r=Nilstrieb
[rust.git] / tests / ui / borrowck / borrowck-closures-mut-and-imm.rs
1 // Tests that two closures cannot simultaneously have mutable
2 // and immutable access to the variable. Issue #6801.
3
4 fn get(x: &isize) -> isize {
5     *x
6 }
7
8 fn set(x: &mut isize) {
9     *x = 4;
10 }
11
12
13
14 fn a() {
15     let mut x = 3;
16     let c1 = || x = 4;
17     let c2 = || x * 5;
18     //~^ ERROR cannot borrow `x` as immutable because it is also borrowed as mutable
19     drop(c1);
20 }
21
22 fn b() {
23     let mut x = 3;
24     let c1 = || set(&mut x);
25     let c2 = || get(&x);
26     //~^ ERROR cannot borrow `x` as immutable because it is also borrowed as mutable
27     drop(c1);
28 }
29
30 fn c() {
31     let mut x = 3;
32     let c1 = || set(&mut x);
33     let c2 = || x * 5;
34     //~^ ERROR cannot borrow `x` as immutable because it is also borrowed as mutable
35     drop(c1);
36 }
37
38 fn d() {
39     let mut x = 3;
40     let c2 = || x * 5;
41     x = 5;
42     //~^ ERROR cannot assign to `x` because it is borrowed
43     drop(c2);
44 }
45
46 fn e() {
47     let mut x = 3;
48     let c1 = || get(&x);
49     x = 5;
50     //~^ ERROR cannot assign to `x` because it is borrowed
51     drop(c1);
52 }
53
54 fn f() {
55     let mut x: Box<_> = Box::new(3);
56     let c1 = || get(&*x);
57     *x = 5;
58     //~^ ERROR cannot assign to `*x` because it is borrowed
59     drop(c1);
60 }
61
62 fn g() {
63     struct Foo {
64         f: Box<isize>
65     }
66
67     let mut x: Box<_> = Box::new(Foo { f: Box::new(3) });
68     let c1 = || get(&*x.f);
69     *x.f = 5;
70     //~^ ERROR cannot assign to `*x.f` because it is borrowed
71     drop(c1);
72 }
73
74 fn h() {
75     struct Foo {
76         f: Box<isize>
77     }
78
79     let mut x: Box<_> = Box::new(Foo { f: Box::new(3) });
80     let c1 = || get(&*x.f);
81     let c2 = || *x.f = 5;
82     //~^ ERROR cannot borrow `x` as mutable because it is also borrowed as immutable
83     drop(c1);
84 }
85
86 fn main() {
87 }