]> git.lizzy.rs Git - rust.git/blob - src/test/run-pass/borrowck-field-sensitivity.rs
33be47e504be2a06c6624d9d00b8a3e3990ee450
[rust.git] / src / test / run-pass / borrowck-field-sensitivity.rs
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 struct B { a: Box<int>, b: Box<int> }
13
14 fn move_after_copy() {
15     let x = A { a: 1, b: box 2 };
16     drop(x.a);
17     drop(x.b);
18 }
19
20 fn move_after_fu_copy() {
21     let x = A { a: 1, b: box 2 };
22     let _y = A { b: box 3, .. x };
23     drop(x.b);
24 }
25
26 fn fu_move_after_copy() {
27     let x = A { a: 1, b: box 2 };
28     drop(x.a);
29     let _y = A { a: 3, .. x };
30 }
31
32 fn fu_move_after_fu_copy() {
33     let x = A { a: 1, b: box 2 };
34     let _y = A { b: box 3, .. x };
35     let _z = A { a: 4, .. x };
36 }
37
38 fn copy_after_move() {
39     let x = A { a: 1, b: box 2 };
40     drop(x.b);
41     drop(x.a);
42 }
43
44 fn copy_after_fu_move() {
45     let x = A { a: 1, b: box 2 };
46     let y = A { a: 3, .. x };
47     drop(x.a);
48 }
49
50 fn fu_copy_after_move() {
51     let x = A { a: 1, b: box 2 };
52     drop(x.b);
53     let _y = A { b: box 3, .. x };
54 }
55
56 fn fu_copy_after_fu_move() {
57     let x = A { a: 1, b: box 2 };
58     let _y = A { a: 3, .. x };
59     let _z = A { b: box 3, .. x };
60 }
61
62 fn borrow_after_move() {
63     let x = A { a: 1, b: box 2 };
64     drop(x.b);
65     let p = &x.a;
66     drop(*p);
67 }
68
69 fn borrow_after_fu_move() {
70     let x = A { a: 1, b: box 2 };
71     let _y = A { a: 3, .. x };
72     let p = &x.a;
73     drop(*p);
74 }
75
76 fn move_after_borrow() {
77     let x = A { a: 1, b: box 2 };
78     let p = &x.a;
79     drop(x.b);
80     drop(*p);
81 }
82
83 fn fu_move_after_borrow() {
84     let x = A { a: 1, b: box 2 };
85     let p = &x.a;
86     let _y = A { a: 3, .. x };
87     drop(*p);
88 }
89
90 fn mut_borrow_after_mut_borrow() {
91     let mut x = A { a: 1, b: box 2 };
92     let p = &mut x.a;
93     let q = &mut x.b;
94     drop(*p);
95     drop(**q);
96 }
97
98 fn move_after_move() {
99     let x = B { a: box 1, b: box 2 };
100     drop(x.a);
101     drop(x.b);
102 }
103
104 fn move_after_fu_move() {
105     let x = B { a: box 1, b: box 2 };
106     let y = B { a: box 3, .. x };
107     drop(x.a);
108 }
109
110 fn fu_move_after_move() {
111     let x = B { a: box 1, b: box 2 };
112     drop(x.a);
113     let z = B { a: box 3, .. x };
114     drop(z.b);
115 }
116
117 fn fu_move_after_fu_move() {
118     let x = B { a: box 1, b: box 2 };
119     let _y = B { b: box 3, .. x };
120     let _z = B { a: box 4, .. x };
121 }
122
123 fn copy_after_assign_after_move() {
124     let mut x = A { a: 1, b: box 2 };
125     drop(x.b);
126     x = A { a: 3, b: box 4 };
127     drop(*x.b);
128 }
129
130 fn copy_after_assign_after_fu_move() {
131     let mut x = A { a: 1, b: box 2 };
132     let _y = A { a: 3, .. x };
133     x = A { a: 3, b: box 4 };
134     drop(*x.b);
135 }
136
137 fn copy_after_field_assign_after_move() {
138     let mut x = A { a: 1, b: box 2 };
139     drop(x.b);
140     x.b = box 3;
141     drop(*x.b);
142 }
143
144 fn copy_after_field_assign_after_fu_move() {
145     let mut x = A { a: 1, b: box 2 };
146     let _y = A { a: 3, .. x };
147     x.b = box 3;
148     drop(*x.b);
149 }
150
151 fn borrow_after_assign_after_move() {
152     let mut x = A { a: 1, b: box 2 };
153     drop(x.b);
154     x = A { a: 3, b: box 4 };
155     let p = &x.b;
156     drop(**p);
157 }
158
159 fn borrow_after_assign_after_fu_move() {
160     let mut x = A { a: 1, b: box 2 };
161     let _y = A { a: 3, .. x };
162     x = A { a: 3, b: box 4 };
163     let p = &x.b;
164     drop(**p);
165 }
166
167 fn borrow_after_field_assign_after_move() {
168     let mut x = A { a: 1, b: box 2 };
169     drop(x.b);
170     x.b = box 3;
171     let p = &x.b;
172     drop(**p);
173 }
174
175 fn borrow_after_field_assign_after_fu_move() {
176     let mut x = A { a: 1, b: box 2 };
177     let _y = A { a: 3, .. x };
178     x.b = box 3;
179     let p = &x.b;
180     drop(**p);
181 }
182
183 fn move_after_assign_after_move() {
184     let mut x = A { a: 1, b: box 2 };
185     let _y = x.b;
186     x = A { a: 3, b: box 4 };
187     drop(x.b);
188 }
189
190 fn move_after_assign_after_fu_move() {
191     let mut x = A { a: 1, b: box 2 };
192     let _y = A { a: 3, .. x };
193     x = A { a: 3, b: box 4 };
194     drop(x.b);
195 }
196
197 fn move_after_field_assign_after_move() {
198     let mut x = A { a: 1, b: box 2 };
199     drop(x.b);
200     x.b = box 3;
201     drop(x.b);
202 }
203
204 fn move_after_field_assign_after_fu_move() {
205     let mut x = A { a: 1, b: box 2 };
206     let _y = A { a: 3, .. x };
207     x.b = box 3;
208     drop(x.b);
209 }
210
211 fn copy_after_assign_after_uninit() {
212     let mut x: A;
213     x = A { a: 1, b: box 2 };
214     drop(x.a);
215 }
216
217 fn borrow_after_assign_after_uninit() {
218     let mut x: A;
219     x = A { a: 1, b: box 2 };
220     let p = &x.a;
221     drop(*p);
222 }
223
224 fn move_after_assign_after_uninit() {
225     let mut x: A;
226     x = A { a: 1, b: box 2 };
227     drop(x.b);
228 }
229
230 fn main() {
231     move_after_copy();
232     move_after_fu_copy();
233     fu_move_after_copy();
234     fu_move_after_fu_copy();
235     copy_after_move();
236     copy_after_fu_move();
237     fu_copy_after_move();
238     fu_copy_after_fu_move();
239
240     borrow_after_move();
241     borrow_after_fu_move();
242     move_after_borrow();
243     fu_move_after_borrow();
244     mut_borrow_after_mut_borrow();
245
246     move_after_move();
247     move_after_fu_move();
248     fu_move_after_move();
249     fu_move_after_fu_move();
250
251     copy_after_assign_after_move();
252     copy_after_assign_after_fu_move();
253     copy_after_field_assign_after_move();
254     copy_after_field_assign_after_fu_move();
255
256     borrow_after_assign_after_move();
257     borrow_after_assign_after_fu_move();
258     borrow_after_field_assign_after_move();
259     borrow_after_field_assign_after_fu_move();
260
261     move_after_assign_after_move();
262     move_after_assign_after_fu_move();
263     move_after_field_assign_after_move();
264     move_after_field_assign_after_fu_move();
265
266     copy_after_assign_after_uninit();
267     borrow_after_assign_after_uninit();
268     move_after_assign_after_uninit();
269 }
270