Add size information

This commit is contained in:
Oli Scherer 2025-03-14 14:39:58 +00:00 committed by Oli Scherer
parent a3359bdd4f
commit 82028b0f9a
2 changed files with 19 additions and 0 deletions

View file

@ -86,6 +86,23 @@ impl<'tcx> InterpCx<'tcx, CompileTimeMachine<'tcx>> {
};
self.write_discriminant(variant_index, &field_dest)?
}
sym::size => {
let layout = self.layout_of(ty)?;
let variant_index = if layout.is_sized() {
let (variant, variant_place) = downcast(sym::Some)?;
let size_field_place =
self.project_field(&variant_place, FieldIdx::ZERO)?;
self.write_scalar(
ScalarInt::try_from_target_usize(layout.size.bytes(), self.tcx.tcx)
.unwrap(),
&size_field_place,
)?;
variant
} else {
downcast(sym::None)?.0
};
self.write_discriminant(variant_index, &field_dest)?;
}
other => span_bug!(self.tcx.span, "unknown `Type` field {other}"),
}
}

View file

@ -12,6 +12,8 @@ use crate::intrinsics::type_of;
pub struct Type {
/// Per-type information
pub kind: TypeKind,
/// Size of the type. `None` if it is unsized
pub size: Option<usize>,
}
impl TypeId {