Skip to content

Commit c87ec1e

Browse files
committed
Auto merge of #24186 - richo:pad-pointers, r=alexcrichton
This pads out the printing of pointers to their native width. Extracted from and rebased on top of #24144
2 parents 93f7fe3 + 64da4e1 commit c87ec1e

File tree

3 files changed

+40
-4
lines changed

3 files changed

+40
-4
lines changed

src/libcore/fmt/mod.rs

+24-1
Original file line numberDiff line numberDiff line change
@@ -847,9 +847,32 @@ impl Display for char {
847847
#[stable(feature = "rust1", since = "1.0.0")]
848848
impl<T> Pointer for *const T {
849849
fn fmt(&self, f: &mut Formatter) -> Result {
850+
let old_width = f.width;
851+
let old_flags = f.flags;
852+
853+
// The alternate flag is already treated by LowerHex as being special-
854+
// it denotes whether to prefix with 0x. We use it to work out whether
855+
// or not to zero extend, and then unconditionally set it to get the
856+
// prefix.
857+
if f.flags & 1 << (FlagV1::Alternate as u32) > 0 {
858+
f.flags |= 1 << (FlagV1::SignAwareZeroPad as u32);
859+
860+
if let None = f.width {
861+
// The formats need two extra bytes, for the 0x
862+
if cfg!(target_pointer_width = "32") {
863+
f.width = Some(10);
864+
} else {
865+
f.width = Some(18);
866+
}
867+
}
868+
}
850869
f.flags |= 1 << (FlagV1::Alternate as u32);
870+
851871
let ret = LowerHex::fmt(&(*self as usize), f);
852-
f.flags &= !(1 << (FlagV1::Alternate as u32));
872+
873+
f.width = old_width;
874+
f.flags = old_flags;
875+
853876
ret
854877
}
855878
}

src/test/run-pass/fmt-pointer-trait.rs

+7
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,13 @@ fn main() {
2323
let _ = format!("{:p}{:p}{:p}",
2424
rc, arc, b);
2525

26+
if cfg!(target_pointer_width = "32") {
27+
assert_eq!(format!("{:#p}", p),
28+
"0x00000000");
29+
} else {
30+
assert_eq!(format!("{:#p}", p),
31+
"0x0000000000000000");
32+
}
2633
assert_eq!(format!("{:p}", p),
2734
"0x0");
2835
}

src/test/run-pass/ifmt.rs

+9-3
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,13 @@ pub fn main() {
7272
t!(format!("{:X}", 10_usize), "A");
7373
t!(format!("{}", "foo"), "foo");
7474
t!(format!("{}", "foo".to_string()), "foo");
75+
if cfg!(target_pointer_width = "32") {
76+
t!(format!("{:#p}", 0x1234 as *const isize), "0x00001234");
77+
t!(format!("{:#p}", 0x1234 as *mut isize), "0x00001234");
78+
} else {
79+
t!(format!("{:#p}", 0x1234 as *const isize), "0x0000000000001234");
80+
t!(format!("{:#p}", 0x1234 as *mut isize), "0x0000000000001234");
81+
}
7582
t!(format!("{:p}", 0x1234 as *const isize), "0x1234");
7683
t!(format!("{:p}", 0x1234 as *mut isize), "0x1234");
7784
t!(format!("{:x}", A), "aloha");
@@ -85,9 +92,8 @@ pub fn main() {
8592
t!(format!("{}", 5 + 5), "10");
8693
t!(format!("{:#4}", C), "☃123");
8794

88-
// FIXME(#20676)
89-
// let a: &fmt::Debug = &1;
90-
// t!(format!("{:?}", a), "1");
95+
let a: &fmt::Debug = &1;
96+
t!(format!("{:?}", a), "1");
9197

9298

9399
// Formatting strings and their arguments

0 commit comments

Comments
 (0)