Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

core: Make Debug impl of raw pointers print metadata if present #135080

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions library/Cargo.lock
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ version = "0.0.0"
dependencies = [
"rand",
"rand_xorshift",
"regex",
]

[[package]]
Expand Down Expand Up @@ -297,6 +298,31 @@ dependencies = [
"rand_core",
]

[[package]]
name = "regex"
version = "1.11.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b544ef1b4eac5dc2db33ea63606ae9ffcfac26c1416a2806ae0bf5f56b201191"
dependencies = [
"regex-automata",
"regex-syntax",
]

[[package]]
name = "regex-automata"
version = "0.4.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "809e8dc61f6de73b46c85f4c96486310fe304c434cfa43669d7b40f711150908"
dependencies = [
"regex-syntax",
]

[[package]]
name = "regex-syntax"
version = "0.8.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2b15c43186be67a4fd63bee50d0303afffcef381492ebe2c5d87f324e1b8815c"

[[package]]
name = "rustc-demangle"
version = "0.1.24"
Expand Down
9 changes: 8 additions & 1 deletion library/core/src/fmt/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2776,7 +2776,14 @@ impl Display for char {
#[stable(feature = "rust1", since = "1.0.0")]
impl<T: ?Sized> Pointer for *const T {
fn fmt(&self, f: &mut Formatter<'_>) -> Result {
pointer_fmt_inner(self.expose_provenance(), f)
if <<T as core::ptr::Pointee>::Metadata as core::unit::IsUnit>::is_unit() {
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()
}
}
}

Expand Down
2 changes: 2 additions & 0 deletions library/core/src/ptr/metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ 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:
// NOTE: The metadata of `dyn Trait + 'a` is `DynMetadata<dyn Trait + 'a>`
// so a `'static` bound must not be added.
type Metadata: fmt::Debug + Copy + Send + Sync + Ord + Hash + Unpin + Freeze;
}

Expand Down
16 changes: 16 additions & 0 deletions library/core/src/unit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,19 @@ impl FromIterator<()> for () {
iter.into_iter().for_each(|()| {})
}
}

pub(crate) trait IsUnit {
fn is_unit() -> bool;
}

impl<T: ?Sized> IsUnit for T {
default fn is_unit() -> bool {
false
}
}

impl IsUnit for () {
fn is_unit() -> bool {
true
}
}
1 change: 1 addition & 0 deletions library/coretests/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,4 @@ test = true
[dev-dependencies]
rand = { version = "0.9.0", default-features = false }
rand_xorshift = { version = "0.4.0", default-features = false }
regex = { version = "1.11.1", default-features = false }
35 changes: 33 additions & 2 deletions library/coretests/tests/fmt/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,39 @@ fn test_format_flags() {
fn test_pointer_formats_data_pointer() {
let b: &[u8] = b"";
let s: &str = "";
assert_eq!(format!("{s:p}"), format!("{:p}", s.as_ptr()));
assert_eq!(format!("{b:p}"), format!("{:p}", b.as_ptr()));
assert_eq!(format!("{s:p}"), format!("{:p}", s as *const _));
assert_eq!(format!("{b:p}"), format!("{:p}", b as *const _));
}

#[test]
fn test_fmt_debug_of_raw_pointers() {
use core::fmt::Debug;

fn check_fmt<T: Debug>(t: T, expected: &str) {
use std::sync::LazyLock;

use regex::Regex;

static ADDR_REGEX: LazyLock<Regex> =
LazyLock::new(|| Regex::new(r"0x[0-9a-fA-F]+").unwrap());

let formatted = format!("{:?}", t);
let normalized = ADDR_REGEX.replace_all(&formatted, "$$HEX");

assert_eq!(normalized, expected);
}

let plain = &mut 100;
check_fmt(plain as *mut i32, "$HEX");
check_fmt(plain as *const i32, "$HEX");

let slice = &mut [200, 300, 400][..];
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, "Pointer { addr: $HEX, metadata: DynMetadata($HEX) }");
check_fmt(vtable as *const dyn Debug, "Pointer { addr: $HEX, metadata: DynMetadata($HEX) }");
}

#[test]
Expand Down
Loading