|
| 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: Box<int> } |
| 12 | + |
| 13 | +fn borrow<T>(_: &T) { } |
| 14 | + |
| 15 | +fn use_after_move() { |
| 16 | + let x = A { a: 1, b: box 2 }; |
| 17 | + drop(x.b); |
| 18 | + drop(*x.b); //~ ERROR use of partially moved value: `*x.b` |
| 19 | +} |
| 20 | + |
| 21 | +fn use_after_fu_move() { |
| 22 | + let x = A { a: 1, b: box 2 }; |
| 23 | + let y = A { a: 3, .. x }; |
| 24 | + drop(*x.b); //~ ERROR use of partially moved value: `*x.b` |
| 25 | +} |
| 26 | + |
| 27 | +fn borrow_after_move() { |
| 28 | + let x = A { a: 1, b: box 2 }; |
| 29 | + drop(x.b); |
| 30 | + borrow(&x.b); //~ ERROR use of moved value: `x.b` |
| 31 | +} |
| 32 | + |
| 33 | +fn borrow_after_fu_move() { |
| 34 | + let x = A { a: 1, b: box 2 }; |
| 35 | + let _y = A { a: 3, .. x }; |
| 36 | + borrow(&x.b); //~ ERROR use of moved value: `x.b` |
| 37 | +} |
| 38 | + |
| 39 | +fn move_after_borrow() { |
| 40 | + let x = A { a: 1, b: box 2 }; |
| 41 | + let y = &x.b; |
| 42 | + drop(x.b); //~ ERROR cannot move out of `x.b` because it is borrowed |
| 43 | + borrow(&*y); |
| 44 | +} |
| 45 | + |
| 46 | +fn fu_move_after_borrow() { |
| 47 | + let x = A { a: 1, b: box 2 }; |
| 48 | + let y = &x.b; |
| 49 | + let _z = A { a: 3, .. x }; //~ ERROR cannot move out of `x.b` because it is borrowed |
| 50 | + borrow(&*y); |
| 51 | +} |
| 52 | + |
| 53 | +fn mut_borrow_after_mut_borrow() { |
| 54 | + let mut x = A { a: 1, b: box 2 }; |
| 55 | + let y = &mut x.a; |
| 56 | + let z = &mut x.a; //~ ERROR cannot borrow `x.a` as mutable more than once at a time |
| 57 | + drop(*y); |
| 58 | + drop(*z); |
| 59 | +} |
| 60 | + |
| 61 | +fn move_after_move() { |
| 62 | + let x = A { a: 1, b: box 2 }; |
| 63 | + drop(x.b); |
| 64 | + drop(x.b); //~ ERROR use of moved value: `x.b` |
| 65 | +} |
| 66 | + |
| 67 | +fn move_after_fu_move() { |
| 68 | + let x = A { a: 1, b: box 2 }; |
| 69 | + let _y = A { a: 3, .. x }; |
| 70 | + drop(x.b); //~ ERROR use of moved value: `x.b` |
| 71 | +} |
| 72 | + |
| 73 | +fn fu_move_after_move() { |
| 74 | + let x = A { a: 1, b: box 2 }; |
| 75 | + drop(x.b); |
| 76 | + let _z = A { a: 3, .. x }; //~ ERROR use of moved value: `x.b` |
| 77 | +} |
| 78 | + |
| 79 | +fn fu_move_after_fu_move() { |
| 80 | + let x = A { a: 1, b: box 2 }; |
| 81 | + let _y = A { a: 3, .. x }; |
| 82 | + let _z = A { a: 4, .. x }; //~ ERROR use of moved value: `x.b` |
| 83 | +} |
| 84 | + |
| 85 | +// The following functions aren't yet accepted, but they should be. |
| 86 | + |
| 87 | +fn use_after_field_assign_after_uninit() { |
| 88 | + let mut x: A; |
| 89 | + x.a = 1; |
| 90 | + drop(x.a); //~ ERROR use of possibly uninitialized variable: `x.a` |
| 91 | +} |
| 92 | + |
| 93 | +fn borrow_after_field_assign_after_uninit() { |
| 94 | + let mut x: A; |
| 95 | + x.a = 1; |
| 96 | + borrow(&x.a); //~ ERROR use of possibly uninitialized variable: `x.a` |
| 97 | +} |
| 98 | + |
| 99 | +fn move_after_field_assign_after_uninit() { |
| 100 | + let mut x: A; |
| 101 | + x.b = box 1; |
| 102 | + drop(x.b); //~ ERROR use of possibly uninitialized variable: `x.b` |
| 103 | +} |
| 104 | + |
| 105 | +fn main() { |
| 106 | + use_after_move(); |
| 107 | + use_after_fu_move(); |
| 108 | + |
| 109 | + borrow_after_move(); |
| 110 | + borrow_after_fu_move(); |
| 111 | + move_after_borrow(); |
| 112 | + fu_move_after_borrow(); |
| 113 | + mut_borrow_after_mut_borrow(); |
| 114 | + |
| 115 | + move_after_move(); |
| 116 | + move_after_fu_move(); |
| 117 | + fu_move_after_move(); |
| 118 | + fu_move_after_fu_move(); |
| 119 | + |
| 120 | + use_after_field_assign_after_uninit(); |
| 121 | + borrow_after_field_assign_after_uninit(); |
| 122 | + move_after_field_assign_after_uninit(); |
| 123 | +} |
| 124 | + |
0 commit comments