]>
Commit | Line | Data |
---|---|---|
1a4d82fc | 1 | // This tests that we can't modify Box<&mut T> contents while they |
c34b1796 AL |
2 | // are borrowed (#14498). |
3 | // | |
4 | // Also includes tests of the errors reported when the Box in question | |
5 | // is immutable (#14270). | |
1a4d82fc | 6 | |
c295e0f8 | 7 | |
1a4d82fc JJ |
8 | |
9 | struct A { a: isize } | |
10 | struct B<'a> { a: Box<&'a mut isize> } | |
11 | ||
c34b1796 AL |
12 | fn indirect_write_to_imm_box() { |
13 | let mut x: isize = 1; | |
c295e0f8 | 14 | let y: Box<_> = Box::new(&mut x); |
c34b1796 | 15 | let p = &y; |
48663c56 | 16 | ***p = 2; //~ ERROR cannot assign to `***p` |
c34b1796 AL |
17 | drop(p); |
18 | } | |
19 | ||
1a4d82fc JJ |
20 | fn borrow_in_var_from_var() { |
21 | let mut x: isize = 1; | |
c295e0f8 | 22 | let mut y: Box<_> = Box::new(&mut x); |
c34b1796 AL |
23 | let p = &y; |
24 | let q = &***p; | |
48663c56 | 25 | **y = 2; //~ ERROR cannot assign to `**y` because it is borrowed |
c34b1796 AL |
26 | drop(p); |
27 | drop(q); | |
28 | } | |
29 | ||
30 | fn borrow_in_var_from_var_via_imm_box() { | |
31 | let mut x: isize = 1; | |
c295e0f8 | 32 | let y: Box<_> = Box::new(&mut x); |
1a4d82fc JJ |
33 | let p = &y; |
34 | let q = &***p; | |
48663c56 | 35 | **y = 2; //~ ERROR cannot assign to `**y` because it is borrowed |
1a4d82fc JJ |
36 | drop(p); |
37 | drop(q); | |
38 | } | |
39 | ||
40 | fn borrow_in_var_from_field() { | |
41 | let mut x = A { a: 1 }; | |
c295e0f8 | 42 | let mut y: Box<_> = Box::new(&mut x.a); |
1a4d82fc JJ |
43 | let p = &y; |
44 | let q = &***p; | |
48663c56 | 45 | **y = 2; //~ ERROR cannot assign to `**y` because it is borrowed |
1a4d82fc JJ |
46 | drop(p); |
47 | drop(q); | |
48 | } | |
49 | ||
c34b1796 AL |
50 | fn borrow_in_var_from_field_via_imm_box() { |
51 | let mut x = A { a: 1 }; | |
c295e0f8 | 52 | let y: Box<_> = Box::new(&mut x.a); |
c34b1796 AL |
53 | let p = &y; |
54 | let q = &***p; | |
48663c56 | 55 | **y = 2; //~ ERROR cannot assign to `**y` because it is borrowed |
c34b1796 AL |
56 | drop(p); |
57 | drop(q); | |
58 | } | |
59 | ||
1a4d82fc | 60 | fn borrow_in_field_from_var() { |
c34b1796 | 61 | let mut x: isize = 1; |
c295e0f8 | 62 | let mut y = B { a: Box::new(&mut x) }; |
c34b1796 AL |
63 | let p = &y.a; |
64 | let q = &***p; | |
48663c56 | 65 | **y.a = 2; //~ ERROR cannot assign to `**y.a` because it is borrowed |
c34b1796 AL |
66 | drop(p); |
67 | drop(q); | |
68 | } | |
69 | ||
70 | fn borrow_in_field_from_var_via_imm_box() { | |
1a4d82fc | 71 | let mut x: isize = 1; |
c295e0f8 | 72 | let y = B { a: Box::new(&mut x) }; |
1a4d82fc JJ |
73 | let p = &y.a; |
74 | let q = &***p; | |
48663c56 | 75 | **y.a = 2; //~ ERROR cannot assign to `**y.a` because it is borrowed |
1a4d82fc JJ |
76 | drop(p); |
77 | drop(q); | |
78 | } | |
79 | ||
80 | fn borrow_in_field_from_field() { | |
c34b1796 | 81 | let mut x = A { a: 1 }; |
c295e0f8 | 82 | let mut y = B { a: Box::new(&mut x.a) }; |
c34b1796 AL |
83 | let p = &y.a; |
84 | let q = &***p; | |
48663c56 | 85 | **y.a = 2; //~ ERROR cannot assign to `**y.a` because it is borrowed |
c34b1796 AL |
86 | drop(p); |
87 | drop(q); | |
88 | } | |
89 | ||
90 | fn borrow_in_field_from_field_via_imm_box() { | |
1a4d82fc | 91 | let mut x = A { a: 1 }; |
c295e0f8 | 92 | let y = B { a: Box::new(&mut x.a) }; |
1a4d82fc JJ |
93 | let p = &y.a; |
94 | let q = &***p; | |
48663c56 | 95 | **y.a = 2; //~ ERROR cannot assign to `**y.a` because it is borrowed |
1a4d82fc JJ |
96 | drop(p); |
97 | drop(q); | |
98 | } | |
99 | ||
100 | fn main() { | |
c34b1796 | 101 | indirect_write_to_imm_box(); |
1a4d82fc | 102 | borrow_in_var_from_var(); |
c34b1796 | 103 | borrow_in_var_from_var_via_imm_box(); |
1a4d82fc | 104 | borrow_in_var_from_field(); |
c34b1796 | 105 | borrow_in_var_from_field_via_imm_box(); |
1a4d82fc | 106 | borrow_in_field_from_var(); |
c34b1796 | 107 | borrow_in_field_from_var_via_imm_box(); |
1a4d82fc | 108 | borrow_in_field_from_field(); |
c34b1796 | 109 | borrow_in_field_from_field_via_imm_box(); |
1a4d82fc | 110 | } |