From c5aa659832334dca7cc9b27074bdc686b9650c12 Mon Sep 17 00:00:00 2001 From: GnomedDev Date: Tue, 20 Feb 2024 18:59:56 +0000 Subject: [PATCH 1/3] Reduce alignment of TypeId to u64 alignment --- library/core/src/any.rs | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/library/core/src/any.rs b/library/core/src/any.rs index e8f00e8760e13..7efbabb725748 100644 --- a/library/core/src/any.rs +++ b/library/core/src/any.rs @@ -605,7 +605,8 @@ impl dyn Any + Send + Sync { #[derive(Clone, Copy, Debug, Eq, PartialOrd, Ord)] #[stable(feature = "rust1", since = "1.0.0")] pub struct TypeId { - t: u128, + // See #115620 for this representation. + t: (u64, u64), } #[stable(feature = "rust1", since = "1.0.0")] @@ -637,7 +638,10 @@ impl TypeId { #[rustc_const_unstable(feature = "const_type_id", issue = "77125")] pub const fn of() -> TypeId { let t: u128 = intrinsics::type_id::(); - TypeId { t } + + let t1 = (t >> 64) as u64; + let t2 = t as u64; + TypeId { t: (t1, t2) } } } @@ -657,7 +661,7 @@ impl hash::Hash for TypeId { // - It is correct to do so -- only hashing a subset of `self` is still // with an `Eq` implementation that considers the entire value, as // ours does. - (self.t as u64).hash(state); + self.t.0.hash(state); } } From aa25c481b77cb9e0ec25c6350d7ded110fae7f0e Mon Sep 17 00:00:00 2001 From: Gnome! Date: Tue, 20 Feb 2024 19:12:42 +0000 Subject: [PATCH 2/3] Add extra detail to field comment Co-authored-by: Nilstrieb <48135649+Nilstrieb@users.noreply.github.com> --- library/core/src/any.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/library/core/src/any.rs b/library/core/src/any.rs index 7efbabb725748..adc45038148e8 100644 --- a/library/core/src/any.rs +++ b/library/core/src/any.rs @@ -605,7 +605,8 @@ impl dyn Any + Send + Sync { #[derive(Clone, Copy, Debug, Eq, PartialOrd, Ord)] #[stable(feature = "rust1", since = "1.0.0")] pub struct TypeId { - // See #115620 for this representation. + // We avoid using `u128` because that imposes higher alignment requirements on many platforms. + // See issue #115620 for more information. t: (u64, u64), } From f142476cf3f0c0c2296c82836fd4c7c86e20d0db Mon Sep 17 00:00:00 2001 From: David Thomas Date: Sun, 25 Feb 2024 14:09:30 +0000 Subject: [PATCH 3/3] Fix Hash impl --- 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 adc45038148e8..a4252d0c9e03c 100644 --- a/library/core/src/any.rs +++ b/library/core/src/any.rs @@ -662,7 +662,7 @@ impl hash::Hash for TypeId { // - It is correct to do so -- only hashing a subset of `self` is still // with an `Eq` implementation that considers the entire value, as // ours does. - self.t.0.hash(state); + self.t.1.hash(state); } }