|
| 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 emit-end-regions -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 (Ast) [E0596] |
| 24 | + //[mir]~| ERROR (Mir) [E0596] |
| 25 | + let _y2 = &mut x_mut; // No error |
| 26 | + } |
| 27 | + |
| 28 | + { // borrow of static |
| 29 | + let _y1 = &mut static_x; //[ast]~ ERROR [E0596] |
| 30 | + //[mir]~^ ERROR (Ast) [E0596] |
| 31 | + //[mir]~| ERROR (Mir) [E0596] |
| 32 | + unsafe { let _y2 = &mut static_x_mut; } // No error |
| 33 | + } |
| 34 | + |
| 35 | + { // borrow of deref to box |
| 36 | + let box_x = Box::new(1); |
| 37 | + let mut box_x_mut = Box::new(1); |
| 38 | + |
| 39 | + let _y1 = &mut *box_x; //[ast]~ ERROR [E0596] |
| 40 | + //[mir]~^ ERROR (Ast) [E0596] |
| 41 | + //[mir]~| ERROR (Mir) [E0596] |
| 42 | + let _y2 = &mut *box_x_mut; // No error |
| 43 | + } |
| 44 | + |
| 45 | + { // borrow of deref to reference |
| 46 | + let ref_x = &x; |
| 47 | + let ref_x_mut = &mut x_mut; |
| 48 | + |
| 49 | + let _y1 = &mut *ref_x; //[ast]~ ERROR [E0596] |
| 50 | + //[mir]~^ ERROR (Ast) [E0596] |
| 51 | + //[mir]~| ERROR (Mir) [E0596] |
| 52 | + let _y2 = &mut *ref_x_mut; // No error |
| 53 | + } |
| 54 | + |
| 55 | + { // borrow of deref to pointer |
| 56 | + let ptr_x : *const _ = &x; |
| 57 | + let ptr_mut_x : *mut _ = &mut x_mut; |
| 58 | + |
| 59 | + unsafe { |
| 60 | + let _y1 = &mut *ptr_x; //[ast]~ ERROR [E0596] |
| 61 | + //[mir]~^ ERROR (Ast) [E0596] |
| 62 | + //[mir]~| ERROR (Mir) [E0596] |
| 63 | + let _y2 = &mut *ptr_mut_x; // No error |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + { // borrowing mutably through an immutable reference |
| 68 | + struct Foo<'a> { f: &'a mut i32, g: &'a i32 }; |
| 69 | + let mut foo = Foo { f: &mut x_mut, g: &x }; |
| 70 | + let foo_ref = &foo; |
| 71 | + let _y = &mut *foo_ref.f; //[ast]~ ERROR [E0389] |
| 72 | + //[mir]~^ ERROR (Ast) [E0389] |
| 73 | + //[mir]~| ERROR (Mir) [E0596] |
| 74 | + // FIXME: Wrong error in MIR |
| 75 | + } |
| 76 | +} |
0 commit comments