|
| 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 | +pub struct Foo { |
| 15 | + x: u32 |
| 16 | +} |
| 17 | + |
| 18 | +pub struct Bar(u32); |
| 19 | + |
| 20 | +pub enum Baz { |
| 21 | + X(u32) |
| 22 | +} |
| 23 | + |
| 24 | +union U { |
| 25 | + a: u8, |
| 26 | + b: u64, |
| 27 | +} |
| 28 | + |
| 29 | +impl Foo { |
| 30 | + fn x(&mut self) -> &mut u32 { &mut self.x } |
| 31 | +} |
| 32 | + |
| 33 | +impl Bar { |
| 34 | + fn x(&mut self) -> &mut u32 { &mut self.0 } |
| 35 | +} |
| 36 | + |
| 37 | +impl Baz { |
| 38 | + fn x(&mut self) -> &mut u32 { |
| 39 | + match *self { |
| 40 | + Baz::X(ref mut value) => value |
| 41 | + } |
| 42 | + } |
| 43 | +} |
| 44 | + |
| 45 | +static mut sfoo : Foo = Foo{x: 23 }; |
| 46 | +static mut sbar : Bar = Bar(23); |
| 47 | +static mut stuple : (i32, i32) = (24, 25); |
| 48 | +static mut senum : Baz = Baz::X(26); |
| 49 | +static mut sunion : U = U { a: 0 }; |
| 50 | + |
| 51 | +fn main() { |
| 52 | + // Local and field from struct |
| 53 | + { |
| 54 | + let mut f = Foo { x: 22 }; |
| 55 | + let _x = f.x(); |
| 56 | + f.x; //[ast]~ ERROR cannot use `f.x` because it was mutably borrowed |
| 57 | + //[mir]~^ ERROR cannot use `f.x` because it was mutably borrowed (Ast) |
| 58 | + //[mir]~| ERROR cannot use `f.x` because it was mutably borrowed (Mir) |
| 59 | + } |
| 60 | + // Local and field from tuple-struct |
| 61 | + { |
| 62 | + let mut g = Bar(22); |
| 63 | + let _0 = g.x(); |
| 64 | + g.0; //[ast]~ ERROR cannot use `g.0` because it was mutably borrowed |
| 65 | + //[mir]~^ ERROR cannot use `g.0` because it was mutably borrowed (Ast) |
| 66 | + //[mir]~| ERROR cannot use `g.0` because it was mutably borrowed (Mir) |
| 67 | + } |
| 68 | + // Local and field from tuple |
| 69 | + { |
| 70 | + let mut h = (22, 23); |
| 71 | + let _0 = &mut h.0; |
| 72 | + h.0; //[ast]~ ERROR cannot use `h.0` because it was mutably borrowed |
| 73 | + //[mir]~^ ERROR cannot use `h.0` because it was mutably borrowed (Ast) |
| 74 | + //[mir]~| ERROR cannot use `h.0` because it was mutably borrowed (Mir) |
| 75 | + } |
| 76 | + // Local and field from enum |
| 77 | + { |
| 78 | + let mut e = Baz::X(2); |
| 79 | + let _e0 = e.x(); |
| 80 | + match e { |
| 81 | + Baz::X(value) => value |
| 82 | + //[ast]~^ ERROR cannot use `e.0` because it was mutably borrowed |
| 83 | + //[mir]~^^ ERROR cannot use `e.0` because it was mutably borrowed (Ast) |
| 84 | + //[mir]~| ERROR cannot use `e.0` because it was mutably borrowed (Mir) |
| 85 | + }; |
| 86 | + } |
| 87 | + // Local and field from union |
| 88 | + unsafe { |
| 89 | + let mut u = U { b: 0 }; |
| 90 | + let _ra = &mut u.a; |
| 91 | + u.a; //[ast]~ ERROR cannot use `u.a` because it was mutably borrowed |
| 92 | + //[mir]~^ ERROR cannot use `u.a` because it was mutably borrowed (Ast) |
| 93 | + //[mir]~| ERROR cannot use `u.a` because it was mutably borrowed (Mir) |
| 94 | + } |
| 95 | + // Static and field from struct |
| 96 | + unsafe { |
| 97 | + let _x = sfoo.x(); |
| 98 | + sfoo.x; //[mir]~ ERROR cannot use `sfoo.x` because it was mutably borrowed (Mir) |
| 99 | + } |
| 100 | + // Static and field from tuple-struct |
| 101 | + unsafe { |
| 102 | + let _0 = sbar.x(); |
| 103 | + sbar.0; //[mir]~ ERROR cannot use `sbar.0` because it was mutably borrowed (Mir) |
| 104 | + } |
| 105 | + // Static and field from tuple |
| 106 | + unsafe { |
| 107 | + let _0 = &mut stuple.0; |
| 108 | + stuple.0; //[mir]~ ERROR cannot use `stuple.0` because it was mutably borrowed (Mir) |
| 109 | + } |
| 110 | + // Static and field from enum |
| 111 | + unsafe { |
| 112 | + let _e0 = senum.x(); |
| 113 | + match senum { |
| 114 | + Baz::X(value) => value |
| 115 | + //[mir]~^ ERROR cannot use `senum.0` because it was mutably borrowed (Mir) |
| 116 | + }; |
| 117 | + } |
| 118 | + // Static and field from union |
| 119 | + unsafe { |
| 120 | + let _ra = &mut sunion.a; |
| 121 | + sunion.a; //[mir]~ ERROR cannot use `sunion.a` because it was mutably borrowed (Mir) |
| 122 | + } |
| 123 | + // Deref and field from struct |
| 124 | + { |
| 125 | + let mut f = Box::new(Foo { x: 22 }); |
| 126 | + let _x = f.x(); |
| 127 | + f.x; //[ast]~ ERROR cannot use `f.x` because it was mutably borrowed |
| 128 | + //[mir]~^ ERROR cannot use `f.x` because it was mutably borrowed (Ast) |
| 129 | + //[mir]~| ERROR cannot use `f.x` because it was mutably borrowed (Mir) |
| 130 | + } |
| 131 | + // Deref and field from tuple-struct |
| 132 | + { |
| 133 | + let mut g = Box::new(Bar(22)); |
| 134 | + let _0 = g.x(); |
| 135 | + g.0; //[ast]~ ERROR cannot use `g.0` because it was mutably borrowed |
| 136 | + //[mir]~^ ERROR cannot use `g.0` because it was mutably borrowed (Ast) |
| 137 | + //[mir]~| ERROR cannot use `g.0` because it was mutably borrowed (Mir) |
| 138 | + } |
| 139 | + // Deref and field from tuple |
| 140 | + { |
| 141 | + let mut h = Box::new((22, 23)); |
| 142 | + let _0 = &mut h.0; |
| 143 | + h.0; //[ast]~ ERROR cannot use `h.0` because it was mutably borrowed |
| 144 | + //[mir]~^ ERROR cannot use `h.0` because it was mutably borrowed (Ast) |
| 145 | + //[mir]~| ERROR cannot use `h.0` because it was mutably borrowed (Mir) |
| 146 | + } |
| 147 | + // Deref and field from enum |
| 148 | + { |
| 149 | + let mut e = Box::new(Baz::X(3)); |
| 150 | + let _e0 = e.x(); |
| 151 | + match *e { |
| 152 | + Baz::X(value) => value |
| 153 | + //[ast]~^ ERROR cannot use `e.0` because it was mutably borrowed |
| 154 | + //[mir]~^^ ERROR cannot use `e.0` because it was mutably borrowed (Ast) |
| 155 | + //[mir]~| ERROR cannot use `e.0` because it was mutably borrowed (Mir) |
| 156 | + }; |
| 157 | + } |
| 158 | + // Deref and field from union |
| 159 | + unsafe { |
| 160 | + let mut u = Box::new(U { b: 0 }); |
| 161 | + let _ra = &mut u.a; |
| 162 | + u.a; //[ast]~ ERROR cannot use `u.a` because it was mutably borrowed |
| 163 | + //[mir]~^ ERROR cannot use `u.a` because it was mutably borrowed (Ast) |
| 164 | + //[mir]~| ERROR cannot use `u.a` because it was mutably borrowed (Mir) |
| 165 | + } |
| 166 | +} |
0 commit comments