From 333eb85d4b7dc51ada5202c35d5c67663111f403 Mon Sep 17 00:00:00 2001 From: Richo Healey Date: Tue, 7 Apr 2015 17:53:55 -0700 Subject: [PATCH 1/3] fmt: {:p#} formats pointers padded to native width --- src/libcore/fmt/mod.rs | 26 +++++++++++++++++++++++++- src/test/run-pass/fmt-pointer-trait.rs | 8 ++++++++ src/test/run-pass/ifmt.rs | 8 ++++++++ 3 files changed, 41 insertions(+), 1 deletion(-) diff --git a/src/libcore/fmt/mod.rs b/src/libcore/fmt/mod.rs index be804327663e5..2a7569e3b731d 100644 --- a/src/libcore/fmt/mod.rs +++ b/src/libcore/fmt/mod.rs @@ -847,9 +847,33 @@ impl Display for char { #[stable(feature = "rust1", since = "1.0.0")] impl Pointer for *const T { fn fmt(&self, f: &mut Formatter) -> Result { + let old_width = f.width; + let old_flags = f.flags; + + // The alternate flag is already treated by LowerHex as being special- + // it denotes whether to prefix with 0x. We use it to work out whether + // or not to zero extend, and then unconditionally set it to get the + // prefix. + if f.flags & 1 << (FlagV1::Alternate as u32) > 0 { + f.flags |= 1 << (FlagV1::SignAwareZeroPad as u32); + + if let None = f.width { + // The formats need two extra bytes, for the 0x + if cfg!(target_pointer_width = "32") { + f.width = Some(10); + } + if cfg!(target_pointer_width = "64") { + f.width = Some(18); + } + } + } f.flags |= 1 << (FlagV1::Alternate as u32); + let ret = LowerHex::fmt(&(*self as usize), f); - f.flags &= !(1 << (FlagV1::Alternate as u32)); + + f.width = old_width; + f.flags = old_flags; + ret } } diff --git a/src/test/run-pass/fmt-pointer-trait.rs b/src/test/run-pass/fmt-pointer-trait.rs index be8ecde67836e..0f4b0bfcec983 100644 --- a/src/test/run-pass/fmt-pointer-trait.rs +++ b/src/test/run-pass/fmt-pointer-trait.rs @@ -23,6 +23,14 @@ fn main() { let _ = format!("{:p}{:p}{:p}", rc, arc, b); + if cfg!(target_pointer_width = "32") { + assert_eq!(format!("{:#p}", p), + "0x00000000"); + } + if cfg!(target_pointer_width = "64") { + assert_eq!(format!("{:#p}", p), + "0x0000000000000000"); + } assert_eq!(format!("{:p}", p), "0x0"); } diff --git a/src/test/run-pass/ifmt.rs b/src/test/run-pass/ifmt.rs index 41c859214e96b..7230fe71e21ee 100644 --- a/src/test/run-pass/ifmt.rs +++ b/src/test/run-pass/ifmt.rs @@ -72,6 +72,14 @@ pub fn main() { t!(format!("{:X}", 10_usize), "A"); t!(format!("{}", "foo"), "foo"); t!(format!("{}", "foo".to_string()), "foo"); + if cfg!(target_pointer_width = "32") { + t!(format!("{:#p}", 0x1234 as *const isize), "0x00001234"); + t!(format!("{:#p}", 0x1234 as *mut isize), "0x00001234"); + } + if cfg!(target_pointer_width = "64") { + t!(format!("{:#p}", 0x1234 as *const isize), "0x0000000000001234"); + t!(format!("{:#p}", 0x1234 as *mut isize), "0x0000000000001234"); + } t!(format!("{:p}", 0x1234 as *const isize), "0x1234"); t!(format!("{:p}", 0x1234 as *mut isize), "0x1234"); t!(format!("{:x}", A), "aloha"); From d8bb08037f9ea7295feb5740a16a6dc1dd810f33 Mon Sep 17 00:00:00 2001 From: Richo Healey Date: Wed, 8 Apr 2015 17:48:19 -0700 Subject: [PATCH 2/3] test: Unignore test for fixed issue #20676 --- src/test/run-pass/ifmt.rs | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/test/run-pass/ifmt.rs b/src/test/run-pass/ifmt.rs index 7230fe71e21ee..8658099ec38c2 100644 --- a/src/test/run-pass/ifmt.rs +++ b/src/test/run-pass/ifmt.rs @@ -93,9 +93,8 @@ pub fn main() { t!(format!("{}", 5 + 5), "10"); t!(format!("{:#4}", C), "☃123"); - // FIXME(#20676) - // let a: &fmt::Debug = &1; - // t!(format!("{:?}", a), "1"); + let a: &fmt::Debug = &1; + t!(format!("{:?}", a), "1"); // Formatting strings and their arguments From 64da4e171d7fa5715b76becde4e8fd40712eaf9f Mon Sep 17 00:00:00 2001 From: Richo Healey Date: Thu, 9 Apr 2015 18:06:01 -0700 Subject: [PATCH 3/3] fmt: Assume that we'll only ever see 32 or 64 bit pointers --- src/libcore/fmt/mod.rs | 3 +-- src/test/run-pass/fmt-pointer-trait.rs | 3 +-- src/test/run-pass/ifmt.rs | 3 +-- 3 files changed, 3 insertions(+), 6 deletions(-) diff --git a/src/libcore/fmt/mod.rs b/src/libcore/fmt/mod.rs index 2a7569e3b731d..67781b73ae23c 100644 --- a/src/libcore/fmt/mod.rs +++ b/src/libcore/fmt/mod.rs @@ -861,8 +861,7 @@ impl Pointer for *const T { // The formats need two extra bytes, for the 0x if cfg!(target_pointer_width = "32") { f.width = Some(10); - } - if cfg!(target_pointer_width = "64") { + } else { f.width = Some(18); } } diff --git a/src/test/run-pass/fmt-pointer-trait.rs b/src/test/run-pass/fmt-pointer-trait.rs index 0f4b0bfcec983..96f31891f2f34 100644 --- a/src/test/run-pass/fmt-pointer-trait.rs +++ b/src/test/run-pass/fmt-pointer-trait.rs @@ -26,8 +26,7 @@ fn main() { if cfg!(target_pointer_width = "32") { assert_eq!(format!("{:#p}", p), "0x00000000"); - } - if cfg!(target_pointer_width = "64") { + } else { assert_eq!(format!("{:#p}", p), "0x0000000000000000"); } diff --git a/src/test/run-pass/ifmt.rs b/src/test/run-pass/ifmt.rs index 8658099ec38c2..3a7af09764419 100644 --- a/src/test/run-pass/ifmt.rs +++ b/src/test/run-pass/ifmt.rs @@ -75,8 +75,7 @@ pub fn main() { if cfg!(target_pointer_width = "32") { t!(format!("{:#p}", 0x1234 as *const isize), "0x00001234"); t!(format!("{:#p}", 0x1234 as *mut isize), "0x00001234"); - } - if cfg!(target_pointer_width = "64") { + } else { t!(format!("{:#p}", 0x1234 as *const isize), "0x0000000000001234"); t!(format!("{:#p}", 0x1234 as *mut isize), "0x0000000000001234"); }