]> git.proxmox.com Git - rustc.git/blob - src/test/ui/generator/size-moved-locals.rs
New upstream version 1.64.0+dfsg1
[rustc.git] / src / test / 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
16 #![feature(generators, generator_trait)]
17
18 use std::ops::Generator;
19
20 const FOO_SIZE: usize = 1024;
21 struct Foo(#[allow(unused_tuple_struct_fields)] [u8; FOO_SIZE]);
22
23 impl Drop for Foo {
24 fn drop(&mut self) {}
25 }
26
27 fn move_before_yield() -> impl Generator<Yield = (), Return = ()> {
28 static || {
29 let first = Foo([0; FOO_SIZE]);
30 let _second = first;
31 yield;
32 // _second dropped here
33 }
34 }
35
36 fn noop() {}
37
38 fn move_before_yield_with_noop() -> impl Generator<Yield = (), Return = ()> {
39 static || {
40 let first = Foo([0; FOO_SIZE]);
41 noop();
42 let _second = first;
43 yield;
44 // _second dropped here
45 }
46 }
47
48 // Today we don't have NRVO (we allocate space for both `first` and `second`,)
49 // but we can overlap `first` with `_third`.
50 fn overlap_move_points() -> impl Generator<Yield = (), Return = ()> {
51 static || {
52 let first = Foo([0; FOO_SIZE]);
53 yield;
54 let second = first;
55 yield;
56 let _third = second;
57 yield;
58 }
59 }
60
61 fn overlap_x_and_y() -> impl Generator<Yield = (), Return = ()> {
62 static || {
63 let x = Foo([0; FOO_SIZE]);
64 yield;
65 drop(x);
66 let y = Foo([0; FOO_SIZE]);
67 yield;
68 drop(y);
69 }
70 }
71
72 fn main() {
73 assert_eq!(1025, std::mem::size_of_val(&move_before_yield()));
74 assert_eq!(1026, std::mem::size_of_val(&move_before_yield_with_noop()));
75 assert_eq!(2051, std::mem::size_of_val(&overlap_move_points()));
76 assert_eq!(1026, std::mem::size_of_val(&overlap_x_and_y()));
77 }