Skip to content

Commit 74eb4b4

Browse files
author
Cameron Zwarich
committed
Add new tests for borrowck field-sensitivity.
1 parent c53d296 commit 74eb4b4

File tree

2 files changed

+347
-0
lines changed

2 files changed

+347
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
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+
13+
fn borrow<T>(_: &T) { }
14+
15+
fn use_after_move() {
16+
let x = A { a: 1, b: box 2 };
17+
drop(x.b);
18+
drop(*x.b); //~ ERROR use of partially moved value: `*x.b`
19+
}
20+
21+
fn use_after_fu_move() {
22+
let x = A { a: 1, b: box 2 };
23+
let y = A { a: 3, .. x };
24+
drop(*x.b); //~ ERROR use of partially moved value: `*x.b`
25+
}
26+
27+
fn borrow_after_move() {
28+
let x = A { a: 1, b: box 2 };
29+
drop(x.b);
30+
borrow(&x.b); //~ ERROR use of moved value: `x.b`
31+
}
32+
33+
fn borrow_after_fu_move() {
34+
let x = A { a: 1, b: box 2 };
35+
let _y = A { a: 3, .. x };
36+
borrow(&x.b); //~ ERROR use of moved value: `x.b`
37+
}
38+
39+
fn move_after_borrow() {
40+
let x = A { a: 1, b: box 2 };
41+
let y = &x.b;
42+
drop(x.b); //~ ERROR cannot move out of `x.b` because it is borrowed
43+
borrow(&*y);
44+
}
45+
46+
fn fu_move_after_borrow() {
47+
let x = A { a: 1, b: box 2 };
48+
let y = &x.b;
49+
let _z = A { a: 3, .. x }; //~ ERROR cannot move out of `x.b` because it is borrowed
50+
borrow(&*y);
51+
}
52+
53+
fn mut_borrow_after_mut_borrow() {
54+
let mut x = A { a: 1, b: box 2 };
55+
let y = &mut x.a;
56+
let z = &mut x.a; //~ ERROR cannot borrow `x.a` as mutable more than once at a time
57+
drop(*y);
58+
drop(*z);
59+
}
60+
61+
fn move_after_move() {
62+
let x = A { a: 1, b: box 2 };
63+
drop(x.b);
64+
drop(x.b); //~ ERROR use of moved value: `x.b`
65+
}
66+
67+
fn move_after_fu_move() {
68+
let x = A { a: 1, b: box 2 };
69+
let _y = A { a: 3, .. x };
70+
drop(x.b); //~ ERROR use of moved value: `x.b`
71+
}
72+
73+
fn fu_move_after_move() {
74+
let x = A { a: 1, b: box 2 };
75+
drop(x.b);
76+
let _z = A { a: 3, .. x }; //~ ERROR use of moved value: `x.b`
77+
}
78+
79+
fn fu_move_after_fu_move() {
80+
let x = A { a: 1, b: box 2 };
81+
let _y = A { a: 3, .. x };
82+
let _z = A { a: 4, .. x }; //~ ERROR use of moved value: `x.b`
83+
}
84+
85+
// The following functions aren't yet accepted, but they should be.
86+
87+
fn use_after_field_assign_after_uninit() {
88+
let mut x: A;
89+
x.a = 1;
90+
drop(x.a); //~ ERROR use of possibly uninitialized variable: `x.a`
91+
}
92+
93+
fn borrow_after_field_assign_after_uninit() {
94+
let mut x: A;
95+
x.a = 1;
96+
borrow(&x.a); //~ ERROR use of possibly uninitialized variable: `x.a`
97+
}
98+
99+
fn move_after_field_assign_after_uninit() {
100+
let mut x: A;
101+
x.b = box 1;
102+
drop(x.b); //~ ERROR use of possibly uninitialized variable: `x.b`
103+
}
104+
105+
fn main() {
106+
use_after_move();
107+
use_after_fu_move();
108+
109+
borrow_after_move();
110+
borrow_after_fu_move();
111+
move_after_borrow();
112+
fu_move_after_borrow();
113+
mut_borrow_after_mut_borrow();
114+
115+
move_after_move();
116+
move_after_fu_move();
117+
fu_move_after_move();
118+
fu_move_after_fu_move();
119+
120+
use_after_field_assign_after_uninit();
121+
borrow_after_field_assign_after_uninit();
122+
move_after_field_assign_after_uninit();
123+
}
124+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,223 @@
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 borrow<T>(_: &T) { }
15+
16+
fn move_after_use() {
17+
let x = A { a: 1, b: box 2 };
18+
drop(x.a);
19+
drop(x.b);
20+
}
21+
22+
fn move_after_fu_use() {
23+
let x = A { a: 1, b: box 2 };
24+
let _y = A { b: box 3, .. x };
25+
drop(x.b);
26+
}
27+
28+
fn fu_move_after_use() {
29+
let x = A { a: 1, b: box 2 };
30+
drop(x.a);
31+
let y = A { a: 3, .. x };
32+
drop(y.b);
33+
}
34+
35+
fn fu_move_after_fu_use() {
36+
let x = A { a: 1, b: box 2 };
37+
let _y = A { b: box 3, .. x };
38+
let z = A { a: 4, .. x };
39+
drop(z.b);
40+
}
41+
42+
fn use_after_move() {
43+
let x = A { a: 1, b: box 2 };
44+
drop(x.b);
45+
drop(x.a);
46+
}
47+
48+
fn use_after_fu_move() {
49+
let x = A { a: 1, b: box 2 };
50+
let y = A { a: 3, .. x };
51+
drop(x.a);
52+
drop(y.b);
53+
}
54+
55+
fn fu_use_after_move() {
56+
let x = A { a: 1, b: box 2 };
57+
drop(x.b);
58+
let _y = A { b: box 3, .. x };
59+
}
60+
61+
fn fu_use_after_fu_move() {
62+
let x = A { a: 1, b: box 2 };
63+
let y = A { a: 3, .. x };
64+
let _z = A { b: box 3, .. x };
65+
drop(y.b);
66+
}
67+
68+
fn borrow_after_move() {
69+
let x = A { a: 1, b: box 2 };
70+
drop(x.b);
71+
borrow(&x.a);
72+
}
73+
74+
fn borrow_after_fu_move() {
75+
let x = A { a: 1, b: box 2 };
76+
let y = A { a: 3, .. x };
77+
borrow(&x.a);
78+
drop(y.b);
79+
}
80+
81+
fn move_after_borrow() {
82+
let x = A { a: 1, b: box 2 };
83+
borrow(&x.a);
84+
drop(x.b);
85+
}
86+
87+
fn fu_move_after_borrow() {
88+
let x = A { a: 1, b: box 2 };
89+
borrow(&x.a);
90+
let y = A { a: 3, .. x };
91+
drop(y.b);
92+
}
93+
94+
fn mut_borrow_after_mut_borrow() {
95+
let mut x = A { a: 1, b: box 2 };
96+
let y = &mut x.a;
97+
let z = &mut x.b;
98+
drop(*y);
99+
drop(**z);
100+
}
101+
102+
fn move_after_move() {
103+
let x = B { a: box 1, b: box 2 };
104+
drop(x.a);
105+
drop(x.b);
106+
}
107+
108+
fn move_after_fu_move() {
109+
let x = B { a: box 1, b: box 2 };
110+
let y = B { a: box 3, .. x };
111+
drop(x.a);
112+
drop(y.b);
113+
}
114+
115+
fn fu_move_after_move() {
116+
let x = B { a: box 1, b: box 2 };
117+
drop(x.a);
118+
let z = B { a: box 3, .. x };
119+
drop(z.b);
120+
}
121+
122+
fn fu_move_after_fu_move() {
123+
let x = B { a: box 1, b: box 2 };
124+
let y = B { b: box 3, .. x };
125+
let z = B { a: box 4, .. x };
126+
drop(y.a);
127+
drop(z.b);
128+
}
129+
130+
fn use_after_assign_after_move() {
131+
let mut x = A { a: 1, b: box 2 };
132+
drop(x.b);
133+
x = A { a: 3, b: box 4 };
134+
drop(*x.b);
135+
}
136+
137+
fn use_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 borrow_after_assign_after_move() {
145+
let mut x = A { a: 1, b: box 2 };
146+
drop(x.b);
147+
x = A { a: 3, b: box 4 };
148+
borrow(&x.b);
149+
}
150+
151+
fn borrow_after_field_assign_after_move() {
152+
let mut x = A { a: 1, b: box 2 };
153+
drop(x.b);
154+
x.b = box 3;
155+
borrow(&x.b);
156+
}
157+
158+
fn move_after_assign_after_move() {
159+
let mut x = A { a: 1, b: box 2 };
160+
let y = x.b;
161+
x = A { a: 3, b: box 4 };
162+
drop(x.b);
163+
drop(y);
164+
}
165+
166+
fn move_after_field_assign_after_move() {
167+
let mut x = A { a: 1, b: box 2 };
168+
drop(x.b);
169+
x.b = box 3;
170+
drop(x.b);
171+
}
172+
173+
fn use_after_assign_after_uninit() {
174+
let mut x: A;
175+
x = A { a: 1, b: box 2 };
176+
drop(x.a);
177+
}
178+
179+
fn borrow_after_assign_after_uninit() {
180+
let mut x: A;
181+
x = A { a: 1, b: box 2 };
182+
borrow(&x.a);
183+
}
184+
185+
fn move_after_assign_after_uninit() {
186+
let mut x: A;
187+
x = A { a: 1, b: box 2 };
188+
drop(x.b);
189+
}
190+
191+
fn main() {
192+
move_after_use();
193+
move_after_fu_use();
194+
fu_move_after_use();
195+
fu_move_after_fu_use();
196+
use_after_move();
197+
use_after_fu_move();
198+
fu_use_after_move();
199+
fu_use_after_fu_move();
200+
201+
borrow_after_move();
202+
borrow_after_fu_move();
203+
move_after_borrow();
204+
fu_move_after_borrow();
205+
mut_borrow_after_mut_borrow();
206+
207+
move_after_move();
208+
move_after_fu_move();
209+
fu_move_after_move();
210+
fu_move_after_fu_move();
211+
212+
use_after_assign_after_move();
213+
use_after_field_assign_after_move();
214+
borrow_after_assign_after_move();
215+
borrow_after_field_assign_after_move();
216+
move_after_assign_after_move();
217+
move_after_field_assign_after_move();
218+
219+
use_after_assign_after_uninit();
220+
borrow_after_assign_after_uninit();
221+
move_after_assign_after_uninit();
222+
}
223+

0 commit comments

Comments
 (0)