Skip to content

Commit 5155c5f

Browse files
committed
Add size information
1 parent bad530b commit 5155c5f

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
@@ -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
@@ -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)