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.
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.
12 //[mir]compile-flags: -Z borrowck=mir
14 static static_x : i32 = 1;
15 static mut static_x_mut : i32 = 1;
22 let _y1 = &mut x; //[ast]~ ERROR [E0596]
23 //[mir]~^ ERROR [E0596]
24 let _y2 = &mut x_mut; // No error
28 let _y1 = &mut static_x; //[ast]~ ERROR [E0596]
29 //[mir]~^ ERROR [E0596]
30 unsafe { let _y2 = &mut static_x_mut; } // No error
33 { // borrow of deref to box
34 let box_x = Box::new(1);
35 let mut box_x_mut = Box::new(1);
37 let _y1 = &mut *box_x; //[ast]~ ERROR [E0596]
38 //[mir]~^ ERROR [E0596]
39 let _y2 = &mut *box_x_mut; // No error
42 { // borrow of deref to reference
44 let ref_x_mut = &mut x_mut;
46 let _y1 = &mut *ref_x; //[ast]~ ERROR [E0596]
47 //[mir]~^ ERROR [E0596]
48 let _y2 = &mut *ref_x_mut; // No error
51 { // borrow of deref to pointer
52 let ptr_x : *const _ = &x;
53 let ptr_mut_x : *mut _ = &mut x_mut;
56 let _y1 = &mut *ptr_x; //[ast]~ ERROR [E0596]
57 //[mir]~^ ERROR [E0596]
58 let _y2 = &mut *ptr_mut_x; // No error
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 };
66 let _y = &mut *foo_ref.f; //[ast]~ ERROR [E0389]
67 //[mir]~^ ERROR [E0596]
68 // FIXME: Wrong error in MIR