Skip to content

Commit a77bbe2

Browse files
committed
Add size information
1 parent 49614c2 commit a77bbe2

File tree

3 files changed

+22
-0
lines changed

3 files changed

+22
-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
@@ -85,6 +85,23 @@ impl<'tcx> InterpCx<'tcx, CompileTimeMachine<'tcx>> {
8585
};
8686
self.write_discriminant(variant_index, &field_dest)?
8787
}
88+
sym::size => {
89+
let layout = self.layout_of(ty)?;
90+
let variant_index = if layout.is_sized() {
91+
let (variant, variant_place) = downcast(sym::Some)?;
92+
let size_field_place =
93+
self.project_field(&variant_place, FieldIdx::ZERO)?;
94+
self.write_scalar(
95+
ScalarInt::try_from_target_usize(layout.size.bytes(), self.tcx.tcx)
96+
.unwrap(),
97+
&size_field_place,
98+
)?;
99+
variant
100+
} else {
101+
downcast(sym::None)?.0
102+
};
103+
self.write_discriminant(variant_index, &field_dest)?;
104+
}
88105
other => span_bug!(self.tcx.span, "unknown `Type` field {other}"),
89106
}
90107
}

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: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,7 @@ Type {
1717
],
1818
},
1919
),
20+
size: Some(
21+
2,
22+
),
2023
}

0 commit comments

Comments
 (0)