]>
Commit | Line | Data |
---|---|---|
e74abb32 XL |
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 | // run-pass | |
9 | ||
10 | // edition:2018 | |
11 | ||
12 | #![allow(unused_variables, unused_assignments)] | |
13 | ||
14 | use std::future::Future; | |
15 | use std::pin::Pin; | |
16 | use std::task::{Context, Poll}; | |
17 | ||
18 | const BIG_FUT_SIZE: usize = 1024; | |
19 | struct Big([u8; BIG_FUT_SIZE]); | |
20 | ||
21 | impl Big { | |
22 | fn new() -> Self { | |
23 | Big([0; BIG_FUT_SIZE]) | |
24 | } | |
25 | } | |
26 | ||
27 | impl Drop for Big { | |
28 | fn drop(&mut self) {} | |
29 | } | |
30 | ||
31 | #[allow(dead_code)] | |
32 | struct Joiner { | |
33 | a: Option<Big>, | |
34 | b: Option<Big>, | |
35 | c: Option<Big>, | |
36 | } | |
37 | ||
38 | impl Future for Joiner { | |
39 | type Output = (); | |
40 | ||
41 | fn poll(self: Pin<&mut Self>, _ctx: &mut Context<'_>) -> Poll<Self::Output> { | |
42 | Poll::Ready(()) | |
43 | } | |
44 | } | |
45 | ||
46 | fn noop() {} | |
47 | async fn fut() {} | |
48 | ||
49 | async fn single() { | |
50 | let x; | |
51 | fut().await; | |
52 | x = Big::new(); | |
53 | } | |
54 | ||
55 | async fn single_with_noop() { | |
56 | let x; | |
57 | fut().await; | |
58 | noop(); | |
59 | x = Big::new(); | |
60 | noop(); | |
61 | } | |
62 | ||
63 | async fn joined() { | |
64 | let joiner; | |
65 | let a = Big::new(); | |
66 | let b = Big::new(); | |
67 | let c = Big::new(); | |
68 | ||
69 | fut().await; | |
70 | noop(); | |
71 | joiner = Joiner { a: Some(a), b: Some(b), c: Some(c) }; | |
72 | noop(); | |
73 | } | |
74 | ||
75 | async fn joined_with_noop() { | |
76 | let joiner; | |
77 | let a = Big::new(); | |
78 | let b = Big::new(); | |
79 | let c = Big::new(); | |
80 | ||
81 | fut().await; | |
82 | noop(); | |
83 | joiner = Joiner { a: Some(a), b: Some(b), c: Some(c) }; | |
84 | noop(); | |
85 | } | |
86 | ||
87 | async fn join_retval() -> Joiner { | |
88 | let a = Big::new(); | |
89 | let b = Big::new(); | |
90 | let c = Big::new(); | |
91 | ||
92 | fut().await; | |
93 | noop(); | |
94 | Joiner { a: Some(a), b: Some(b), c: Some(c) } | |
95 | } | |
96 | ||
97 | fn main() { | |
ba9703b0 XL |
98 | assert_eq!(2, std::mem::size_of_val(&single())); |
99 | assert_eq!(3, std::mem::size_of_val(&single_with_noop())); | |
100 | assert_eq!(3078, std::mem::size_of_val(&joined())); | |
101 | assert_eq!(3078, std::mem::size_of_val(&joined_with_noop())); | |
102 | assert_eq!(3074, std::mem::size_of_val(&join_retval())); | |
e74abb32 | 103 | } |