Skip to content

Commit 11b09e7

Browse files
committed
Add size tests for moved locals in generators
1 parent 9969417 commit 11b09e7

File tree

2 files changed

+146
-0
lines changed

2 files changed

+146
-0
lines changed
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
// Test that we don't duplicate storage for futures moved around in .await, and
2+
// for futures moved into other futures.
3+
//
4+
// The exact sizes can change by a few bytes (we'd like to know when they do).
5+
// What we don't want to see is the wrong multiple of 1024 (the size of BigFut)
6+
// being reflected in the size.
7+
//
8+
// See issue #59123 for a full explanation.
9+
10+
// edition:2018
11+
12+
#![feature(async_await)]
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 BigFut([u8; BIG_FUT_SIZE]);
20+
21+
impl BigFut {
22+
fn new() -> Self {
23+
BigFut([0; BIG_FUT_SIZE])
24+
} }
25+
26+
impl Drop for BigFut {
27+
fn drop(&mut self) {}
28+
}
29+
30+
impl Future for BigFut {
31+
type Output = ();
32+
33+
fn poll(self: Pin<&mut Self>, _ctx: &mut Context<'_>) -> Poll<Self::Output> {
34+
Poll::Ready(())
35+
}
36+
}
37+
38+
#[allow(dead_code)]
39+
struct Joiner {
40+
a: Option<BigFut>,
41+
b: Option<BigFut>,
42+
c: Option<BigFut>,
43+
}
44+
45+
impl Future for Joiner {
46+
type Output = ();
47+
48+
fn poll(self: Pin<&mut Self>, _ctx: &mut Context<'_>) -> Poll<Self::Output> {
49+
Poll::Ready(())
50+
}
51+
}
52+
53+
fn noop() {}
54+
55+
async fn single() {
56+
let x = BigFut::new();
57+
x.await;
58+
}
59+
60+
async fn single_with_noop() {
61+
let x = BigFut::new();
62+
noop();
63+
x.await;
64+
}
65+
66+
async fn joined() {
67+
let a = BigFut::new();
68+
let b = BigFut::new();
69+
let c = BigFut::new();
70+
71+
let joiner = Joiner {
72+
a: Some(a),
73+
b: Some(b),
74+
c: Some(c),
75+
};
76+
joiner.await
77+
}
78+
79+
async fn joined_with_noop() {
80+
let a = BigFut::new();
81+
let b = BigFut::new();
82+
let c = BigFut::new();
83+
84+
let joiner = Joiner {
85+
a: Some(a),
86+
b: Some(b),
87+
c: Some(c),
88+
};
89+
noop();
90+
joiner.await
91+
}
92+
93+
fn main() {
94+
assert_eq!(1028, std::mem::size_of_val(&single()));
95+
assert_eq!(1032, std::mem::size_of_val(&single_with_noop()));
96+
assert_eq!(3084, std::mem::size_of_val(&joined()));
97+
assert_eq!(3084, std::mem::size_of_val(&joined_with_noop()));
98+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
// Test that we don't duplicate storage for a variable that is moved to another
2+
// binding. This used to happen in the presence of unwind and drop edges (see
3+
// `complex` below.)
4+
//
5+
// The exact sizes here can change (we'd like to know when they do). What we
6+
// don't want to see is the `complex` generator size being upwards of 2048 bytes
7+
// (which would indicate it is reserving space for two copies of Foo.)
8+
//
9+
// See issue #59123 for a full explanation.
10+
11+
// edition:2018
12+
13+
#![feature(generators, generator_trait)]
14+
15+
use std::ops::Generator;
16+
17+
const FOO_SIZE: usize = 1024;
18+
struct Foo([u8; FOO_SIZE]);
19+
20+
impl Drop for Foo {
21+
fn drop(&mut self) {}
22+
}
23+
24+
fn simple() -> impl Generator<Yield = (), Return = ()> {
25+
static || {
26+
let first = Foo([0; FOO_SIZE]);
27+
let _second = first;
28+
yield;
29+
// _second dropped here
30+
}
31+
}
32+
33+
fn noop() {}
34+
35+
fn complex() -> impl Generator<Yield = (), Return = ()> {
36+
static || {
37+
let first = Foo([0; FOO_SIZE]);
38+
noop();
39+
let _second = first;
40+
yield;
41+
// _second dropped here
42+
}
43+
}
44+
45+
fn main() {
46+
assert_eq!(1028, std::mem::size_of_val(&simple()));
47+
assert_eq!(1032, std::mem::size_of_val(&complex()));
48+
}

0 commit comments

Comments
 (0)