]> git.lizzy.rs Git - rust.git/commitdiff
print pointers more compactly when they are too big
authorRalf Jung <post@ralfj.de>
Sun, 26 Apr 2020 16:14:16 +0000 (18:14 +0200)
committerRalf Jung <post@ralfj.de>
Sun, 26 Apr 2020 16:14:19 +0000 (18:14 +0200)
src/librustc_middle/mir/interpret/mod.rs
src/librustc_middle/mir/interpret/pointer.rs
src/librustc_mir/util/pretty.rs
src/test/mir-opt/const_allocation/32bit/rustc.main.ConstProp.after.mir
src/test/mir-opt/const_allocation2/32bit/rustc.main.ConstProp.after.mir
src/test/mir-opt/const_allocation3/32bit/rustc.main.ConstProp.after.mir

index 96bf694d8fa67181013ff99e73996a2a7b43d327..3925f6d0f9bd71d7f68215dfb37472dffd3c2cf7 100644 (file)
@@ -168,15 +168,21 @@ pub enum LitToConstError {
 #[derive(Copy, Clone, Eq, Hash, Ord, PartialEq, PartialOrd)]
 pub struct AllocId(pub u64);
 
-impl fmt::Debug for AllocId {
-    fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
-        fmt::Display::fmt(self, fmt)
+impl fmt::Display for AllocId {
+    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
+        if f.alternate() {
+            write!(f, "a{}", self.0)
+        } else {
+            write!(f, "alloc{}", self.0)
+        }
     }
 }
 
-impl fmt::Display for AllocId {
-    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
-        write!(f, "alloc{}", self.0)
+// We also want the `Debug` output to be readable as it is used by `derive(Debug)` for
+// all the Miri types.
+impl fmt::Debug for AllocId {
+    fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
+        fmt::Display::fmt(self, fmt)
     }
 }
 
index 8434b45a1e7e795e5b7a9026d664afd7abdc5d21..9e2cc666a282b865f95312b0ad5c1f0c9f0f3d6a 100644 (file)
@@ -119,15 +119,31 @@ pub struct Pointer<Tag = (), Id = AllocId> {
 
 static_assert_size!(Pointer, 16);
 
-impl<Tag: fmt::Debug, Id: fmt::Debug> fmt::Debug for Pointer<Tag, Id> {
+impl<Tag: fmt::Debug, Id: fmt::Debug> fmt::Display for Pointer<Tag, Id> {
     default fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
-        write!(f, "{:?}+0x{:x}[{:?}]", self.alloc_id, self.offset.bytes(), self.tag)
+        if f.alternate() {
+            write!(f, "{:#?}+0x{:x}[{:?}]", self.alloc_id, self.offset.bytes(), self.tag)
+        } else {
+            write!(f, "{:?}+0x{:x}[{:?}]", self.alloc_id, self.offset.bytes(), self.tag)
+        }
     }
 }
 // Specialization for no tag
-impl<Id: fmt::Debug> fmt::Debug for Pointer<(), Id> {
+impl<Id: fmt::Debug> fmt::Display for Pointer<(), Id> {
     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
-        write!(f, "{:?}+0x{:x}", self.alloc_id, self.offset.bytes())
+        if f.alternate() {
+            write!(f, "{:#?}+0x{:x}", self.alloc_id, self.offset.bytes())
+        } else {
+            write!(f, "{:?}+0x{:x}", self.alloc_id, self.offset.bytes())
+        }
+    }
+}
+
+// We also want the `Debug` output to be readable as it is used by `derive(Debug)` for
+// all the Miri types.
+impl<Tag: fmt::Debug, Id: fmt::Debug> fmt::Debug for Pointer<Tag, Id> {
+    fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
+        fmt::Display::fmt(self, fmt)
     }
 }
 
index 9a01deee519c24fe6e566848edc9a97b024aa7f7..932570e522f7922190fddd8ec1defc03367b8cdd 100644 (file)
@@ -726,6 +726,10 @@ fn write_allocation_bytes<Tag: Copy + Debug, Extra>(
             let relocation_width = |bytes| bytes * 3;
             let ptr = Pointer::new_with_tag(target_id, offset, tag);
             let mut target = format!("{:?}", ptr);
+            if target.len() > relocation_width(ptr_size.bytes_usize() - 1) {
+                // This is too long, try to save some space.
+                target = format!("{:#?}", ptr);
+            }
             if ((i - line_start) + ptr_size).bytes_usize() > BYTES_PER_LINE {
                 // This branch handles the situation where a relocation starts in the current line
                 // but ends in the next one.
index 3946b9dc45bceb5acd8fd1d0ba88630448f13c0e..66df28ea702eadb88c0a7f1e94382363a5c8b4f3 100644 (file)
@@ -30,19 +30,19 @@ fn main() -> () {
 }
 
 alloc0 (static: FOO, size: 8, align: 4) {
-    ╾alloc17+0x0 (4 ptr bytes)╼ 03 00 00 00                         │ ╾──╼....
+    ╾─a17+0x0─╼ 03 00 00 00                         │ ╾──╼....
 }
 
 alloc17 (size: 48, align: 4) {
-    0x00 │ 00 00 00 00 __ __ __ __ ╾alloc4+0x0 (4 ptr bytes)╼ 00 00 00 00 │ ....░░░░╾──╼....
-    0x10 │ 00 00 00 00 __ __ __ __ ╾alloc8+0x0 (4 ptr bytes)╼ 02 00 00 00 │ ....░░░░╾──╼....
-    0x20 │ 01 00 00 00 2a 00 00 00 ╾alloc13+0x0 (4 ptr bytes)╼ 03 00 00 00 │ ....*...╾──╼....
+    0x00 │ 00 00 00 00 __ __ __ __ ╾─a4+0x0──╼ 00 00 00 00 │ ....░░░░╾──╼....
+    0x10 │ 00 00 00 00 __ __ __ __ ╾─a8+0x0──╼ 02 00 00 00 │ ....░░░░╾──╼....
+    0x20 │ 01 00 00 00 2a 00 00 00 ╾─a13+0x0─╼ 03 00 00 00 │ ....*...╾──╼....
 }
 
 alloc4 (size: 0, align: 4) {}
 
 alloc8 (size: 16, align: 4) {
-    ╾alloc7+0x0 (4 ptr bytes)╼ 03 00 00 00 ╾alloc9+0x0 (4 ptr bytes)╼ 03 00 00 00 │ ╾──╼....╾──╼....
+    ╾─a7+0x0──╼ 03 00 00 00 ╾─a9+0x0──╼ 03 00 00 00 │ ╾──╼....╾──╼....
 }
 
 alloc7 (size: 3, align: 1) {
@@ -54,8 +54,8 @@ alloc9 (size: 3, align: 1) {
 }
 
 alloc13 (size: 24, align: 4) {
-    0x00 │ ╾alloc12+0x0 (4 ptr bytes)╼ 03 00 00 00 ╾alloc14+0x0 (4 ptr bytes)╼ 03 00 00 00 │ ╾──╼....╾──╼....
-    0x10 │ ╾alloc15+0x0 (4 ptr bytes)╼ 04 00 00 00                         │ ╾──╼....
+    0x00 │ ╾─a12+0x0─╼ 03 00 00 00 ╾─a14+0x0─╼ 03 00 00 00 │ ╾──╼....╾──╼....
+    0x10 │ ╾─a15+0x0─╼ 04 00 00 00                         │ ╾──╼....
 }
 
 alloc12 (size: 3, align: 1) {
index ce3410cf86e07d5fe844bc944efc6555d49283cd..caced1dc95d39267268ba320d9c48341aaadbaa0 100644 (file)
@@ -30,19 +30,19 @@ fn main() -> () {
 }
 
 alloc0 (static: FOO, size: 8, align: 4) {
-    ╾alloc21+0x0 (4 ptr bytes)╼ 03 00 00 00                         │ ╾──╼....
+    ╾─a21+0x0─╼ 03 00 00 00                         │ ╾──╼....
 }
 
 alloc21 (size: 48, align: 4) {
-    0x00 │ 00 00 00 00 __ __ __ __ ╾alloc4+0x0 (4 ptr bytes)╼ 00 00 00 00 │ ....░░░░╾──╼....
-    0x10 │ 00 00 00 00 __ __ __ __ ╾alloc9+0x0 (4 ptr bytes)╼ 02 00 00 00 │ ....░░░░╾──╼....
-    0x20 │ 01 00 00 00 2a 00 00 00 ╾alloc19+0x0 (4 ptr bytes)╼ 03 00 00 00 │ ....*...╾──╼....
+    0x00 │ 00 00 00 00 __ __ __ __ ╾─a4+0x0──╼ 00 00 00 00 │ ....░░░░╾──╼....
+    0x10 │ 00 00 00 00 __ __ __ __ ╾─a9+0x0──╼ 02 00 00 00 │ ....░░░░╾──╼....
+    0x20 │ 01 00 00 00 2a 00 00 00 ╾─a19+0x0─╼ 03 00 00 00 │ ....*...╾──╼....
 }
 
 alloc4 (size: 0, align: 4) {}
 
 alloc9 (size: 8, align: 4) {
-    ╾alloc7+0x0 (4 ptr bytes)╼ ╾alloc8+0x0 (4 ptr bytes)╼                         │ ╾──╼╾──╼
+    ╾─a7+0x0──╼ ╾─a8+0x0──╼                         │ ╾──╼╾──╼
 }
 
 alloc7 (size: 1, align: 1) {
@@ -54,7 +54,7 @@ alloc8 (size: 1, align: 1) {
 }
 
 alloc19 (size: 12, align: 4) {
-    ╾alloc15+0x3 (4 ptr bytes)╼ ╾alloc16+0x0 (4 ptr bytes)╼ ╾alloc18+0x2 (4 ptr bytes)╼             │ ╾──╼╾──╼╾──╼
+    ╾─a15+0x3─╼ ╾─a16+0x0─╼ ╾─a18+0x2─╼             │ ╾──╼╾──╼╾──╼
 }
 
 alloc15 (size: 4, align: 1) {
index f004a4b6b7c263fff1ae801f6bacb0bf47656d8b..a93161a0183cad3b8d41be17c89b3b601a6f1828 100644 (file)
@@ -30,20 +30,20 @@ fn main() -> () {
 }
 
 alloc0 (static: FOO, size: 4, align: 4) {
-    ╾alloc9+0x0 (4 ptr bytes)╼                                     │ ╾──╼
+    ╾─a9+0x0──╼                                     │ ╾──╼
 }
 
 alloc9 (size: 168, align: 1) {
     0x00 │ ab ab ab ab ab ab ab ab ab ab ab ab ab ab ab ab │ ................
-    0x10 │ ab ab ab ab ab ab ab ab ab ab ab ab ╾alloc4+0x0 (4 ptr bytes)╼ │ ............╾──╼
+    0x10 │ ab ab ab ab ab ab ab ab ab ab ab ab ╾─a4+0x0──╼ │ ............╾──╼
     0x20 │ 01 ef cd ab 00 00 00 00 00 00 00 00 00 00 00 00 │ ................
     0x30 │ 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 │ ................
     0x40 │ 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 │ ................
     0x50 │ 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 │ ................
     0x60 │ 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 │ ................
     0x70 │ 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 │ ................
-    0x80 │ 00 00 00 00 00 00 00 00 00 00 ╾alloc6+0x0 (4 ptr bytes)╼ 00 00 │ ..........╾──╼..
-    0x90 │ ╾alloc7+0x63 (4 ptr bytes)╼ 00 00 00 00 00 00 00 00 00 00 00 00 │ ╾──╼............
+    0x80 │ 00 00 00 00 00 00 00 00 00 00 ╾─a6+0x0──╼ 00 00 │ ..........╾──╼..
+    0x90 │ ╾─a7+0x63─╼ 00 00 00 00 00 00 00 00 00 00 00 00 │ ╾──╼............
     0xa0 │ 00 00 00 00 00 00 00 00                         │ ........
 }