|
| 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 | +// This tests that we can't modify Box<&mut T> contents while they |
| 12 | +// are borrowed. |
| 13 | + |
| 14 | +struct A { a: int } |
| 15 | +struct B<'a> { a: Box<&'a mut int> } |
| 16 | + |
| 17 | +fn borrow_in_var_from_var() { |
| 18 | + let mut x: int = 1; |
| 19 | + let y = box &mut x; |
| 20 | + let p = &y; |
| 21 | + let q = &***p; |
| 22 | + **y = 2; //~ ERROR cannot assign to `**y` because it is borrowed |
| 23 | + drop(p); |
| 24 | + drop(q); |
| 25 | +} |
| 26 | + |
| 27 | +fn borrow_in_var_from_field() { |
| 28 | + let mut x = A { a: 1 }; |
| 29 | + let y = box &mut x.a; |
| 30 | + let p = &y; |
| 31 | + let q = &***p; |
| 32 | + **y = 2; //~ ERROR cannot assign to `**y` because it is borrowed |
| 33 | + drop(p); |
| 34 | + drop(q); |
| 35 | +} |
| 36 | + |
| 37 | +fn borrow_in_field_from_var() { |
| 38 | + let mut x: int = 1; |
| 39 | + let y = B { a: box &mut x }; |
| 40 | + let p = &y.a; |
| 41 | + let q = &***p; |
| 42 | + **y.a = 2; //~ ERROR cannot assign to `**y.a` because it is borrowed |
| 43 | + drop(p); |
| 44 | + drop(q); |
| 45 | +} |
| 46 | + |
| 47 | +fn borrow_in_field_from_field() { |
| 48 | + let mut x = A { a: 1 }; |
| 49 | + let y = B { a: box &mut x.a }; |
| 50 | + let p = &y.a; |
| 51 | + let q = &***p; |
| 52 | + **y.a = 2; //~ ERROR cannot assign to `**y.a` because it is borrowed |
| 53 | + drop(p); |
| 54 | + drop(q); |
| 55 | +} |
| 56 | + |
| 57 | +fn main() { |
| 58 | + borrow_in_var_from_var(); |
| 59 | + borrow_in_var_from_field(); |
| 60 | + borrow_in_field_from_var(); |
| 61 | + borrow_in_field_from_field(); |
| 62 | +} |
| 63 | + |
0 commit comments