Skip to content

Commit aab0141

Browse files
committed
Add size information
1 parent a0cfabc commit aab0141

File tree

3 files changed

+21
-0
lines changed

3 files changed

+21
-0
lines changed

compiler/rustc_const_eval/src/const_eval/type_info.rs

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

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)