|
9 | 9 | // except according to those terms.
|
10 | 10 |
|
11 | 11 | // This tests that we can't modify Box<&mut T> contents while they
|
12 |
| -// are borrowed. |
| 12 | +// are borrowed (#14498). |
| 13 | +// |
| 14 | +// Also includes tests of the errors reported when the Box in question |
| 15 | +// is immutable (#14270). |
13 | 16 |
|
14 | 17 | #![feature(box_syntax)]
|
15 | 18 |
|
16 | 19 | struct A { a: isize }
|
17 | 20 | struct B<'a> { a: Box<&'a mut isize> }
|
18 | 21 |
|
| 22 | +fn indirect_write_to_imm_box() { |
| 23 | + let mut x: isize = 1; |
| 24 | + let y: Box<_> = box &mut x; |
| 25 | + let p = &y; |
| 26 | + ***p = 2; //~ ERROR cannot assign to data in an immutable container |
| 27 | + drop(p); |
| 28 | +} |
| 29 | + |
19 | 30 | fn borrow_in_var_from_var() {
|
| 31 | + let mut x: isize = 1; |
| 32 | + let mut y: Box<_> = box &mut x; |
| 33 | + let p = &y; |
| 34 | + let q = &***p; |
| 35 | + **y = 2; //~ ERROR cannot assign to `**y` because it is borrowed |
| 36 | + drop(p); |
| 37 | + drop(q); |
| 38 | +} |
| 39 | + |
| 40 | +fn borrow_in_var_from_var_via_imm_box() { |
20 | 41 | let mut x: isize = 1;
|
21 | 42 | let y: Box<_> = box &mut x;
|
22 | 43 | let p = &y;
|
23 | 44 | let q = &***p;
|
24 | 45 | **y = 2; //~ ERROR cannot assign to `**y` because it is borrowed
|
| 46 | + //~^ ERROR cannot assign to data in an immutable container |
25 | 47 | drop(p);
|
26 | 48 | drop(q);
|
27 | 49 | }
|
28 | 50 |
|
29 | 51 | fn borrow_in_var_from_field() {
|
| 52 | + let mut x = A { a: 1 }; |
| 53 | + let mut y: Box<_> = box &mut x.a; |
| 54 | + let p = &y; |
| 55 | + let q = &***p; |
| 56 | + **y = 2; //~ ERROR cannot assign to `**y` because it is borrowed |
| 57 | + drop(p); |
| 58 | + drop(q); |
| 59 | +} |
| 60 | + |
| 61 | +fn borrow_in_var_from_field_via_imm_box() { |
30 | 62 | let mut x = A { a: 1 };
|
31 | 63 | let y: Box<_> = box &mut x.a;
|
32 | 64 | let p = &y;
|
33 | 65 | let q = &***p;
|
34 | 66 | **y = 2; //~ ERROR cannot assign to `**y` because it is borrowed
|
| 67 | + //~^ ERROR cannot assign to data in an immutable container |
35 | 68 | drop(p);
|
36 | 69 | drop(q);
|
37 | 70 | }
|
38 | 71 |
|
39 | 72 | fn borrow_in_field_from_var() {
|
| 73 | + let mut x: isize = 1; |
| 74 | + let mut y = B { a: box &mut x }; |
| 75 | + let p = &y.a; |
| 76 | + let q = &***p; |
| 77 | + **y.a = 2; //~ ERROR cannot assign to `**y.a` because it is borrowed |
| 78 | + drop(p); |
| 79 | + drop(q); |
| 80 | +} |
| 81 | + |
| 82 | +fn borrow_in_field_from_var_via_imm_box() { |
40 | 83 | let mut x: isize = 1;
|
41 | 84 | let y = B { a: box &mut x };
|
42 | 85 | let p = &y.a;
|
43 | 86 | let q = &***p;
|
44 | 87 | **y.a = 2; //~ ERROR cannot assign to `**y.a` because it is borrowed
|
| 88 | + //~^ ERROR cannot assign to data in an immutable container |
45 | 89 | drop(p);
|
46 | 90 | drop(q);
|
47 | 91 | }
|
48 | 92 |
|
49 | 93 | fn borrow_in_field_from_field() {
|
| 94 | + let mut x = A { a: 1 }; |
| 95 | + let mut y = B { a: box &mut x.a }; |
| 96 | + let p = &y.a; |
| 97 | + let q = &***p; |
| 98 | + **y.a = 2; //~ ERROR cannot assign to `**y.a` because it is borrowed |
| 99 | + drop(p); |
| 100 | + drop(q); |
| 101 | +} |
| 102 | + |
| 103 | +fn borrow_in_field_from_field_via_imm_box() { |
50 | 104 | let mut x = A { a: 1 };
|
51 | 105 | let y = B { a: box &mut x.a };
|
52 | 106 | let p = &y.a;
|
53 | 107 | let q = &***p;
|
54 | 108 | **y.a = 2; //~ ERROR cannot assign to `**y.a` because it is borrowed
|
| 109 | + //~^ ERROR cannot assign to data in an immutable container |
55 | 110 | drop(p);
|
56 | 111 | drop(q);
|
57 | 112 | }
|
58 | 113 |
|
59 | 114 | fn main() {
|
| 115 | + indirect_write_to_imm_box(); |
60 | 116 | borrow_in_var_from_var();
|
| 117 | + borrow_in_var_from_var_via_imm_box(); |
61 | 118 | borrow_in_var_from_field();
|
| 119 | + borrow_in_var_from_field_via_imm_box(); |
62 | 120 | borrow_in_field_from_var();
|
| 121 | + borrow_in_field_from_var_via_imm_box(); |
63 | 122 | borrow_in_field_from_field();
|
| 123 | + borrow_in_field_from_field_via_imm_box(); |
64 | 124 | }
|
0 commit comments