diff --git a/library/core/src/any.rs b/library/core/src/any.rs index 866419ac34b11..268d2cbb30cf7 100644 --- a/library/core/src/any.rs +++ b/library/core/src/any.rs @@ -665,12 +665,46 @@ impl dyn Any + Send + Sync { /// While `TypeId` implements `Hash`, `PartialOrd`, and `Ord`, it is worth /// noting that the hashes and ordering will vary between Rust releases. Beware /// of relying on them inside of your code! -#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Debug, Hash)] +#[derive(Clone, Copy, Debug)] #[stable(feature = "rust1", since = "1.0.0")] +#[allow(dead_code)] pub struct TypeId { + // This field is unused, and is intended solely + // to break invalid transmutes to `TypeId`. + pad: core::mem::MaybeUninit, t: u64, } +#[stable(feature = "rust1", since = "1.0.0")] +impl PartialEq for TypeId { + fn eq(&self, other: &Self) -> bool { + self.t == other.t + } +} +#[stable(feature = "rust1", since = "1.0.0")] +impl Eq for TypeId {} + +#[stable(feature = "rust1", since = "1.0.0")] +impl core::hash::Hash for TypeId { + fn hash(&self, state: &mut H) { + self.t.hash(state); + } +} + +#[stable(feature = "rust1", since = "1.0.0")] +impl PartialOrd for TypeId { + fn partial_cmp(&self, other: &Self) -> Option { + Some(self.cmp(other)) + } +} + +#[stable(feature = "rust1", since = "1.0.0")] +impl Ord for TypeId { + fn cmp(&self, other: &Self) -> core::cmp::Ordering { + self.t.cmp(&other.t) + } +} + impl TypeId { /// Returns the `TypeId` of the type this generic function has been /// instantiated with. @@ -691,7 +725,7 @@ impl TypeId { #[stable(feature = "rust1", since = "1.0.0")] #[rustc_const_unstable(feature = "const_type_id", issue = "77125")] pub const fn of() -> TypeId { - TypeId { t: intrinsics::type_id::() } + TypeId { pad: core::mem::MaybeUninit::uninit(), t: intrinsics::type_id::() } } } diff --git a/src/test/ui/const-generics/issues/issue-90318.stderr b/src/test/ui/const-generics/issues/issue-90318.stderr index c8690ecd0da7e..1503e610bfde1 100644 --- a/src/test/ui/const-generics/issues/issue-90318.stderr +++ b/src/test/ui/const-generics/issues/issue-90318.stderr @@ -18,10 +18,9 @@ LL | If<{ TypeId::of::() != TypeId::of::<()>() }>: True, note: impl defined here, but it is not `const` --> $SRC_DIR/core/src/any.rs:LL:COL | -LL | #[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Debug, Hash)] - | ^^^^^^^^^ +LL | impl PartialEq for TypeId { + | ^^^^^^^^^^^^^^^^^^^^^^^^^ = note: calls in constants are limited to constant functions, tuple structs and tuple variants - = note: this error originates in the derive macro `PartialEq` (in Nightly builds, run with -Z macro-backtrace for more info) error: overly complex generic constant --> $DIR/issue-90318.rs:22:8 @@ -43,10 +42,9 @@ LL | If<{ TypeId::of::() != TypeId::of::<()>() }>: True, note: impl defined here, but it is not `const` --> $SRC_DIR/core/src/any.rs:LL:COL | -LL | #[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Debug, Hash)] - | ^^^^^^^^^ +LL | impl PartialEq for TypeId { + | ^^^^^^^^^^^^^^^^^^^^^^^^^ = note: calls in constants are limited to constant functions, tuple structs and tuple variants - = note: this error originates in the derive macro `PartialEq` (in Nightly builds, run with -Z macro-backtrace for more info) error: aborting due to 4 previous errors diff --git a/src/test/ui/consts/issue-73976-monomorphic.rs b/src/test/ui/consts/issue-73976-monomorphic.rs index 7706a97f23b48..873e5a005d49e 100644 --- a/src/test/ui/consts/issue-73976-monomorphic.rs +++ b/src/test/ui/consts/issue-73976-monomorphic.rs @@ -5,20 +5,9 @@ // will be properly rejected. This test will ensure that monomorphic use of these // would not be wrongly rejected in patterns. -#![feature(const_type_id)] #![feature(const_type_name)] -use std::any::{self, TypeId}; - -pub struct GetTypeId(T); - -impl GetTypeId { - pub const VALUE: TypeId = TypeId::of::(); -} - -const fn check_type_id() -> bool { - matches!(GetTypeId::::VALUE, GetTypeId::::VALUE) -} +use std::any; pub struct GetTypeNameLen(T); @@ -31,6 +20,5 @@ const fn check_type_name_len() -> bool { } fn main() { - assert!(check_type_id::()); assert!(check_type_name_len::()); }