Skip to content

Commit a509588

Browse files
committed
feat: support slices in reflection type info
1 parent daa90b9 commit a509588

File tree

6 files changed

+59
-3
lines changed

6 files changed

+59
-3
lines changed

‎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
@@ -124,7 +132,6 @@ impl<'tcx> InterpCx<'tcx, CompileTimeMachine<'tcx>> {
124132
ty::Adt(_, _)
125133
| ty::Foreign(_)
126134
| ty::Pat(_, _)
127-
| ty::Slice(_)
128135
| ty::FnDef(..)
129136
| ty::FnPtr(..)
130137
| ty::UnsafeBinder(..)
@@ -254,6 +261,27 @@ impl<'tcx> InterpCx<'tcx, CompileTimeMachine<'tcx>> {
254261
interp_ok(())
255262
}
256263

264+
pub(crate) fn write_slice_type_info(
265+
&mut self,
266+
place: impl Writeable<'tcx, CtfeProvenance>,
267+
ty: Ty<'tcx>,
268+
) -> InterpResult<'tcx> {
269+
// Iterate over all fields of `type_info::Slice`.
270+
for (field_idx, field) in
271+
place.layout().ty.ty_adt_def().unwrap().non_enum_variant().fields.iter_enumerated()
272+
{
273+
let field_place = self.project_field(&place, field_idx)?;
274+
275+
match field.name {
276+
// Write the `TypeId` of the slice's elements to the `element_ty` field.
277+
sym::element_ty => self.write_type_id(ty, &field_place)?,
278+
other => span_bug!(self.tcx.def_span(field.did), "unimplemented field {other}"),
279+
}
280+
}
281+
282+
interp_ok(())
283+
}
284+
257285
fn write_int_type_info(
258286
&mut self,
259287
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.
@@ -94,6 +96,15 @@ pub struct Array {
9496
pub len: usize,
9597
}
9698

99+
/// Compile-time type information about slices.
100+
#[derive(Debug)]
101+
#[non_exhaustive]
102+
#[unstable(feature = "type_info", issue = "146922")]
103+
pub struct Slice {
104+
/// The type of each element in the slice.
105+
pub element_ty: TypeId,
106+
}
107+
97108
/// Compile-time type information about `bool`.
98109
#[derive(Debug)]
99110
#[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.bit32.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 {

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