|
| 1 | +// Copyright 2014 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 | +struct A { a: int, b: int } |
| 12 | +struct B { a: int, b: Box<int> } |
| 13 | + |
| 14 | +fn var_copy_after_var_borrow() { |
| 15 | + let mut x: int = 1; |
| 16 | + let p = &mut x; |
| 17 | + drop(x); //~ ERROR cannot use `x` because it was mutably borrowed |
| 18 | + *p = 2; |
| 19 | +} |
| 20 | + |
| 21 | +fn var_copy_after_field_borrow() { |
| 22 | + let mut x = A { a: 1, b: 2 }; |
| 23 | + let p = &mut x.a; |
| 24 | + drop(x); //~ ERROR cannot use `x` because it was mutably borrowed |
| 25 | + *p = 3; |
| 26 | +} |
| 27 | + |
| 28 | +fn field_copy_after_var_borrow() { |
| 29 | + let mut x = A { a: 1, b: 2 }; |
| 30 | + let p = &mut x; |
| 31 | + drop(x.a); //~ ERROR cannot use `x.a` because it was mutably borrowed |
| 32 | + p.a = 3; |
| 33 | +} |
| 34 | + |
| 35 | +fn field_copy_after_field_borrow() { |
| 36 | + let mut x = A { a: 1, b: 2 }; |
| 37 | + let p = &mut x.a; |
| 38 | + drop(x.a); //~ ERROR cannot use `x.a` because it was mutably borrowed |
| 39 | + *p = 3; |
| 40 | +} |
| 41 | + |
| 42 | +fn fu_field_copy_after_var_borrow() { |
| 43 | + let mut x = A { a: 1, b: 2 }; |
| 44 | + let p = &mut x; |
| 45 | + let y = A { b: 3, .. x }; //~ ERROR cannot use `x.a` because it was mutably borrowed |
| 46 | + drop(y); |
| 47 | + p.a = 4; |
| 48 | +} |
| 49 | + |
| 50 | +fn fu_field_copy_after_field_borrow() { |
| 51 | + let mut x = A { a: 1, b: 2 }; |
| 52 | + let p = &mut x.a; |
| 53 | + let y = A { b: 3, .. x }; //~ ERROR cannot use `x.a` because it was mutably borrowed |
| 54 | + drop(y); |
| 55 | + *p = 4; |
| 56 | +} |
| 57 | + |
| 58 | +fn var_deref_after_var_borrow() { |
| 59 | + let mut x: Box<int> = box 1; |
| 60 | + let p = &mut x; |
| 61 | + drop(*x); //~ ERROR cannot use `*x` because it was mutably borrowed |
| 62 | + **p = 2; |
| 63 | +} |
| 64 | + |
| 65 | +fn field_deref_after_var_borrow() { |
| 66 | + let mut x = B { a: 1, b: box 2 }; |
| 67 | + let p = &mut x; |
| 68 | + drop(*x.b); //~ ERROR cannot use `*x.b` because it was mutably borrowed |
| 69 | + p.a = 3; |
| 70 | +} |
| 71 | + |
| 72 | +fn field_deref_after_field_borrow() { |
| 73 | + let mut x = B { a: 1, b: box 2 }; |
| 74 | + let p = &mut x.b; |
| 75 | + drop(*x.b); //~ ERROR cannot use `*x.b` because it was mutably borrowed |
| 76 | + **p = 3; |
| 77 | +} |
| 78 | + |
| 79 | +fn main() { |
| 80 | + var_copy_after_var_borrow(); |
| 81 | + var_copy_after_field_borrow(); |
| 82 | + |
| 83 | + field_copy_after_var_borrow(); |
| 84 | + field_copy_after_field_borrow(); |
| 85 | + |
| 86 | + fu_field_copy_after_var_borrow(); |
| 87 | + fu_field_copy_after_field_borrow(); |
| 88 | + |
| 89 | + var_deref_after_var_borrow(); |
| 90 | + field_deref_after_var_borrow(); |
| 91 | + field_deref_after_field_borrow(); |
| 92 | +} |
| 93 | + |
0 commit comments