]> git.lizzy.rs Git - rust.git/blob - src/test/ui/borrowck/borrowck-access-permissions.rs
Auto merge of #54720 - davidtwco:issue-51191, r=nikomatsakis
[rust.git] / src / test / ui / borrowck / borrowck-access-permissions.rs
1 // Copyright 2016 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10
11 // revisions: ast mir
12 //[mir]compile-flags: -Z borrowck=mir
13
14 static static_x : i32 = 1;
15 static mut static_x_mut : i32 = 1;
16
17 fn main() {
18     let x = 1;
19     let mut x_mut = 1;
20
21     { // borrow of local
22         let _y1 = &mut x; //[ast]~ ERROR [E0596]
23                           //[mir]~^ ERROR [E0596]
24         let _y2 = &mut x_mut; // No error
25     }
26
27     { // borrow of static
28         let _y1 = &mut static_x; //[ast]~ ERROR [E0596]
29                                  //[mir]~^ ERROR [E0596]
30         unsafe { let _y2 = &mut static_x_mut; } // No error
31     }
32
33     { // borrow of deref to box
34         let box_x = Box::new(1);
35         let mut box_x_mut = Box::new(1);
36
37         let _y1 = &mut *box_x; //[ast]~ ERROR [E0596]
38                                //[mir]~^ ERROR [E0596]
39         let _y2 = &mut *box_x_mut; // No error
40     }
41
42     { // borrow of deref to reference
43         let ref_x = &x;
44         let ref_x_mut = &mut x_mut;
45
46         let _y1 = &mut *ref_x; //[ast]~ ERROR [E0596]
47                                //[mir]~^ ERROR [E0596]
48         let _y2 = &mut *ref_x_mut; // No error
49     }
50
51     { // borrow of deref to pointer
52         let ptr_x : *const _ = &x;
53         let ptr_mut_x : *mut _ = &mut x_mut;
54
55         unsafe {
56             let _y1 = &mut *ptr_x; //[ast]~ ERROR [E0596]
57                                    //[mir]~^ ERROR [E0596]
58             let _y2 = &mut *ptr_mut_x; // No error
59         }
60     }
61
62     { // borrowing mutably through an immutable reference
63         struct Foo<'a> { f: &'a mut i32, g: &'a i32 };
64         let mut foo = Foo { f: &mut x_mut, g: &x };
65         let foo_ref = &foo;
66         let _y = &mut *foo_ref.f; //[ast]~ ERROR [E0389]
67                                   //[mir]~^ ERROR [E0596]
68                                   // FIXME: Wrong error in MIR
69     }
70 }