]>
Commit | Line | Data |
---|---|---|
1a4d82fc JJ |
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 | |
c34b1796 AL |
12 | // are borrowed (#14498). |
13 | // | |
14 | // Also includes tests of the errors reported when the Box in question | |
15 | // is immutable (#14270). | |
1a4d82fc | 16 | |
ff7c6d11 XL |
17 | // revisions: ast mir |
18 | //[mir]compile-flags: -Z borrowck=mir | |
19 | ||
1a4d82fc JJ |
20 | #![feature(box_syntax)] |
21 | ||
22 | struct A { a: isize } | |
23 | struct B<'a> { a: Box<&'a mut isize> } | |
24 | ||
c34b1796 AL |
25 | fn indirect_write_to_imm_box() { |
26 | let mut x: isize = 1; | |
27 | let y: Box<_> = box &mut x; | |
28 | let p = &y; | |
ff7c6d11 | 29 | ***p = 2; //[ast]~ ERROR cannot assign to data in a `&` reference |
94b46f34 | 30 | //[mir]~^ ERROR cannot assign to `***p` |
c34b1796 AL |
31 | drop(p); |
32 | } | |
33 | ||
1a4d82fc JJ |
34 | fn borrow_in_var_from_var() { |
35 | let mut x: isize = 1; | |
c34b1796 AL |
36 | let mut y: Box<_> = box &mut x; |
37 | let p = &y; | |
38 | let q = &***p; | |
ff7c6d11 XL |
39 | **y = 2; //[ast]~ ERROR cannot assign to `**y` because it is borrowed |
40 | //[mir]~^ ERROR cannot assign to `**y` because it is borrowed | |
c34b1796 AL |
41 | drop(p); |
42 | drop(q); | |
43 | } | |
44 | ||
45 | fn borrow_in_var_from_var_via_imm_box() { | |
46 | let mut x: isize = 1; | |
47 | let y: Box<_> = box &mut x; | |
1a4d82fc JJ |
48 | let p = &y; |
49 | let q = &***p; | |
ff7c6d11 XL |
50 | **y = 2; //[ast]~ ERROR cannot assign to `**y` because it is borrowed |
51 | //[mir]~^ ERROR cannot assign to `**y` because it is borrowed | |
1a4d82fc JJ |
52 | drop(p); |
53 | drop(q); | |
54 | } | |
55 | ||
56 | fn borrow_in_var_from_field() { | |
57 | let mut x = A { a: 1 }; | |
c34b1796 | 58 | let mut y: Box<_> = box &mut x.a; |
1a4d82fc JJ |
59 | let p = &y; |
60 | let q = &***p; | |
ff7c6d11 XL |
61 | **y = 2; //[ast]~ ERROR cannot assign to `**y` because it is borrowed |
62 | //[mir]~^ ERROR cannot assign to `**y` because it is borrowed | |
1a4d82fc JJ |
63 | drop(p); |
64 | drop(q); | |
65 | } | |
66 | ||
c34b1796 AL |
67 | fn borrow_in_var_from_field_via_imm_box() { |
68 | let mut x = A { a: 1 }; | |
69 | let y: Box<_> = box &mut x.a; | |
70 | let p = &y; | |
71 | let q = &***p; | |
ff7c6d11 XL |
72 | **y = 2; //[ast]~ ERROR cannot assign to `**y` because it is borrowed |
73 | //[mir]~^ ERROR cannot assign to `**y` because it is borrowed | |
c34b1796 AL |
74 | drop(p); |
75 | drop(q); | |
76 | } | |
77 | ||
1a4d82fc | 78 | fn borrow_in_field_from_var() { |
c34b1796 AL |
79 | let mut x: isize = 1; |
80 | let mut y = B { a: box &mut x }; | |
81 | let p = &y.a; | |
82 | let q = &***p; | |
ff7c6d11 XL |
83 | **y.a = 2; //[ast]~ ERROR cannot assign to `**y.a` because it is borrowed |
84 | //[mir]~^ ERROR cannot assign to `**y.a` because it is borrowed | |
c34b1796 AL |
85 | drop(p); |
86 | drop(q); | |
87 | } | |
88 | ||
89 | fn borrow_in_field_from_var_via_imm_box() { | |
1a4d82fc JJ |
90 | let mut x: isize = 1; |
91 | let y = B { a: box &mut x }; | |
92 | let p = &y.a; | |
93 | let q = &***p; | |
ff7c6d11 XL |
94 | **y.a = 2; //[ast]~ ERROR cannot assign to `**y.a` because it is borrowed |
95 | //[mir]~^ ERROR cannot assign to `**y.a` because it is borrowed | |
1a4d82fc JJ |
96 | drop(p); |
97 | drop(q); | |
98 | } | |
99 | ||
100 | fn borrow_in_field_from_field() { | |
c34b1796 AL |
101 | let mut x = A { a: 1 }; |
102 | let mut y = B { a: box &mut x.a }; | |
103 | let p = &y.a; | |
104 | let q = &***p; | |
ff7c6d11 XL |
105 | **y.a = 2; //[ast]~ ERROR cannot assign to `**y.a` because it is borrowed |
106 | //[mir]~^ ERROR cannot assign to `**y.a` because it is borrowed | |
c34b1796 AL |
107 | drop(p); |
108 | drop(q); | |
109 | } | |
110 | ||
111 | fn borrow_in_field_from_field_via_imm_box() { | |
1a4d82fc JJ |
112 | let mut x = A { a: 1 }; |
113 | let y = B { a: box &mut x.a }; | |
114 | let p = &y.a; | |
115 | let q = &***p; | |
ff7c6d11 XL |
116 | **y.a = 2; //[ast]~ ERROR cannot assign to `**y.a` because it is borrowed |
117 | //[mir]~^ ERROR cannot assign to `**y.a` because it is borrowed | |
1a4d82fc JJ |
118 | drop(p); |
119 | drop(q); | |
120 | } | |
121 | ||
122 | fn main() { | |
c34b1796 | 123 | indirect_write_to_imm_box(); |
1a4d82fc | 124 | borrow_in_var_from_var(); |
c34b1796 | 125 | borrow_in_var_from_var_via_imm_box(); |
1a4d82fc | 126 | borrow_in_var_from_field(); |
c34b1796 | 127 | borrow_in_var_from_field_via_imm_box(); |
1a4d82fc | 128 | borrow_in_field_from_var(); |
c34b1796 | 129 | borrow_in_field_from_var_via_imm_box(); |
1a4d82fc | 130 | borrow_in_field_from_field(); |
c34b1796 | 131 | borrow_in_field_from_field_via_imm_box(); |
1a4d82fc | 132 | } |