Skip to content

Commit dab5a8f

Browse files
Enseliclukas-code
andcommitted
core: Make Debug impl of raw pointers print metadata if present
Make Rust pointers less magic by including metadata information in their `Debug` output. This does not break Rust stability guarantees because `Debug` output is explicitly exempted from stability: https://doc.rust-lang.org/std/fmt/trait.Debug.html#stability Co-authored-by: Lukas <26522220+lukas-code@users.noreply.github.com>
1 parent 5f086cc commit dab5a8f

File tree

4 files changed

+16
-7
lines changed

4 files changed

+16
-7
lines changed

library/core/src/fmt/mod.rs

+10-1
Original file line numberDiff line numberDiff line change
@@ -2776,7 +2776,16 @@ impl Display for char {
27762776
#[stable(feature = "rust1", since = "1.0.0")]
27772777
impl<T: ?Sized> Pointer for *const T {
27782778
fn fmt(&self, f: &mut Formatter<'_>) -> Result {
2779-
pointer_fmt_inner(self.expose_provenance(), f)
2779+
if core::any::TypeId::of::<<T as core::ptr::Pointee>::Metadata>()
2780+
== core::any::TypeId::of::<()>()
2781+
{
2782+
pointer_fmt_inner(self.expose_provenance(), f)
2783+
} else {
2784+
f.debug_struct("Pointer")
2785+
.field_with("addr", |f| pointer_fmt_inner(self.expose_provenance(), f))
2786+
.field("metadata", &core::ptr::metadata(*self))
2787+
.finish()
2788+
}
27802789
}
27812790
}
27822791

library/core/src/ptr/metadata.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ pub trait Pointee {
6161
// NOTE: Keep trait bounds in `static_assert_expected_bounds_for_metadata`
6262
// in `library/core/src/ptr/metadata.rs`
6363
// in sync with those here:
64-
type Metadata: fmt::Debug + Copy + Send + Sync + Ord + Hash + Unpin + Freeze;
64+
type Metadata: fmt::Debug + Copy + Send + Sync + Ord + Hash + Unpin + Freeze + 'static;
6565
}
6666

6767
/// Pointers to types implementing this trait alias are “thin”.

library/coretests/tests/fmt/mod.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,12 @@ fn test_fmt_debug_of_raw_pointers() {
4242
check_fmt(plain as *const i32, "$HEX");
4343

4444
let slice = &mut [200, 300, 400][..];
45-
check_fmt(slice as *mut [i32], "$HEX");
46-
check_fmt(slice as *const [i32], "$HEX");
45+
check_fmt(slice as *mut [i32], "Pointer { addr: $HEX, metadata: 3 }");
46+
check_fmt(slice as *const [i32], "Pointer { addr: $HEX, metadata: 3 }");
4747

4848
let vtable = &mut 500 as &mut dyn Debug;
49-
check_fmt(vtable as *mut dyn Debug, "$HEX");
50-
check_fmt(vtable as *const dyn Debug, "$HEX");
49+
check_fmt(vtable as *mut dyn Debug, "Pointer { addr: $HEX, metadata: DynMetadata($HEX) }");
50+
check_fmt(vtable as *const dyn Debug, "Pointer { addr: $HEX, metadata: DynMetadata($HEX) }");
5151
}
5252

5353
#[test]

library/coretests/tests/ptr.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -589,7 +589,7 @@ fn ptr_metadata_bounds() {
589589
fn static_assert_expected_bounds_for_metadata<Meta>()
590590
where
591591
// Keep this in sync with the associated type in `library/core/src/ptr/metadata.rs`
592-
Meta: Debug + Copy + Send + Sync + Ord + std::hash::Hash + Unpin + Freeze,
592+
Meta: Debug + Copy + Send + Sync + Ord + std::hash::Hash + Unpin + Freeze + 'static,
593593
{
594594
}
595595
}

0 commit comments

Comments
 (0)