Skip to content

Commit 7ad7c2f

Browse files
committed
Remove Hash, PartialEq, Eq, PartialOrd, Ord from DynMetadata
1 parent 3fae1b9 commit 7ad7c2f

File tree

3 files changed

+15
-42
lines changed

3 files changed

+15
-42
lines changed

library/core/src/hash/mod.rs

+8-4
Original file line numberDiff line numberDiff line change
@@ -960,17 +960,21 @@ mod impls {
960960
fn hash<H: Hasher>(&self, state: &mut H) {
961961
let (address, metadata) = self.to_raw_parts();
962962
state.write_usize(address.addr());
963-
metadata.hash(state);
963+
if mem::size_of_val(&metadata) == mem::size_of::<usize>() {
964+
// SAFETY: This is either `usize` or `DynMetadata`. In both case doing transmute
965+
// is okay.
966+
// We can't hash `DynMetadata` directly so transmute it to usize before hashing.
967+
let metadata_as_usize = unsafe { mem::transmute_copy(&metadata) };
968+
state.write_usize(metadata_as_usize);
969+
}
964970
}
965971
}
966972

967973
#[stable(feature = "rust1", since = "1.0.0")]
968974
impl<T: ?Sized> Hash for *mut T {
969975
#[inline]
970976
fn hash<H: Hasher>(&self, state: &mut H) {
971-
let (address, metadata) = self.to_raw_parts();
972-
state.write_usize(address.addr());
973-
metadata.hash(state);
977+
self.cast_const().hash(state);
974978
}
975979
}
976980
}

library/core/src/ptr/metadata.rs

+1-32
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
#![unstable(feature = "ptr_metadata", issue = "81513")]
22

33
use crate::fmt;
4-
use crate::hash::{Hash, Hasher};
54

65
/// Provides the pointer metadata type of any pointed-to type.
76
///
@@ -57,7 +56,7 @@ pub trait Pointee {
5756
// NOTE: Keep trait bounds in `static_assert_expected_bounds_for_metadata`
5857
// in `library/core/src/ptr/metadata.rs`
5958
// in sync with those here:
60-
type Metadata: Copy + Send + Sync + Ord + Hash + Unpin;
59+
type Metadata: Copy + Send + Sync + Unpin;
6160
}
6261

6362
/// Pointers to types implementing this trait alias are “thin”.
@@ -241,33 +240,3 @@ impl<Dyn: ?Sized> Clone for DynMetadata<Dyn> {
241240
*self
242241
}
243242
}
244-
245-
impl<Dyn: ?Sized> Eq for DynMetadata<Dyn> {}
246-
247-
impl<Dyn: ?Sized> PartialEq for DynMetadata<Dyn> {
248-
#[inline]
249-
fn eq(&self, other: &Self) -> bool {
250-
crate::ptr::eq::<VTable>(self.vtable_ptr, other.vtable_ptr)
251-
}
252-
}
253-
254-
impl<Dyn: ?Sized> Ord for DynMetadata<Dyn> {
255-
#[inline]
256-
fn cmp(&self, other: &Self) -> crate::cmp::Ordering {
257-
(self.vtable_ptr as *const VTable).cmp(&(other.vtable_ptr as *const VTable))
258-
}
259-
}
260-
261-
impl<Dyn: ?Sized> PartialOrd for DynMetadata<Dyn> {
262-
#[inline]
263-
fn partial_cmp(&self, other: &Self) -> Option<crate::cmp::Ordering> {
264-
Some(self.cmp(other))
265-
}
266-
}
267-
268-
impl<Dyn: ?Sized> Hash for DynMetadata<Dyn> {
269-
#[inline]
270-
fn hash<H: Hasher>(&self, hasher: &mut H) {
271-
crate::ptr::hash::<VTable, _>(self.vtable_ptr, hasher)
272-
}
273-
}

library/core/tests/ptr.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -801,17 +801,17 @@ fn ptr_metadata() {
801801

802802
#[test]
803803
fn ptr_metadata_bounds() {
804-
fn metadata_eq_method_address<T: ?Sized>() -> usize {
805-
// The `Metadata` associated type has an `Ord` bound, so this is valid:
806-
<<T as Pointee>::Metadata as PartialEq>::eq as usize
804+
fn metadata_clone_method_address<T: ?Sized>() -> usize {
805+
// The `Metadata` associated type has an `Copy` bound, so this is valid:
806+
<<T as Pointee>::Metadata as Clone>::clone as usize
807807
}
808808
// "Synthetic" trait impls generated by the compiler like those of `Pointee`
809809
// are not checked for bounds of associated type.
810810
// So with a buggy core we could have both:
811811
// * `<dyn Display as Pointee>::Metadata == DynMetadata`
812-
// * `DynMetadata: !PartialEq`
812+
// * `DynMetadata: !Clone`
813813
// … and cause an ICE here:
814-
metadata_eq_method_address::<dyn Display>();
814+
metadata_clone_method_address::<dyn Display>();
815815

816816
// For this reason, let’s check here that bounds are satisfied:
817817

@@ -825,7 +825,7 @@ fn ptr_metadata_bounds() {
825825
fn static_assert_expected_bounds_for_metadata<Meta>()
826826
where
827827
// Keep this in sync with the associated type in `library/core/src/ptr/metadata.rs`
828-
Meta: Copy + Send + Sync + Ord + std::hash::Hash + Unpin,
828+
Meta: Copy + Send + Sync + Unpin,
829829
{
830830
}
831831
}

0 commit comments

Comments
 (0)