]> git.lizzy.rs Git - rust.git/blob - tests/ui/generator/size-moved-locals.rs
Auto merge of #106646 - Amanieu:ilp32-object, r=Mark-Simulacrum
[rust.git] / tests / ui / generator / size-moved-locals.rs
1 // run-pass
2 // Test that we don't duplicate storage for a variable that is moved to another
3 // binding. This used to happen in the presence of unwind and drop edges (see
4 // `complex` below.)
5 //
6 // The exact sizes here can change (we'd like to know when they do). What we
7 // don't want to see is the `complex` generator size being upwards of 2048 bytes
8 // (which would indicate it is reserving space for two copies of Foo.)
9 //
10 // See issue #59123 for a full explanation.
11
12 // edition:2018
13 // ignore-wasm32 issue #62807
14 // ignore-asmjs issue #62807
15 // needs-unwind Size of Closures change on panic=abort
16
17 #![feature(generators, generator_trait)]
18
19 use std::ops::Generator;
20
21 const FOO_SIZE: usize = 1024;
22 struct Foo(#[allow(unused_tuple_struct_fields)] [u8; FOO_SIZE]);
23
24 impl Drop for Foo {
25     fn drop(&mut self) {}
26 }
27
28 fn move_before_yield() -> impl Generator<Yield = (), Return = ()> {
29     static || {
30         let first = Foo([0; FOO_SIZE]);
31         let _second = first;
32         yield;
33         // _second dropped here
34     }
35 }
36
37 fn noop() {}
38
39 fn move_before_yield_with_noop() -> impl Generator<Yield = (), Return = ()> {
40     static || {
41         let first = Foo([0; FOO_SIZE]);
42         noop();
43         let _second = first;
44         yield;
45         // _second dropped here
46     }
47 }
48
49 // Today we don't have NRVO (we allocate space for both `first` and `second`,)
50 // but we can overlap `first` with `_third`.
51 fn overlap_move_points() -> impl Generator<Yield = (), Return = ()> {
52     static || {
53         let first = Foo([0; FOO_SIZE]);
54         yield;
55         let second = first;
56         yield;
57         let _third = second;
58         yield;
59     }
60 }
61
62 fn overlap_x_and_y() -> impl Generator<Yield = (), Return = ()> {
63     static || {
64         let x = Foo([0; FOO_SIZE]);
65         yield;
66         drop(x);
67         let y = Foo([0; FOO_SIZE]);
68         yield;
69         drop(y);
70     }
71 }
72
73 fn main() {
74     assert_eq!(1025, std::mem::size_of_val(&move_before_yield()));
75     assert_eq!(1026, std::mem::size_of_val(&move_before_yield_with_noop()));
76     assert_eq!(2051, std::mem::size_of_val(&overlap_move_points()));
77     assert_eq!(1026, std::mem::size_of_val(&overlap_x_and_y()));
78 }