]> git.lizzy.rs Git - rust.git/blob - tests/ui/borrowck/borrowck-access-permissions.rs
Rollup merge of #106726 - cmorin6:fix-comment-typos, r=Nilstrieb
[rust.git] / tests / ui / borrowck / borrowck-access-permissions.rs
1 static static_x : i32 = 1;
2 static mut static_x_mut : i32 = 1;
3
4 fn main() {
5     let x = 1;
6     let mut x_mut = 1;
7
8     { // borrow of local
9         let _y1 = &mut x; //~ ERROR [E0596]
10         let _y2 = &mut x_mut; // No error
11     }
12
13     { // borrow of static
14         let _y1 = &mut static_x; //~ ERROR [E0596]
15         unsafe { let _y2 = &mut static_x_mut; } // No error
16     }
17
18     { // borrow of deref to box
19         let box_x = Box::new(1);
20         let mut box_x_mut = Box::new(1);
21
22         let _y1 = &mut *box_x; //~ ERROR [E0596]
23         let _y2 = &mut *box_x_mut; // No error
24     }
25
26     { // borrow of deref to reference
27         let ref_x = &x;
28         let ref_x_mut = &mut x_mut;
29
30         let _y1 = &mut *ref_x; //~ ERROR [E0596]
31         let _y2 = &mut *ref_x_mut; // No error
32     }
33
34     { // borrow of deref to pointer
35         let ptr_x : *const _ = &x;
36         let ptr_mut_x : *mut _ = &mut x_mut;
37
38         unsafe {
39             let _y1 = &mut *ptr_x; //~ ERROR [E0596]
40             let _y2 = &mut *ptr_mut_x; // No error
41         }
42     }
43
44     { // borrowing mutably through an immutable reference
45         struct Foo<'a> { f: &'a mut i32, g: &'a i32 };
46         let mut foo = Foo { f: &mut x_mut, g: &x };
47         let foo_ref = &foo;
48         let _y = &mut *foo_ref.f; //~ ERROR [E0596]
49     }
50 }