From 0ea82de9576a270a99c3bd79ab80d044c3ba9e45 Mon Sep 17 00:00:00 2001 From: Yara Date: Fri, 7 Aug 2026 16:04:30 +0200 Subject: [PATCH] Add `TypeId::is_signed` method --- .../src/const_eval/machine.rs | 5 +++++ .../rustc_hir_analysis/src/check/intrinsic.rs | 2 ++ compiler/rustc_span/src/symbol.rs | 1 + library/core/src/intrinsics/mod.rs | 8 +++++++ library/core/src/mem/type_info.rs | 21 +++++++++++++++++++ 5 files changed, 37 insertions(+) diff --git a/compiler/rustc_const_eval/src/const_eval/machine.rs b/compiler/rustc_const_eval/src/const_eval/machine.rs index 3f52fbecb0950..e27699253dcc0 100644 --- a/compiler/rustc_const_eval/src/const_eval/machine.rs +++ b/compiler/rustc_const_eval/src/const_eval/machine.rs @@ -605,6 +605,11 @@ impl<'tcx> interpret::Machine<'tcx> for CompileTimeMachine<'tcx> { ecx.write_type_info(ty, dest)?; } + sym::type_id_is_signed => { + let ty = ecx.read_type_id(&args[0])?; + ecx.write_scalar(Scalar::from_bool(ty.is_signed()), dest)?; + } + sym::size_of_type_id => { let ty = ecx.read_type_id(&args[0])?; let layout = ecx.layout_of(ty)?; diff --git a/compiler/rustc_hir_analysis/src/check/intrinsic.rs b/compiler/rustc_hir_analysis/src/check/intrinsic.rs index 67fddd87fbb1d..311a68d83f91f 100644 --- a/compiler/rustc_hir_analysis/src/check/intrinsic.rs +++ b/compiler/rustc_hir_analysis/src/check/intrinsic.rs @@ -220,6 +220,7 @@ fn intrinsic_operation_unsafety(tcx: TyCtxt<'_>, intrinsic_id: LocalDefId) -> hi | sym::type_id_field_representing_type | sym::type_id_fields | sym::type_id_generics + | sym::type_id_is_signed | sym::type_id_variants | sym::type_id_vtable | sym::type_name @@ -331,6 +332,7 @@ pub(crate) fn check_intrinsic_type( (0, 0, vec![type_id_ty(), tcx.types.usize, tcx.types.usize], type_id_ty()) } sym::type_id_fields => (0, 0, vec![type_id_ty(), tcx.types.usize], tcx.types.usize), + sym::type_id_is_signed => (0, 0, vec![type_id_ty()], tcx.types.bool), sym::type_id_variants => (0, 0, vec![type_id_ty()], tcx.types.usize), sym::type_id_vtable => { let dyn_metadata = tcx.require_lang_item(LangItem::DynMetadata, span); diff --git a/compiler/rustc_span/src/symbol.rs b/compiler/rustc_span/src/symbol.rs index a453cc6b555db..84f61bb7e0209 100644 --- a/compiler/rustc_span/src/symbol.rs +++ b/compiler/rustc_span/src/symbol.rs @@ -2174,6 +2174,7 @@ symbols! { type_id_field_representing_type, type_id_fields, type_id_generics, + type_id_is_signed, type_id_variants, type_id_vtable, type_info, diff --git a/library/core/src/intrinsics/mod.rs b/library/core/src/intrinsics/mod.rs index fad2cb08a8a64..4e67199bba8c9 100644 --- a/library/core/src/intrinsics/mod.rs +++ b/library/core/src/intrinsics/mod.rs @@ -3128,6 +3128,14 @@ pub const fn type_id_eq(a: crate::any::TypeId, b: crate::any::TypeId) -> bool { unsafe { crate::mem::transmute::<_, u128>(a) == crate::mem::transmute::<_, u128>(b) } } +/// Returns whether the type represented by this `TypeId` is a signed integer. +/// +/// The more user-friendly version of this intrinsic is [`core::any::TypeId::is_signed`]. +#[rustc_intrinsic] +#[unstable(feature = "core_intrinsics", issue = "none")] +#[rustc_comptime] +pub fn type_id_is_signed(_id: crate::any::TypeId) -> bool; + /// Gets the size of the type represented by this `TypeId`. /// /// The more user-friendly version of this intrinsic is [`core::any::TypeId::size`]. diff --git a/library/core/src/mem/type_info.rs b/library/core/src/mem/type_info.rs index 66f85dab7b6c9..dd97a3c7197fd 100644 --- a/library/core/src/mem/type_info.rs +++ b/library/core/src/mem/type_info.rs @@ -379,6 +379,27 @@ pub enum Abi { } impl TypeId { + /// Returns `true` if the type represented by this `TypeId` is an signed integer. + /// + /// For everything else this returns false. + /// + /// # Examples + /// + /// ``` + /// #![feature(type_info)] + /// use std::any::TypeId; + /// + /// assert_eq!(const { TypeId::of::().is_signed() }, true); + /// assert_eq!(const { TypeId::of::().is_signed() }, false); + /// assert_eq!(const { TypeId::of::().is_signed() }, false); + /// ``` + #[unstable(feature = "type_info", issue = "146922")] + #[rustc_const_unstable(feature = "type_info", issue = "146922")] + #[rustc_comptime] + pub fn is_signed(self) -> bool { + intrinsics::type_id_is_signed(self) + } + /// Returns the size of the type represented by this `TypeId`. `None` if it is unsized. /// /// # Examples