]> git.lizzy.rs Git - rust.git/blob - tests/ui/async-await/async-fn-size-uninit-locals.rs
Rollup merge of #106836 - ibraheemdev:sync-sender-spin, r=Amanieu
[rust.git] / tests / ui / async-await / async-fn-size-uninit-locals.rs
1 // Test that we don't store uninitialized locals in futures from `async fn`.
2 //
3 // The exact sizes can change by a few bytes (we'd like to know when they do).
4 // What we don't want to see is the wrong multiple of 1024 (the size of `Big`)
5 // being reflected in the size.
6
7 // ignore-emscripten (sizes don't match)
8 // needs-unwind Size of Futures change on panic=abort
9 // run-pass
10
11 // edition:2018
12
13 #![allow(unused_variables, unused_assignments)]
14
15 use std::future::Future;
16 use std::pin::Pin;
17 use std::task::{Context, Poll};
18
19 const BIG_FUT_SIZE: usize = 1024;
20 struct Big(#[allow(unused_tuple_struct_fields)] [u8; BIG_FUT_SIZE]);
21
22 impl Big {
23     fn new() -> Self {
24         Big([0; BIG_FUT_SIZE])
25     }
26 }
27
28 impl Drop for Big {
29     fn drop(&mut self) {}
30 }
31
32 #[allow(dead_code)]
33 struct Joiner {
34     a: Option<Big>,
35     b: Option<Big>,
36     c: Option<Big>,
37 }
38
39 impl Future for Joiner {
40     type Output = ();
41
42     fn poll(self: Pin<&mut Self>, _ctx: &mut Context<'_>) -> Poll<Self::Output> {
43         Poll::Ready(())
44     }
45 }
46
47 fn noop() {}
48 async fn fut() {}
49
50 async fn single() {
51     let x;
52     fut().await;
53     x = Big::new();
54 }
55
56 async fn single_with_noop() {
57     let x;
58     fut().await;
59     noop();
60     x = Big::new();
61     noop();
62 }
63
64 async fn joined() {
65     let joiner;
66     let a = Big::new();
67     let b = Big::new();
68     let c = Big::new();
69
70     fut().await;
71     joiner = Joiner { a: Some(a), b: Some(b), c: Some(c) };
72 }
73
74 async fn joined_with_noop() {
75     let joiner;
76     let a = Big::new();
77     let b = Big::new();
78     let c = Big::new();
79
80     fut().await;
81     noop();
82     joiner = Joiner { a: Some(a), b: Some(b), c: Some(c) };
83     noop();
84 }
85
86 async fn join_retval() -> Joiner {
87     let a = Big::new();
88     let b = Big::new();
89     let c = Big::new();
90
91     fut().await;
92     noop();
93     Joiner { a: Some(a), b: Some(b), c: Some(c) }
94 }
95
96 fn main() {
97     assert_eq!(2, std::mem::size_of_val(&single()));
98     assert_eq!(3, std::mem::size_of_val(&single_with_noop()));
99     assert_eq!(3074, std::mem::size_of_val(&joined()));
100     assert_eq!(3078, std::mem::size_of_val(&joined_with_noop()));
101     assert_eq!(3074, std::mem::size_of_val(&join_retval()));
102 }