Skip to content

Commit b45b948

Browse files
Compute generator sizes with -Zprint_type_sizes
1 parent 023b513 commit b45b948

File tree

6 files changed

+74
-1
lines changed

6 files changed

+74
-1
lines changed

compiler/rustc_session/src/code_stats.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ pub enum DataTypeKind {
3333
Union,
3434
Enum,
3535
Closure,
36+
Generator,
3637
}
3738

3839
#[derive(PartialEq, Eq, Hash, Debug)]
@@ -113,7 +114,7 @@ impl CodeStats {
113114
let mut max_variant_size = discr_size;
114115

115116
let struct_like = match kind {
116-
DataTypeKind::Struct | DataTypeKind::Closure => true,
117+
DataTypeKind::Struct | DataTypeKind::Closure | DataTypeKind::Generator => true,
117118
DataTypeKind::Enum | DataTypeKind::Union => false,
118119
};
119120
for (i, variant_info) in variants.into_iter().enumerate() {

compiler/rustc_ty_utils/src/layout.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -826,6 +826,12 @@ fn record_layout_for_printing_outlined<'tcx>(
826826
return;
827827
}
828828

829+
ty::Generator(..) => {
830+
debug!("print-type-size t: `{:?}` record generator", layout.ty);
831+
record(DataTypeKind::Generator, false, None, vec![]);
832+
return;
833+
}
834+
829835
_ => {
830836
debug!("print-type-size t: `{:?}` skip non-nominal", layout.ty);
831837
return;

src/test/ui/print_type_sizes/async.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// compile-flags: -Z print-type-sizes
2+
// edition:2021
3+
// build-pass
4+
// ignore-pass
5+
6+
#![feature(start)]
7+
8+
async fn wait() {}
9+
10+
async fn test(arg: [u8; 8192]) {
11+
wait().await;
12+
drop(arg);
13+
}
14+
15+
#[start]
16+
fn start(_: isize, _: *const *const u8) -> isize {
17+
let _ = test([0; 8192]);
18+
0
19+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
print-type-size type: `[static generator@$DIR/async.rs:10:32: 13:2]`: 16386 bytes, alignment: 1 bytes
2+
print-type-size end padding: 16386 bytes
3+
print-type-size type: `std::future::from_generator::GenFuture<[static generator@$DIR/async.rs:10:32: 13:2]>`: 16386 bytes, alignment: 1 bytes
4+
print-type-size field `.0`: 16386 bytes
5+
print-type-size type: `std::mem::ManuallyDrop<[u8; 8192]>`: 8192 bytes, alignment: 1 bytes
6+
print-type-size field `.value`: 8192 bytes
7+
print-type-size type: `std::mem::MaybeUninit<[u8; 8192]>`: 8192 bytes, alignment: 1 bytes
8+
print-type-size variant `MaybeUninit`: 8192 bytes
9+
print-type-size field `.uninit`: 0 bytes
10+
print-type-size field `.value`: 8192 bytes
11+
print-type-size type: `[static generator@$DIR/async.rs:8:17: 8:19]`: 1 bytes, alignment: 1 bytes
12+
print-type-size end padding: 1 bytes
13+
print-type-size type: `std::future::from_generator::GenFuture<[static generator@$DIR/async.rs:8:17: 8:19]>`: 1 bytes, alignment: 1 bytes
14+
print-type-size field `.0`: 1 bytes
15+
print-type-size type: `std::mem::ManuallyDrop<std::future::from_generator::GenFuture<[static generator@$DIR/async.rs:8:17: 8:19]>>`: 1 bytes, alignment: 1 bytes
16+
print-type-size field `.value`: 1 bytes
17+
print-type-size type: `std::mem::MaybeUninit<std::future::from_generator::GenFuture<[static generator@$DIR/async.rs:8:17: 8:19]>>`: 1 bytes, alignment: 1 bytes
18+
print-type-size variant `MaybeUninit`: 1 bytes
19+
print-type-size field `.uninit`: 0 bytes
20+
print-type-size field `.value`: 1 bytes
21+
print-type-size type: `std::task::Poll<()>`: 1 bytes, alignment: 1 bytes
22+
print-type-size discriminant: 1 bytes
23+
print-type-size variant `Ready`: 0 bytes
24+
print-type-size field `.0`: 0 bytes
25+
print-type-size variant `Pending`: 0 bytes
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// compile-flags: -Z print-type-sizes
2+
// build-pass
3+
// ignore-pass
4+
5+
#![feature(start, generators, generator_trait)]
6+
7+
use std::ops::Generator;
8+
9+
fn generator<const C: usize>(array: [u8; C]) -> impl Generator<Yield = (), Return = ()> {
10+
move |()| {
11+
yield ();
12+
let _ = array;
13+
}
14+
}
15+
16+
#[start]
17+
fn start(_: isize, _: *const *const u8) -> isize {
18+
let _ = generator([0; 8192]);
19+
0
20+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
print-type-size type: `[generator@$DIR/generator.rs:10:5: 10:14]`: 8193 bytes, alignment: 1 bytes
2+
print-type-size end padding: 8193 bytes

0 commit comments

Comments
 (0)