Skip to content

Commit 4d1d728

Browse files
committed
Add size information
1 parent 30a566d commit 4d1d728

File tree

3 files changed

+37
-0
lines changed

3 files changed

+37
-0
lines changed

compiler/rustc_const_eval/src/const_eval/type_info.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,23 @@ impl<'tcx> InterpCx<'tcx, CompileTimeMachine<'tcx>> {
8181
};
8282
self.write_discriminant(variant_index, &field_dest)?
8383
}
84+
sym::size => {
85+
let layout = self.layout_of(ty)?;
86+
let variant_index = if layout.is_sized() {
87+
let (variant, variant_place) = downcast(sym::Some)?;
88+
let size_field_place =
89+
self.project_field(&variant_place, FieldIdx::ZERO)?;
90+
self.write_scalar(
91+
ScalarInt::try_from_target_usize(layout.size.bytes(), self.tcx.tcx)
92+
.unwrap(),
93+
&size_field_place,
94+
)?;
95+
variant
96+
} else {
97+
downcast(sym::None)?.0
98+
};
99+
self.write_discriminant(variant_index, &field_dest)?;
100+
}
84101
other => span_bug!(self.tcx.span, "unknown `Type` field {other}"),
85102
}
86103
}

library/core/src/mem/type_info.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ use crate::any::TypeId;
1111
pub struct Type {
1212
/// Per-type information
1313
pub kind: TypeKind,
14+
/// Size of the type. `None` if it is unsized
15+
pub size: Option<usize>,
1416
}
1517

1618
/// Compute the type information of a concrete type.

tests/ui/reflection/dump.run.stdout

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,19 +17,37 @@ Type {
1717
],
1818
},
1919
),
20+
size: Some(
21+
2,
22+
),
2023
}
2124
Type {
2225
kind: Other,
26+
size: Some(
27+
4,
28+
),
2329
}
2430
Type {
2531
kind: Other,
32+
size: Some(
33+
24,
34+
),
2635
}
2736
Type {
2837
kind: Other,
38+
size: Some(
39+
16,
40+
),
2941
}
3042
Type {
3143
kind: Other,
44+
size: Some(
45+
16,
46+
),
3247
}
3348
Type {
3449
kind: Other,
50+
size: Some(
51+
16,
52+
),
3553
}

0 commit comments

Comments
 (0)