Skip to content

Commit f6c5528

Browse files
committed
tests: Add regression test for Debug impl of raw pointers
1 parent 9a2ee9d commit f6c5528

File tree

4 files changed

+61
-0
lines changed

4 files changed

+61
-0
lines changed

library/Cargo.lock

+26
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ version = "0.0.0"
7575
dependencies = [
7676
"rand",
7777
"rand_xorshift",
78+
"regex",
7879
]
7980

8081
[[package]]
@@ -282,6 +283,31 @@ dependencies = [
282283
"rand_core",
283284
]
284285

286+
[[package]]
287+
name = "regex"
288+
version = "1.11.1"
289+
source = "registry+https://github.com/rust-lang/crates.io-index"
290+
checksum = "b544ef1b4eac5dc2db33ea63606ae9ffcfac26c1416a2806ae0bf5f56b201191"
291+
dependencies = [
292+
"regex-automata",
293+
"regex-syntax",
294+
]
295+
296+
[[package]]
297+
name = "regex-automata"
298+
version = "0.4.9"
299+
source = "registry+https://github.com/rust-lang/crates.io-index"
300+
checksum = "809e8dc61f6de73b46c85f4c96486310fe304c434cfa43669d7b40f711150908"
301+
dependencies = [
302+
"regex-syntax",
303+
]
304+
305+
[[package]]
306+
name = "regex-syntax"
307+
version = "0.8.5"
308+
source = "registry+https://github.com/rust-lang/crates.io-index"
309+
checksum = "2b15c43186be67a4fd63bee50d0303afffcef381492ebe2c5d87f324e1b8815c"
310+
285311
[[package]]
286312
name = "rustc-demangle"
287313
version = "0.1.24"

library/core/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ test = true
2727
[dev-dependencies]
2828
rand = { version = "0.8.5", default-features = false }
2929
rand_xorshift = { version = "0.3.0", default-features = false }
30+
regex = { version = "1.11.1", default-features = false }
3031

3132
[features]
3233
# Make panics and failed asserts immediately abort without formatting any message

library/core/tests/fmt/mod.rs

+31
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,37 @@ fn test_pointer_formats_data_pointer() {
1919
assert_eq!(format!("{b:p}"), format!("{:p}", b as *const _));
2020
}
2121

22+
#[test]
23+
fn test_fmt_debug_of_raw_pointers() {
24+
use core::fmt::Debug;
25+
26+
fn check_fmt<T: Debug>(t: T, expected: &str) {
27+
use std::sync::LazyLock;
28+
29+
use regex::Regex;
30+
31+
static ADDR_REGEX: LazyLock<Regex> =
32+
LazyLock::new(|| Regex::new(r"0x[0-9a-fA-F]+").unwrap());
33+
34+
let formatted = format!("{:?}", t);
35+
let normalized = ADDR_REGEX.replace_all(&formatted, "$$HEX");
36+
37+
assert_eq!(normalized, expected);
38+
}
39+
40+
let plain = &mut 100;
41+
check_fmt(plain as *mut i32, "$HEX");
42+
check_fmt(plain as *const i32, "$HEX");
43+
44+
let slice = &mut [200, 300, 400][..];
45+
check_fmt(slice as *mut [i32], "$HEX");
46+
check_fmt(slice as *const [i32], "$HEX");
47+
48+
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");
51+
}
52+
2253
#[test]
2354
fn test_estimated_capacity() {
2455
assert_eq!(format_args!("").estimated_capacity(), 0);

src/tools/tidy/src/deps.rs

+3
Original file line numberDiff line numberDiff line change
@@ -482,6 +482,9 @@ const PERMITTED_STDLIB_DEPENDENCIES: &[&str] = &[
482482
"rand",
483483
"rand_core",
484484
"rand_xorshift",
485+
"regex",
486+
"regex-automata",
487+
"regex-syntax",
485488
"rustc-demangle",
486489
"shlex",
487490
"unicode-width",

0 commit comments

Comments
 (0)