]> git.lizzy.rs Git - rust.git/blob - src/test/ui/borrowck/borrowck-closures-mut-and-imm.rs
Rollup merge of #57132 - daxpedda:master, r=steveklabnik
[rust.git] / src / test / 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 // ignore-tidy-linelength
5 // revisions: ast mir
6 //[mir]compile-flags: -Z borrowck=mir
7
8 #![feature(box_syntax)]
9
10 fn get(x: &isize) -> isize {
11     *x
12 }
13
14 fn set(x: &mut isize) {
15     *x = 4;
16 }
17
18 fn a() {
19     let mut x = 3;
20     let c1 = || x = 4;
21     let c2 = || x * 5; //[ast]~ ERROR cannot borrow `x`
22     //[mir]~^ ERROR cannot borrow `x` as immutable because it is also borrowed as mutable
23     drop(c1);
24 }
25
26 fn b() {
27     let mut x = 3;
28     let c1 = || set(&mut x);
29     let c2 = || get(&x); //[ast]~ ERROR cannot borrow `x`
30                          //[mir]~^ ERROR cannot borrow `x` as immutable because it is also borrowed as mutable
31     drop(c1);
32 }
33
34 fn c() {
35     let mut x = 3;
36     let c1 = || set(&mut x);
37     let c2 = || x * 5; //[ast]~ ERROR cannot borrow `x`
38                        //[mir]~^ ERROR cannot borrow `x` as immutable because it is also borrowed as mutable
39     drop(c1);
40 }
41
42 fn d() {
43     let mut x = 3;
44     let c2 = || x * 5;
45     x = 5; //[ast]~ ERROR cannot assign
46            //[mir]~^ ERROR cannot assign to `x` because it is borrowed
47     drop(c2);
48 }
49
50 fn e() {
51     let mut x = 3;
52     let c1 = || get(&x);
53     x = 5; //[ast]~ ERROR cannot assign
54            //[mir]~^ ERROR cannot assign to `x` because it is borrowed
55     drop(c1);
56 }
57
58 fn f() {
59     let mut x: Box<_> = box 3;
60     let c1 = || get(&*x);
61     *x = 5; //[ast]~ ERROR cannot assign to `*x`
62             //[mir]~^ ERROR cannot assign to `*x` because it is borrowed
63     drop(c1);
64 }
65
66 fn g() {
67     struct Foo {
68         f: Box<isize>
69     }
70
71     let mut x: Box<_> = box Foo { f: box 3 };
72     let c1 = || get(&*x.f);
73     *x.f = 5; //[ast]~ ERROR cannot assign to `*x.f`
74               //[mir]~^ ERROR cannot assign to `*x.f` because it is borrowed
75     drop(c1);
76 }
77
78 fn h() {
79     struct Foo {
80         f: Box<isize>
81     }
82
83     let mut x: Box<_> = box Foo { f: box 3 };
84     let c1 = || get(&*x.f);
85     let c2 = || *x.f = 5; //[ast]~ ERROR cannot borrow `x` as mutable
86                           //[mir]~^ ERROR cannot borrow `x` as mutable because it is also borrowed as immutable
87     drop(c1);
88 }
89
90 fn main() {
91 }