]> git.lizzy.rs Git - rust.git/blob - src/test/ui/nll/issue-21232-partial-init-and-use.rs
Rollup merge of #105216 - GuillaumeGomez:rm-unused-gui-test, r=notriddle
[rust.git] / src / test / ui / nll / issue-21232-partial-init-and-use.rs
1 // This test enumerates various cases of interest for partial
2 // [re]initialization of ADTs and tuples.
3 //
4 // See rust-lang/rust#21232, rust-lang/rust#54986, and rust-lang/rust#54987.
5 //
6 // All of tests in this file are expected to change from being
7 // rejected, at least under NLL (by rust-lang/rust#54986) to being
8 // **accepted** when rust-lang/rust#54987 is implemented.
9 // (That's why there are assertions in the code.)
10 //
11 // See issue-21232-partial-init-and-erroneous-use.rs for cases of
12 // tests that are meant to continue failing to compile once
13 // rust-lang/rust#54987 is implemented.
14
15 struct S<Y> {
16     x: u32,
17
18     // Note that even though `y` may implement `Drop`, under #54987 we
19     // will still allow partial initialization of `S` itself.
20     y: Y,
21 }
22
23 enum Void { }
24
25 type B = Box<u32>;
26
27 impl S<B> { fn new() -> Self { S { x: 0, y: Box::new(0) } } }
28
29 fn borrow_s(s: &S<B>) { assert_eq!(s.x, 10); assert_eq!(*s.y, 20); }
30 fn move_s(s: S<B>) {  assert_eq!(s.x, 10); assert_eq!(*s.y, 20); }
31 fn borrow_field(x: &u32) { assert_eq!(*x, 10); }
32
33 type T = (u32, B);
34 type Tvoid = (u32, Void);
35
36 fn borrow_t(t: &T) { assert_eq!(t.0, 10); assert_eq!(*t.1, 20); }
37 fn move_t(t: T) {  assert_eq!(t.0, 10); assert_eq!(*t.1, 20); }
38
39 struct Q<F> {
40     v: u32,
41     r: R<F>,
42 }
43
44 struct R<F> {
45     w: u32,
46     f: F,
47 }
48
49 impl<F> Q<F> { fn new(f: F) -> Self { Q { v: 0, r: R::new(f) } } }
50 impl<F> R<F> { fn new(f: F) -> Self { R { w: 0, f } } }
51
52 // Axes to cover:
53 // * local/field: Is the structure in a local or a field
54 // * fully/partial/void: Are we fully initializing it before using any part?
55 //                       Is whole type empty due to a void component?
56 // * init/reinit: First initialization, or did we previously initialize and then move out?
57 // * struct/tuple: Is this a struct or a (X, Y).
58 //
59 // As a shorthand for the cases above, adding a numeric summary to
60 // each test's fn name to denote each point on each axis.
61 //
62 // e.g., 1000 = field fully init struct; 0211 = local void reinit tuple
63
64 // It got pretty monotonous writing the same code over and over, and I
65 // feared I would forget details. So I abstracted some desiderata into
66 // macros. But I left the initialization code inline, because that's
67 // where the errors for #54986 will be emitted.
68
69 macro_rules! use_fully {
70     (struct $s:expr) => { {
71         borrow_field(& $s.x );
72         borrow_s(& $s );
73         move_s( $s );
74     } };
75
76     (tuple $t:expr) => { {
77         borrow_field(& $t.0 );
78         borrow_t(& $t );
79         move_t( $t );
80     } }
81 }
82
83 macro_rules! use_part {
84     (struct $s:expr) => { {
85         borrow_field(& $s.x );
86         match $s { S { ref x, y: _ } => { borrow_field(x); } }
87     } };
88
89     (tuple $t:expr) => { {
90         borrow_field(& $t.0 );
91         match $t { (ref x, _) => { borrow_field(x); } }
92     } }
93 }
94
95 fn test_0000_local_fully_init_and_use_struct() {
96     let s: S<B>;
97     s.x = 10; s.y = Box::new(20); //~ ERROR E0381
98     use_fully!(struct s);
99 }
100
101 fn test_0001_local_fully_init_and_use_tuple() {
102     let t: T;
103     t.0 = 10; t.1 = Box::new(20); //~ ERROR E0381
104     use_fully!(tuple t);
105 }
106
107 fn test_0010_local_fully_reinit_and_use_struct() {
108     let mut s: S<B> = S::new(); drop(s);
109     s.x = 10; s.y = Box::new(20);
110     //~^ ERROR assign to part of moved value: `s` [E0382]
111     use_fully!(struct s);
112 }
113
114 fn test_0011_local_fully_reinit_and_use_tuple() {
115     let mut t: T = (0, Box::new(0)); drop(t);
116     t.0 = 10; t.1 = Box::new(20);
117     //~^ ERROR assign to part of moved value: `t` [E0382]
118     use_fully!(tuple t);
119 }
120
121 fn test_0100_local_partial_init_and_use_struct() {
122     let s: S<B>;
123     s.x = 10; //~ ERROR E0381
124     use_part!(struct s);
125 }
126
127 fn test_0101_local_partial_init_and_use_tuple() {
128     let t: T;
129     t.0 = 10; //~ ERROR E0381
130     use_part!(tuple t);
131 }
132
133 fn test_0110_local_partial_reinit_and_use_struct() {
134     let mut s: S<B> = S::new(); drop(s);
135     s.x = 10;
136     //~^ ERROR assign to part of moved value: `s` [E0382]
137     use_part!(struct s);
138 }
139
140 fn test_0111_local_partial_reinit_and_use_tuple() {
141     let mut t: T = (0, Box::new(0)); drop(t);
142     t.0 = 10;
143     //~^ ERROR assign to part of moved value: `t` [E0382]
144     use_part!(tuple t);
145 }
146
147 fn test_0200_local_void_init_and_use_struct() {
148     let s: S<Void>;
149     s.x = 10; //~ ERROR E0381
150     use_part!(struct s);
151 }
152
153 fn test_0201_local_void_init_and_use_tuple() {
154     let t: Tvoid;
155     t.0 = 10; //~ ERROR E0381
156     use_part!(tuple t);
157 }
158
159 // NOTE: uniform structure of tests here makes n21n (aka combining
160 // Void with Reinit) an (even more) senseless case, as we cannot
161 // safely create initial instance containing Void to move out of and
162 // then reinitialize. While I was tempted to sidestep this via some
163 // unsafe code (eek), lets just instead not encode such tests.
164
165 // fn test_0210_local_void_reinit_and_use_struct() { unimplemented!() }
166 // fn test_0211_local_void_reinit_and_use_tuple() { unimplemented!() }
167
168 fn test_1000_field_fully_init_and_use_struct() {
169     let q: Q<S<B>>;
170     q.r.f.x = 10; q.r.f.y = Box::new(20); //~ ERROR E0381
171     use_fully!(struct q.r.f);
172 }
173
174 fn test_1001_field_fully_init_and_use_tuple() {
175     let q: Q<T>;
176     q.r.f.0 = 10; q.r.f.1 = Box::new(20); //~ ERROR E0381
177     use_fully!(tuple q.r.f);
178 }
179
180 fn test_1010_field_fully_reinit_and_use_struct() {
181     let mut q: Q<S<B>> = Q::new(S::new()); drop(q.r);
182     q.r.f.x = 10; q.r.f.y = Box::new(20);
183     //~^ ERROR assign to part of moved value: `q.r` [E0382]
184     use_fully!(struct q.r.f);
185 }
186
187 fn test_1011_field_fully_reinit_and_use_tuple() {
188     let mut q: Q<T> = Q::new((0, Box::new(0))); drop(q.r);
189     q.r.f.0 = 10; q.r.f.1 = Box::new(20);
190     //~^ ERROR assign to part of moved value: `q.r` [E0382]
191     use_fully!(tuple q.r.f);
192 }
193
194 fn test_1100_field_partial_init_and_use_struct() {
195     let q: Q<S<B>>;
196     q.r.f.x = 10; //~ ERROR E0381
197     use_part!(struct q.r.f);
198 }
199
200 fn test_1101_field_partial_init_and_use_tuple() {
201     let q: Q<T>;
202     q.r.f.0 = 10; //~ ERROR E0381
203     use_part!(tuple q.r.f);
204 }
205
206 fn test_1110_field_partial_reinit_and_use_struct() {
207     let mut q: Q<S<B>> = Q::new(S::new()); drop(q.r);
208     q.r.f.x = 10;
209     //~^ ERROR assign to part of moved value: `q.r` [E0382]
210     use_part!(struct q.r.f);
211 }
212
213 fn test_1111_field_partial_reinit_and_use_tuple() {
214     let mut q: Q<T> = Q::new((0, Box::new(0))); drop(q.r);
215     q.r.f.0 = 10;
216     //~^ ERROR assign to part of moved value: `q.r` [E0382]
217     use_part!(tuple q.r.f);
218 }
219
220 fn test_1200_field_void_init_and_use_struct() {
221     let mut q: Q<S<Void>>;
222     q.r.f.x = 10; //~ ERROR E0381
223     use_part!(struct q.r.f);
224 }
225
226 fn test_1201_field_void_init_and_use_tuple() {
227     let mut q: Q<Tvoid>;
228     q.r.f.0 = 10; //~ ERROR E0381
229     use_part!(tuple q.r.f);
230 }
231
232 // See NOTE abve.
233
234 // fn test_1210_field_void_reinit_and_use_struct() { unimplemented!() }
235 // fn test_1211_field_void_reinit_and_use_tuple() { unimplemented!() }
236
237 // The below are some additional cases of interest that have been
238 // transcribed from other bugs based on old erroneous codegen when we
239 // encountered partial writes.
240
241 fn issue_26996() {
242     let mut c = (1, "".to_owned());
243     match c {
244         c2 => {
245             c.0 = 2; //~ ERROR assign to part of moved value
246             assert_eq!(c2.0, 1);
247         }
248     }
249 }
250
251 fn issue_27021() {
252     let mut c = (1, (1, "".to_owned()));
253     match c {
254         c2 => {
255             (c.1).0 = 2; //~ ERROR assign to part of moved value
256             assert_eq!((c2.1).0, 1);
257         }
258     }
259
260     let mut c = (1, (1, (1, "".to_owned())));
261     match c.1 {
262         c2 => {
263             ((c.1).1).0 = 3; //~ ERROR assign to part of moved value
264             assert_eq!((c2.1).0, 1);
265         }
266     }
267 }
268
269 fn main() {
270     test_0000_local_fully_init_and_use_struct();
271     test_0001_local_fully_init_and_use_tuple();
272     test_0010_local_fully_reinit_and_use_struct();
273     test_0011_local_fully_reinit_and_use_tuple();
274     test_0100_local_partial_init_and_use_struct();
275     test_0101_local_partial_init_and_use_tuple();
276     test_0110_local_partial_reinit_and_use_struct();
277     test_0111_local_partial_reinit_and_use_tuple();
278     test_0200_local_void_init_and_use_struct();
279     test_0201_local_void_init_and_use_tuple();
280     // test_0210_local_void_reinit_and_use_struct();
281     // test_0211_local_void_reinit_and_use_tuple();
282     test_1000_field_fully_init_and_use_struct();
283     test_1001_field_fully_init_and_use_tuple();
284     test_1010_field_fully_reinit_and_use_struct();
285     test_1011_field_fully_reinit_and_use_tuple();
286     test_1100_field_partial_init_and_use_struct();
287     test_1101_field_partial_init_and_use_tuple();
288     test_1110_field_partial_reinit_and_use_struct();
289     test_1111_field_partial_reinit_and_use_tuple();
290     test_1200_field_void_init_and_use_struct();
291     test_1201_field_void_init_and_use_tuple();
292     // test_1210_field_void_reinit_and_use_struct();
293     // test_1211_field_void_reinit_and_use_tuple();
294
295     issue_26996();
296     issue_27021();
297 }