From 642b8e14c01594a27926bb8c5a0bcf94f5e3a4bd Mon Sep 17 00:00:00 2001 From: carbotaniuman <41451839+carbotaniuman@users.noreply.github.com> Date: Tue, 12 Jul 2022 13:09:36 -0500 Subject: [PATCH 1/9] Pad size of typeid and remove structuraleq --- library/core/src/any.rs | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/library/core/src/any.rs b/library/core/src/any.rs index 866419ac34b11..853fc8b513824 100644 --- a/library/core/src/any.rs +++ b/library/core/src/any.rs @@ -668,9 +668,17 @@ impl dyn Any + Send + Sync { #[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Debug, Hash)] #[stable(feature = "rust1", since = "1.0.0")] pub struct TypeId { + pad: u64, t: u64, } +impl PartialEq for TypeId { + fn eq(&self, other: &Self) -> bool { + self.t == other.t + } +} +impl Eq for TypeId {} + impl TypeId { /// Returns the `TypeId` of the type this generic function has been /// instantiated with. @@ -691,7 +699,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: 0, t: intrinsics::type_id::() } } } From 61e93958e165a96e45d2ca0f6c509caf1d50ceb9 Mon Sep 17 00:00:00 2001 From: carbotaniuman <41451839+carbotaniuman@users.noreply.github.com> Date: Tue, 12 Jul 2022 13:32:36 -0500 Subject: [PATCH 2/9] Actually delete derive --- library/core/src/any.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/library/core/src/any.rs b/library/core/src/any.rs index 853fc8b513824..da9096787f8c2 100644 --- a/library/core/src/any.rs +++ b/library/core/src/any.rs @@ -665,7 +665,7 @@ 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, PartialOrd, Ord, Debug, Hash)] #[stable(feature = "rust1", since = "1.0.0")] pub struct TypeId { pad: u64, From 0bb7737742409a4db4597417bf25c793e4f7bbeb Mon Sep 17 00:00:00 2001 From: carbotaniuman <41451839+carbotaniuman@users.noreply.github.com> Date: Tue, 12 Jul 2022 13:56:42 -0500 Subject: [PATCH 3/9] Add stability attrs --- library/core/src/any.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/library/core/src/any.rs b/library/core/src/any.rs index da9096787f8c2..f15640bf0e0a5 100644 --- a/library/core/src/any.rs +++ b/library/core/src/any.rs @@ -672,11 +672,13 @@ pub struct TypeId { 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 {} impl TypeId { From 7985d934b0de0a2f51b4df69c13341d7b542ef4d Mon Sep 17 00:00:00 2001 From: carbotaniuman <41451839+carbotaniuman@users.noreply.github.com> Date: Tue, 12 Jul 2022 20:19:18 -0500 Subject: [PATCH 4/9] Update tests --- library/core/src/any.rs | 2 ++ .../ui/const-generics/issues/issue-90318.stderr | 10 ++++------ src/test/ui/consts/issue-73976-monomorphic.rs | 14 +------------- 3 files changed, 7 insertions(+), 19 deletions(-) diff --git a/library/core/src/any.rs b/library/core/src/any.rs index f15640bf0e0a5..c9214235cf986 100644 --- a/library/core/src/any.rs +++ b/library/core/src/any.rs @@ -668,6 +668,8 @@ impl dyn Any + Send + Sync { #[derive(Clone, Copy, PartialOrd, Ord, Debug, Hash)] #[stable(feature = "rust1", since = "1.0.0")] pub struct TypeId { + // This field is unused, and is intended solely + // to break invalid transmutes to `TypeId`. pad: u64, t: u64, } 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::()); } From b35362cb972e31c153f4b56553bd5a7f20005efb Mon Sep 17 00:00:00 2001 From: carbotaniuman <41451839+carbotaniuman@users.noreply.github.com> Date: Thu, 21 Jul 2022 15:52:22 -0500 Subject: [PATCH 5/9] Try MaybeUninit --- library/core/src/any.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/library/core/src/any.rs b/library/core/src/any.rs index c9214235cf986..be9e2e4466d8c 100644 --- a/library/core/src/any.rs +++ b/library/core/src/any.rs @@ -670,7 +670,7 @@ impl dyn Any + Send + Sync { pub struct TypeId { // This field is unused, and is intended solely // to break invalid transmutes to `TypeId`. - pad: u64, + pad: core::mem::MaybeUninit, t: u64, } @@ -703,7 +703,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 { pad: 0, t: intrinsics::type_id::() } + TypeId { pad: core::mem::MaybeUninit::new(), t: intrinsics::type_id::() } } } From 34b2cce917bcad047b7505a5eba1bf55a5bff4ec Mon Sep 17 00:00:00 2001 From: carbotaniuman <41451839+carbotaniuman@users.noreply.github.com> Date: Thu, 21 Jul 2022 16:00:56 -0500 Subject: [PATCH 6/9] Impl traits manually --- library/core/src/any.rs | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/library/core/src/any.rs b/library/core/src/any.rs index be9e2e4466d8c..ecfa9210e2a84 100644 --- a/library/core/src/any.rs +++ b/library/core/src/any.rs @@ -665,7 +665,7 @@ 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, PartialOrd, Ord, Debug, Hash)] +#[derive(Clone, Copy, Debug)] #[stable(feature = "rust1", since = "1.0.0")] pub struct TypeId { // This field is unused, and is intended solely @@ -683,6 +683,27 @@ impl PartialEq for TypeId { #[stable(feature = "rust1", since = "1.0.0")] impl Eq for TypeId {} +#[stable(feature = "rust1", since = "1.0.0")] +impl 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) -> Ordering { + self.t.cmp(&other.t) + } +} + impl TypeId { /// Returns the `TypeId` of the type this generic function has been /// instantiated with. From e531a9a7f9c93e941624a10083b7b47c6986df95 Mon Sep 17 00:00:00 2001 From: carbotaniuman <41451839+carbotaniuman@users.noreply.github.com> Date: Thu, 21 Jul 2022 16:18:33 -0500 Subject: [PATCH 7/9] Fullly qualify --- library/core/src/any.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/library/core/src/any.rs b/library/core/src/any.rs index ecfa9210e2a84..103dd1a346f9d 100644 --- a/library/core/src/any.rs +++ b/library/core/src/any.rs @@ -684,22 +684,22 @@ impl PartialEq for TypeId { impl Eq for TypeId {} #[stable(feature = "rust1", since = "1.0.0")] -impl Hash for TypeId { - fn hash(&self, state: &mut H) { +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 { + 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) -> Ordering { + fn cmp(&self, other: &Self) -> core::cmp::Ordering { self.t.cmp(&other.t) } } From ee6d19bd7abb76a3a92c73b6792f6a940f3433a5 Mon Sep 17 00:00:00 2001 From: carbotaniuman <41451839+carbotaniuman@users.noreply.github.com> Date: Thu, 21 Jul 2022 18:46:37 -0500 Subject: [PATCH 8/9] Fix --- library/core/src/any.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/library/core/src/any.rs b/library/core/src/any.rs index 103dd1a346f9d..bb663e96bde17 100644 --- a/library/core/src/any.rs +++ b/library/core/src/any.rs @@ -724,7 +724,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 { pad: core::mem::MaybeUninit::new(), t: intrinsics::type_id::() } + TypeId { pad: core::mem::MaybeUninit::uninit(), t: intrinsics::type_id::() } } } From 53dcb861e41535f9e72c4635fddafa694cbdcc75 Mon Sep 17 00:00:00 2001 From: carbotaniuman <41451839+carbotaniuman@users.noreply.github.com> Date: Thu, 21 Jul 2022 19:13:38 -0500 Subject: [PATCH 9/9] Dead code warnings --- library/core/src/any.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/library/core/src/any.rs b/library/core/src/any.rs index bb663e96bde17..268d2cbb30cf7 100644 --- a/library/core/src/any.rs +++ b/library/core/src/any.rs @@ -667,6 +667,7 @@ impl dyn Any + Send + Sync { /// of relying on them inside of your code! #[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`.