diff --git a/library/core/src/fmt/mod.rs b/library/core/src/fmt/mod.rs index a1bf3a4d7a706..bcb878b6d219b 100644 --- a/library/core/src/fmt/mod.rs +++ b/library/core/src/fmt/mod.rs @@ -2773,10 +2773,19 @@ impl Display for char { } } +use core::any::TypeId; + #[stable(feature = "rust1", since = "1.0.0")] impl Pointer for *const T { fn fmt(&self, f: &mut Formatter<'_>) -> Result { - pointer_fmt_inner(self.expose_provenance(), f) + if TypeId::of::<::Metadata>() == TypeId::of::<()>() { + pointer_fmt_inner(self.expose_provenance(), f) + } else { + f.debug_struct("Pointer") + .field_with("addr", |f| pointer_fmt_inner(self.expose_provenance(), f)) + .field("metadata", &core::ptr::metadata(*self)) + .finish() + } } } diff --git a/library/core/src/ptr/metadata.rs b/library/core/src/ptr/metadata.rs index e93b5658e2436..0909278c645ca 100644 --- a/library/core/src/ptr/metadata.rs +++ b/library/core/src/ptr/metadata.rs @@ -61,7 +61,7 @@ pub trait Pointee { // NOTE: Keep trait bounds in `static_assert_expected_bounds_for_metadata` // in `library/core/src/ptr/metadata.rs` // in sync with those here: - type Metadata: fmt::Debug + Copy + Send + Sync + Ord + Hash + Unpin + Freeze; + type Metadata: fmt::Debug + Copy + Send + Sync + Ord + Hash + Unpin + Freeze + 'static; } /// Pointers to types implementing this trait alias are “thin”. diff --git a/library/coretests/tests/fmt/mod.rs b/library/coretests/tests/fmt/mod.rs index 13f7bca646fa8..cb185dae9de35 100644 --- a/library/coretests/tests/fmt/mod.rs +++ b/library/coretests/tests/fmt/mod.rs @@ -42,12 +42,12 @@ fn test_fmt_debug_of_raw_pointers() { check_fmt(plain as *const i32, "$HEX"); let slice = &mut [200, 300, 400][..]; - check_fmt(slice as *mut [i32], "$HEX"); - check_fmt(slice as *const [i32], "$HEX"); + check_fmt(slice as *mut [i32], "Pointer { addr: $HEX, metadata: 3 }"); + check_fmt(slice as *const [i32], "Pointer { addr: $HEX, metadata: 3 }"); let vtable = &mut 500 as &mut dyn Debug; - check_fmt(vtable as *mut dyn Debug, "$HEX"); - check_fmt(vtable as *const dyn Debug, "$HEX"); + check_fmt(vtable as *mut dyn Debug, "Pointer { addr: $HEX, metadata: DynMetadata($HEX) }"); + check_fmt(vtable as *const dyn Debug, "Pointer { addr: $HEX, metadata: DynMetadata($HEX) }"); } #[test]