summaryrefslogtreecommitdiffstats
path: root/tests/ui/async-await/async-fn-size-uninit-locals.rs
blob: 5461726935457b6c3688ea4b8ddcb5a1582a68ff (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
// Test that we don't store uninitialized locals in futures from `async fn`.
//
// The exact sizes can change by a few bytes (we'd like to know when they do).
// What we don't want to see is the wrong multiple of 1024 (the size of `Big`)
// being reflected in the size.

// ignore-emscripten (sizes don't match)
// needs-unwind Size of Futures change on panic=abort
// run-pass

// edition:2018

#![allow(unused_variables, unused_assignments)]

use std::future::Future;
use std::pin::Pin;
use std::task::{Context, Poll};

const BIG_FUT_SIZE: usize = 1024;
struct Big(#[allow(unused_tuple_struct_fields)] [u8; BIG_FUT_SIZE]);

impl Big {
    fn new() -> Self {
        Big([0; BIG_FUT_SIZE])
    }
}

impl Drop for Big {
    fn drop(&mut self) {}
}

#[allow(dead_code)]
struct Joiner {
    a: Option<Big>,
    b: Option<Big>,
    c: Option<Big>,
}

impl Future for Joiner {
    type Output = ();

    fn poll(self: Pin<&mut Self>, _ctx: &mut Context<'_>) -> Poll<Self::Output> {
        Poll::Ready(())
    }
}

fn noop() {}
async fn fut() {}

async fn single() {
    let x;
    fut().await;
    x = Big::new();
}

async fn single_with_noop() {
    let x;
    fut().await;
    noop();
    x = Big::new();
    noop();
}

async fn joined() {
    let joiner;
    let a = Big::new();
    let b = Big::new();
    let c = Big::new();

    fut().await;
    joiner = Joiner { a: Some(a), b: Some(b), c: Some(c) };
}

async fn joined_with_noop() {
    let joiner;
    let a = Big::new();
    let b = Big::new();
    let c = Big::new();

    fut().await;
    noop();
    joiner = Joiner { a: Some(a), b: Some(b), c: Some(c) };
    noop();
}

async fn join_retval() -> Joiner {
    let a = Big::new();
    let b = Big::new();
    let c = Big::new();

    fut().await;
    noop();
    Joiner { a: Some(a), b: Some(b), c: Some(c) }
}

fn main() {
    assert_eq!(2, std::mem::size_of_val(&single()));
    assert_eq!(3, std::mem::size_of_val(&single_with_noop()));
    assert_eq!(3074, std::mem::size_of_val(&joined()));
    assert_eq!(3078, std::mem::size_of_val(&joined_with_noop()));
    assert_eq!(3074, std::mem::size_of_val(&join_retval()));
}