From 3c43eb050a52a109df8d307f36048274cd2ef696 Mon Sep 17 00:00:00 2001 From: Yara Date: Wed, 19 Aug 2026 16:29:59 +0200 Subject: [PATCH 1/2] reflection: remove fields from TypeKind::Int and Float These fields served to query the bitwidth for both and whether an integer is signed or not. The bitwith is now provided by `TypeId::bits` while the signedness can be gotten trough the earlier introduces `TypeId::is_signed`. --- .../src/const_eval/type_info.rs | 69 ++----------------- library/core/src/mem/type_info.rs | 24 +------ library/coretests/tests/mem/type_info.rs | 23 +++---- 3 files changed, 17 insertions(+), 99 deletions(-) diff --git a/compiler/rustc_const_eval/src/const_eval/type_info.rs b/compiler/rustc_const_eval/src/const_eval/type_info.rs index d77e43b9858af..f6e0208d98835 100644 --- a/compiler/rustc_const_eval/src/const_eval/type_info.rs +++ b/compiler/rustc_const_eval/src/const_eval/type_info.rs @@ -61,7 +61,6 @@ impl<'tcx> InterpCx<'tcx, CompileTimeMachine<'tcx>> { // Fill all fields of the `TypeInfo` struct. for (idx, field) in ty_struct.fields.iter_enumerated() { let field_dest = self.project_field(dest, idx)?; - let ptr_bit_width = || self.tcx.data_layout.pointer_size().bits(); match field.name { sym::kind => { let variant_index = match ty.kind() { @@ -115,33 +114,19 @@ impl<'tcx> InterpCx<'tcx, CompileTimeMachine<'tcx>> { self.project_downcast_named(&field_dest, sym::Char)?; variant } - ty::Int(int_ty) => { - let (variant, variant_place) = + ty::Int(_) => { + let (variant, _variant_place) = self.project_downcast_named(&field_dest, sym::Int)?; - let place = self.project_field(&variant_place, FieldIdx::ZERO)?; - self.write_int_type_info( - place, - int_ty.bit_width().unwrap_or_else(/* isize */ ptr_bit_width), - true, - )?; variant } - ty::Uint(uint_ty) => { - let (variant, variant_place) = + ty::Uint(_) => { + let (variant, _variant_place) = self.project_downcast_named(&field_dest, sym::Int)?; - let place = self.project_field(&variant_place, FieldIdx::ZERO)?; - self.write_int_type_info( - place, - uint_ty.bit_width().unwrap_or_else(/* usize */ ptr_bit_width), - false, - )?; variant } - ty::Float(float_ty) => { - let (variant, variant_place) = + ty::Float(_) => { + let (variant, _variant_place) = self.project_downcast_named(&field_dest, sym::Float)?; - let place = self.project_field(&variant_place, FieldIdx::ZERO)?; - self.write_float_type_info(place, float_ty.bit_width())?; variant } ty::Str => { @@ -316,48 +301,6 @@ impl<'tcx> InterpCx<'tcx, CompileTimeMachine<'tcx>> { interp_ok(()) } - fn write_int_type_info( - &mut self, - place: impl Writeable<'tcx, CtfeProvenance>, - bit_width: u64, - signed: bool, - ) -> InterpResult<'tcx> { - for (field_idx, field) in - place.layout().ty.ty_adt_def().unwrap().non_enum_variant().fields.iter_enumerated() - { - let field_place = self.project_field(&place, field_idx)?; - match field.name { - sym::bits => self.write_scalar( - Scalar::from_u32(bit_width.try_into().expect("bit_width overflowed")), - &field_place, - )?, - sym::signed => self.write_scalar(Scalar::from_bool(signed), &field_place)?, - other => span_bug!(self.tcx.def_span(field.did), "unimplemented field {other}"), - } - } - interp_ok(()) - } - - fn write_float_type_info( - &mut self, - place: impl Writeable<'tcx, CtfeProvenance>, - bit_width: u64, - ) -> InterpResult<'tcx> { - for (field_idx, field) in - place.layout().ty.ty_adt_def().unwrap().non_enum_variant().fields.iter_enumerated() - { - let field_place = self.project_field(&place, field_idx)?; - match field.name { - sym::bits => self.write_scalar( - Scalar::from_u32(bit_width.try_into().expect("bit_width overflowed")), - &field_place, - )?, - other => span_bug!(self.tcx.def_span(field.did), "unimplemented field {other}"), - } - } - interp_ok(()) - } - pub(crate) fn write_reference_type_info( &mut self, place: impl Writeable<'tcx, CtfeProvenance>, diff --git a/library/core/src/mem/type_info.rs b/library/core/src/mem/type_info.rs index 2f38f9b77faf4..02737d393be18 100644 --- a/library/core/src/mem/type_info.rs +++ b/library/core/src/mem/type_info.rs @@ -94,9 +94,9 @@ pub enum TypeKind { /// Primitive character type. Char(Char), /// Primitive signed and unsigned integer type. - Int(Int), + Int, /// Primitive floating-point type. - Float(Float), + Float, /// String slice type. Str(Str), /// References. @@ -215,26 +215,6 @@ pub struct Char { // No additional information to provide for now. } -/// Compile-time type information about signed and unsigned integer types. -#[derive(Debug)] -#[non_exhaustive] -#[unstable(feature = "type_info", issue = "146922")] -pub struct Int { - /// The bit width of the signed integer type. - pub bits: u32, - /// Whether the integer type is signed. - pub signed: bool, -} - -/// Compile-time type information about floating-point types. -#[derive(Debug)] -#[non_exhaustive] -#[unstable(feature = "type_info", issue = "146922")] -pub struct Float { - /// The bit width of the floating-point type. - pub bits: u32, -} - /// Compile-time type information about string slice types. #[derive(Debug)] #[non_exhaustive] diff --git a/library/coretests/tests/mem/type_info.rs b/library/coretests/tests/mem/type_info.rs index 74be53c57b46b..92e6d47ef6047 100644 --- a/library/coretests/tests/mem/type_info.rs +++ b/library/coretests/tests/mem/type_info.rs @@ -233,36 +233,31 @@ fn test_primitives() { assert!(ty_id.size() == Some(size_of::())); assert!(ty_id.variants() == 1); - let Type { kind: Int(ty), .. } = (const { Type::of::() }) else { panic!() }; - assert!(ty.bits == 32); - assert!(ty.signed); + let Type { kind: Int, .. } = (const { Type::of::() }) else { panic!() }; let ty_id = TypeId::of::(); + assert!(ty_id.is_signed()); assert!(ty_id.size() == Some(size_of::())); assert!(ty_id.variants() == 1); - let Type { kind: Int(ty), .. } = (const { Type::of::() }) else { panic!() }; - assert!(ty.bits as usize == size_of::() * 8); - assert!(ty.signed); + let Type { kind: Int, .. } = (const { Type::of::() }) else { panic!() }; let ty_id = TypeId::of::(); + assert!(ty_id.is_signed()); assert!(ty_id.size() == Some(size_of::())); assert!(ty_id.variants() == 1); - let Type { kind: Int(ty), .. } = (const { Type::of::() }) else { panic!() }; - assert!(ty.bits == 32); - assert!(!ty.signed); + let Type { kind: Int, .. } = (const { Type::of::() }) else { panic!() }; let ty_id = TypeId::of::(); + assert!(!ty_id.is_signed()); assert!(ty_id.size() == Some(size_of::())); assert!(ty_id.variants() == 1); - let Type { kind: Int(ty), .. } = (const { Type::of::() }) else { panic!() }; - assert!(ty.bits as usize == size_of::() * 8); - assert!(!ty.signed); + let Type { kind: Int, .. } = (const { Type::of::() }) else { panic!() }; let ty_id = TypeId::of::(); + assert!(!ty_id.is_signed()); assert!(ty_id.size() == Some(size_of::())); assert!(ty_id.variants() == 1); - let Type { kind: Float(ty), .. } = (const { Type::of::() }) else { panic!() }; - assert!(ty.bits == 32); + let Type { kind: Float, .. } = (const { Type::of::() }) else { panic!() }; let ty_id = TypeId::of::(); assert!(ty_id.size() == Some(size_of::())); assert!(ty_id.variants() == 1); From 1d2a65f3f7d0825097c281386d366406516fe851 Mon Sep 17 00:00:00 2001 From: Yara Date: Wed, 19 Aug 2026 16:29:59 +0200 Subject: [PATCH 2/2] reflection: remove field from TypeKind::Bool and Char --- library/core/src/mem/type_info.rs | 20 ++---------------- library/coretests/tests/mem/type_info.rs | 4 ++-- tests/ui/lint/recommend-literal.rs | 3 +-- tests/ui/lint/recommend-literal.stderr | 27 ++++++++++++------------ 4 files changed, 19 insertions(+), 35 deletions(-) diff --git a/library/core/src/mem/type_info.rs b/library/core/src/mem/type_info.rs index 02737d393be18..111664775ca8d 100644 --- a/library/core/src/mem/type_info.rs +++ b/library/core/src/mem/type_info.rs @@ -90,9 +90,9 @@ pub enum TypeKind { /// Unions. Union, /// Primitive boolean type. - Bool(Bool), + Bool, /// Primitive character type. - Char(Char), + Char, /// Primitive signed and unsigned integer type. Int, /// Primitive floating-point type. @@ -199,22 +199,6 @@ pub struct Const { pub ty: TypeId, } -/// Compile-time type information about `bool`. -#[derive(Debug)] -#[non_exhaustive] -#[unstable(feature = "type_info", issue = "146922")] -pub struct Bool { - // No additional information to provide for now. -} - -/// Compile-time type information about `char`. -#[derive(Debug)] -#[non_exhaustive] -#[unstable(feature = "type_info", issue = "146922")] -pub struct Char { - // No additional information to provide for now. -} - /// Compile-time type information about string slice types. #[derive(Debug)] #[non_exhaustive] diff --git a/library/coretests/tests/mem/type_info.rs b/library/coretests/tests/mem/type_info.rs index 92e6d47ef6047..f3a69dd857aba 100644 --- a/library/coretests/tests/mem/type_info.rs +++ b/library/coretests/tests/mem/type_info.rs @@ -223,12 +223,12 @@ fn test_primitives() { use TypeKind::*; const { - let Type { kind: Bool(_ty), .. } = (const { Type::of::() }) else { panic!() }; + let Type { kind: Bool, .. } = (const { Type::of::() }) else { panic!() }; let ty_id = TypeId::of::(); assert!(ty_id.size() == Some(size_of::())); assert!(ty_id.variants() == 1); - let Type { kind: Char(_ty), .. } = (const { Type::of::() }) else { panic!() }; + let Type { kind: Char, .. } = (const { Type::of::() }) else { panic!() }; let ty_id = TypeId::of::(); assert!(ty_id.size() == Some(size_of::())); assert!(ty_id.variants() == 1); diff --git a/tests/ui/lint/recommend-literal.rs b/tests/ui/lint/recommend-literal.rs index be074c1114532..4eae569df130f 100644 --- a/tests/ui/lint/recommend-literal.rs +++ b/tests/ui/lint/recommend-literal.rs @@ -1,5 +1,3 @@ -//~vv HELP consider importing this struct - type Real = double; //~^ ERROR cannot find type `double` in this scope //~| HELP perhaps you intended to use this type @@ -16,6 +14,7 @@ fn main() { //~^ ERROR: cannot find type `Bool` in this scope [E0425] //~| HELP a builtin type with a similar name exists //~| HELP perhaps you intended to use this type + //~| HELP: there is an enum variant `std::mem::type_info::TypeKind::Bool`; try using the variant's enum } fn z(a: boolean) { diff --git a/tests/ui/lint/recommend-literal.stderr b/tests/ui/lint/recommend-literal.stderr index 01e993df17a98..351760589cd17 100644 --- a/tests/ui/lint/recommend-literal.stderr +++ b/tests/ui/lint/recommend-literal.stderr @@ -1,5 +1,5 @@ error[E0425]: cannot find type `double` in this scope - --> $DIR/recommend-literal.rs:3:13 + --> $DIR/recommend-literal.rs:1:13 | LL | type Real = double; | ^^^^^^ @@ -8,7 +8,7 @@ LL | type Real = double; | help: perhaps you intended to use this type: `f64` error[E0425]: cannot find type `long` in this scope - --> $DIR/recommend-literal.rs:9:12 + --> $DIR/recommend-literal.rs:7:12 | LL | let y: long = 74802374902374923; | ^^^^ @@ -17,7 +17,7 @@ LL | let y: long = 74802374902374923; | help: perhaps you intended to use this type: `i64` error[E0425]: cannot find type `Boolean` in this scope - --> $DIR/recommend-literal.rs:12:13 + --> $DIR/recommend-literal.rs:10:13 | LL | let v1: Boolean = true; | ^^^^^^^ @@ -26,11 +26,16 @@ LL | let v1: Boolean = true; | help: perhaps you intended to use this type: `bool` error[E0425]: cannot find type `Bool` in this scope - --> $DIR/recommend-literal.rs:15:13 + --> $DIR/recommend-literal.rs:13:13 | LL | let v2: Bool = true; | ^^^^ | +help: there is an enum variant `std::mem::type_info::TypeKind::Bool`; try using the variant's enum + | +LL - let v2: Bool = true; +LL + let v2: std::mem::type_info::TypeKind = true; + | help: a builtin type with a similar name exists | LL - let v2: Bool = true; @@ -41,13 +46,9 @@ help: perhaps you intended to use this type LL - let v2: Bool = true; LL + let v2: bool = true; | -help: consider importing this struct - | -LL + use std::mem::type_info::Bool; - | error[E0425]: cannot find type `boolean` in this scope - --> $DIR/recommend-literal.rs:21:9 + --> $DIR/recommend-literal.rs:20:9 | LL | fn z(a: boolean) { | ^^^^^^^ @@ -56,7 +57,7 @@ LL | fn z(a: boolean) { | help: perhaps you intended to use this type: `bool` error[E0425]: cannot find type `byte` in this scope - --> $DIR/recommend-literal.rs:26:11 + --> $DIR/recommend-literal.rs:25:11 | LL | fn a() -> byte { | ^^^^ @@ -65,7 +66,7 @@ LL | fn a() -> byte { | help: perhaps you intended to use this type: `u8` error[E0425]: cannot find type `float` in this scope - --> $DIR/recommend-literal.rs:33:12 + --> $DIR/recommend-literal.rs:32:12 | LL | width: float, | ^^^^^ @@ -74,7 +75,7 @@ LL | width: float, | help: perhaps you intended to use this type: `f32` error[E0425]: cannot find type `int` in this scope - --> $DIR/recommend-literal.rs:36:19 + --> $DIR/recommend-literal.rs:35:19 | LL | depth: Option, | ^^^ not found in this scope @@ -90,7 +91,7 @@ LL | struct Data { | +++++ error[E0425]: cannot find type `short` in this scope - --> $DIR/recommend-literal.rs:42:16 + --> $DIR/recommend-literal.rs:41:16 | LL | impl Stuff for short {} | ^^^^^