Skip to content

Commit ce6ca4a

Browse files
committed
feat: support slices in reflection type info
1 parent 9b37157 commit ce6ca4a

5 files changed

Lines changed: 54 additions & 2 deletions

File tree

compiler/rustc_const_eval/src/const_eval/type_info.rs

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,14 @@ impl<'tcx> InterpCx<'tcx, CompileTimeMachine<'tcx>> {
6666

6767
variant
6868
}
69+
ty::Slice(ty) => {
70+
let (variant, variant_place) = downcast(sym::Slice)?;
71+
let slice_place = self.project_field(&variant_place, FieldIdx::ZERO)?;
72+
73+
self.write_slice_type_info(slice_place, *ty)?;
74+
75+
variant
76+
}
6977
ty::Bool => {
7078
let (variant, _variant_place) = downcast(sym::Bool)?;
7179
variant
@@ -115,7 +123,6 @@ impl<'tcx> InterpCx<'tcx, CompileTimeMachine<'tcx>> {
115123
ty::Adt(_, _)
116124
| ty::Foreign(_)
117125
| ty::Pat(_, _)
118-
| ty::Slice(_)
119126
| ty::RawPtr(..)
120127
| ty::FnDef(..)
121128
| ty::FnPtr(..)
@@ -246,6 +253,27 @@ impl<'tcx> InterpCx<'tcx, CompileTimeMachine<'tcx>> {
246253
interp_ok(())
247254
}
248255

256+
pub(crate) fn write_slice_type_info(
257+
&mut self,
258+
place: impl Writeable<'tcx, CtfeProvenance>,
259+
ty: Ty<'tcx>,
260+
) -> InterpResult<'tcx> {
261+
// Iterate over all fields of `type_info::Slice`.
262+
for (field_idx, field) in
263+
place.layout().ty.ty_adt_def().unwrap().non_enum_variant().fields.iter_enumerated()
264+
{
265+
let field_place = self.project_field(&place, field_idx)?;
266+
267+
match field.name {
268+
// Write the `TypeId` of the slice's elements to the `element_ty` field.
269+
sym::element_ty => self.write_type_id(ty, &field_place)?,
270+
other => span_bug!(self.tcx.def_span(field.did), "unimplemented field {other}"),
271+
}
272+
}
273+
274+
interp_ok(())
275+
}
276+
249277
fn write_int_type_info(
250278
&mut self,
251279
place: impl Writeable<'tcx, CtfeProvenance>,

compiler/rustc_span/src/symbol.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -362,6 +362,7 @@ symbols! {
362362
Send,
363363
SeqCst,
364364
Sized,
365+
Slice,
365366
SliceIndex,
366367
SliceIter,
367368
Some,

library/core/src/mem/type_info.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ pub enum TypeKind {
4545
Tuple(Tuple),
4646
/// Arrays.
4747
Array(Array),
48+
/// Slices.
49+
Slice(Slice),
4850
/// Primitive boolean type.
4951
Bool(Bool),
5052
/// Primitive character type.
@@ -92,6 +94,15 @@ pub struct Array {
9294
pub len: usize,
9395
}
9496

97+
/// Compile-time type information about slices.
98+
#[derive(Debug)]
99+
#[non_exhaustive]
100+
#[unstable(feature = "type_info", issue = "146922")]
101+
pub struct Slice {
102+
/// The type of each element in the slice.
103+
pub element_ty: TypeId,
104+
}
105+
95106
/// Compile-time type information about `bool`.
96107
#[derive(Debug)]
97108
#[non_exhaustive]

library/coretests/tests/mem/type_info.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,14 @@ fn test_arrays() {
2222
}
2323
}
2424

25+
#[test]
26+
fn test_slices() {
27+
match const { Type::of::<[usize]>() }.kind {
28+
TypeKind::Slice(slice) => assert_eq!(slice.element_ty, TypeId::of::<usize>()),
29+
_ => unreachable!(),
30+
}
31+
}
32+
2533
#[test]
2634
fn test_tuples() {
2735
fn assert_tuple_arity<T: 'static, const N: usize>() {

tests/ui/reflection/dump.bit64.run.stdout

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,11 @@ Type {
194194
size: None,
195195
}
196196
Type {
197-
kind: Other,
197+
kind: Slice(
198+
Slice {
199+
element_ty: TypeId(0x0596b48cc04376e64d5c788c2aa46bdb),
200+
},
201+
),
198202
size: None,
199203
}
200204
Type {

0 commit comments

Comments
 (0)