Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions compiler/rustc_const_eval/src/const_eval/machine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)?;
Expand Down
2 changes: 2 additions & 0 deletions compiler/rustc_hir_analysis/src/check/intrinsic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);
Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_span/src/symbol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
8 changes: 8 additions & 0 deletions library/core/src/intrinsics/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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`].
Expand Down
21 changes: 21 additions & 0 deletions library/core/src/mem/type_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<i32>().is_signed() }, true);
/// assert_eq!(const { TypeId::of::<u8>().is_signed() }, false);
/// assert_eq!(const { TypeId::of::<bool>().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
Expand Down
Loading