Skip to content

Commit 82028b0

Browse files
committed
Add size information
1 parent a3359bd commit 82028b0

2 files changed

Lines changed: 19 additions & 0 deletions

File tree

compiler/rustc_const_eval/src/const_eval/type_info.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,23 @@ impl<'tcx> InterpCx<'tcx, CompileTimeMachine<'tcx>> {
8686
};
8787
self.write_discriminant(variant_index, &field_dest)?
8888
}
89+
sym::size => {
90+
let layout = self.layout_of(ty)?;
91+
let variant_index = if layout.is_sized() {
92+
let (variant, variant_place) = downcast(sym::Some)?;
93+
let size_field_place =
94+
self.project_field(&variant_place, FieldIdx::ZERO)?;
95+
self.write_scalar(
96+
ScalarInt::try_from_target_usize(layout.size.bytes(), self.tcx.tcx)
97+
.unwrap(),
98+
&size_field_place,
99+
)?;
100+
variant
101+
} else {
102+
downcast(sym::None)?.0
103+
};
104+
self.write_discriminant(variant_index, &field_dest)?;
105+
}
89106
other => span_bug!(self.tcx.span, "unknown `Type` field {other}"),
90107
}
91108
}

library/core/src/mem/type_info.rs

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

1719
impl TypeId {

0 commit comments

Comments
 (0)