Skip to content

Commit 19eb934

Browse files
committed
print pointers more compactly when they are too big
1 parent c427438 commit 19eb934

File tree

6 files changed

+53
-27
lines changed

6 files changed

+53
-27
lines changed

src/librustc_middle/mir/interpret/mod.rs

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -168,15 +168,21 @@ pub enum LitToConstError {
168168
#[derive(Copy, Clone, Eq, Hash, Ord, PartialEq, PartialOrd)]
169169
pub struct AllocId(pub u64);
170170

171-
impl fmt::Debug for AllocId {
172-
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
173-
fmt::Display::fmt(self, fmt)
171+
impl fmt::Display for AllocId {
172+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
173+
if f.alternate() {
174+
write!(f, "a{}", self.0)
175+
} else {
176+
write!(f, "alloc{}", self.0)
177+
}
174178
}
175179
}
176180

177-
impl fmt::Display for AllocId {
178-
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
179-
write!(f, "alloc{}", self.0)
181+
// We also want the `Debug` output to be readable as it is used by `derive(Debug)` for
182+
// all the Miri types.
183+
impl fmt::Debug for AllocId {
184+
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
185+
fmt::Display::fmt(self, fmt)
180186
}
181187
}
182188

src/librustc_middle/mir/interpret/pointer.rs

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -119,15 +119,31 @@ pub struct Pointer<Tag = (), Id = AllocId> {
119119

120120
static_assert_size!(Pointer, 16);
121121

122-
impl<Tag: fmt::Debug, Id: fmt::Debug> fmt::Debug for Pointer<Tag, Id> {
122+
impl<Tag: fmt::Debug, Id: fmt::Debug> fmt::Display for Pointer<Tag, Id> {
123123
default fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
124-
write!(f, "{:?}+0x{:x}[{:?}]", self.alloc_id, self.offset.bytes(), self.tag)
124+
if f.alternate() {
125+
write!(f, "{:#?}+0x{:x}[{:?}]", self.alloc_id, self.offset.bytes(), self.tag)
126+
} else {
127+
write!(f, "{:?}+0x{:x}[{:?}]", self.alloc_id, self.offset.bytes(), self.tag)
128+
}
125129
}
126130
}
127131
// Specialization for no tag
128-
impl<Id: fmt::Debug> fmt::Debug for Pointer<(), Id> {
132+
impl<Id: fmt::Debug> fmt::Display for Pointer<(), Id> {
129133
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
130-
write!(f, "{:?}+0x{:x}", self.alloc_id, self.offset.bytes())
134+
if f.alternate() {
135+
write!(f, "{:#?}+0x{:x}", self.alloc_id, self.offset.bytes())
136+
} else {
137+
write!(f, "{:?}+0x{:x}", self.alloc_id, self.offset.bytes())
138+
}
139+
}
140+
}
141+
142+
// We also want the `Debug` output to be readable as it is used by `derive(Debug)` for
143+
// all the Miri types.
144+
impl<Tag: fmt::Debug, Id: fmt::Debug> fmt::Debug for Pointer<Tag, Id> {
145+
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
146+
fmt::Display::fmt(self, fmt)
131147
}
132148
}
133149

src/librustc_mir/util/pretty.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -726,6 +726,10 @@ fn write_allocation_bytes<Tag: Copy + Debug, Extra>(
726726
let relocation_width = |bytes| bytes * 3;
727727
let ptr = Pointer::new_with_tag(target_id, offset, tag);
728728
let mut target = format!("{:?}", ptr);
729+
if target.len() > relocation_width(ptr_size.bytes_usize() - 1) {
730+
// This is too long, try to save some space.
731+
target = format!("{:#?}", ptr);
732+
}
729733
if ((i - line_start) + ptr_size).bytes_usize() > BYTES_PER_LINE {
730734
// This branch handles the situation where a relocation starts in the current line
731735
// but ends in the next one.

src/test/mir-opt/const_allocation/32bit/rustc.main.ConstProp.after.mir

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,19 +30,19 @@ fn main() -> () {
3030
}
3131

3232
alloc0 (static: FOO, size: 8, align: 4) {
33-
alloc17+0x0 (4 ptr bytes)╼ 03 00 00 00 │ ╾──╼....
33+
─a17+0x0╼ 03 00 00 00 │ ╾──╼....
3434
}
3535

3636
alloc17 (size: 48, align: 4) {
37-
0x00 │ 00 00 00 00 __ __ __ __ ╾alloc4+0x0 (4 ptr bytes)╼ 00 00 00 00 │ ....░░░░╾──╼....
38-
0x10 │ 00 00 00 00 __ __ __ __ ╾alloc8+0x0 (4 ptr bytes)╼ 02 00 00 00 │ ....░░░░╾──╼....
39-
0x20 │ 01 00 00 00 2a 00 00 00 ╾alloc13+0x0 (4 ptr bytes)╼ 03 00 00 00 │ ....*...╾──╼....
37+
0x00 │ 00 00 00 00 __ __ __ __ ╾─a4+0x0──╼ 00 00 00 00 │ ....░░░░╾──╼....
38+
0x10 │ 00 00 00 00 __ __ __ __ ╾─a8+0x0──╼ 02 00 00 00 │ ....░░░░╾──╼....
39+
0x20 │ 01 00 00 00 2a 00 00 00 ╾─a13+0x0╼ 03 00 00 00 │ ....*...╾──╼....
4040
}
4141

4242
alloc4 (size: 0, align: 4) {}
4343

4444
alloc8 (size: 16, align: 4) {
45-
alloc7+0x0 (4 ptr bytes)╼ 03 00 00 00 ╾alloc9+0x0 (4 ptr bytes)╼ 03 00 00 00 │ ╾──╼....╾──╼....
45+
─a7+0x0──╼ 03 00 00 00 ╾─a9+0x0──╼ 03 00 00 00 │ ╾──╼....╾──╼....
4646
}
4747

4848
alloc7 (size: 3, align: 1) {
@@ -54,8 +54,8 @@ alloc9 (size: 3, align: 1) {
5454
}
5555

5656
alloc13 (size: 24, align: 4) {
57-
0x00 │ ╾alloc12+0x0 (4 ptr bytes)╼ 03 00 00 00 ╾alloc14+0x0 (4 ptr bytes)╼ 03 00 00 00 │ ╾──╼....╾──╼....
58-
0x10 │ ╾alloc15+0x0 (4 ptr bytes)╼ 04 00 00 00 │ ╾──╼....
57+
0x00 │ ╾─a12+0x0╼ 03 00 00 00 ╾─a14+0x0╼ 03 00 00 00 │ ╾──╼....╾──╼....
58+
0x10 │ ╾─a15+0x0╼ 04 00 00 00 │ ╾──╼....
5959
}
6060

6161
alloc12 (size: 3, align: 1) {

src/test/mir-opt/const_allocation2/32bit/rustc.main.ConstProp.after.mir

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,19 +30,19 @@ fn main() -> () {
3030
}
3131

3232
alloc0 (static: FOO, size: 8, align: 4) {
33-
alloc21+0x0 (4 ptr bytes)╼ 03 00 00 00 │ ╾──╼....
33+
─a21+0x0╼ 03 00 00 00 │ ╾──╼....
3434
}
3535

3636
alloc21 (size: 48, align: 4) {
37-
0x00 │ 00 00 00 00 __ __ __ __ ╾alloc4+0x0 (4 ptr bytes)╼ 00 00 00 00 │ ....░░░░╾──╼....
38-
0x10 │ 00 00 00 00 __ __ __ __ ╾alloc9+0x0 (4 ptr bytes)╼ 02 00 00 00 │ ....░░░░╾──╼....
39-
0x20 │ 01 00 00 00 2a 00 00 00 ╾alloc19+0x0 (4 ptr bytes)╼ 03 00 00 00 │ ....*...╾──╼....
37+
0x00 │ 00 00 00 00 __ __ __ __ ╾─a4+0x0──╼ 00 00 00 00 │ ....░░░░╾──╼....
38+
0x10 │ 00 00 00 00 __ __ __ __ ╾─a9+0x0──╼ 02 00 00 00 │ ....░░░░╾──╼....
39+
0x20 │ 01 00 00 00 2a 00 00 00 ╾─a19+0x0╼ 03 00 00 00 │ ....*...╾──╼....
4040
}
4141

4242
alloc4 (size: 0, align: 4) {}
4343

4444
alloc9 (size: 8, align: 4) {
45-
alloc7+0x0 (4 ptr bytes)╼ ╾alloc8+0x0 (4 ptr bytes)╼ │ ╾──╼╾──╼
45+
─a7+0x0──╼ ╾─a8+0x0──╼ │ ╾──╼╾──╼
4646
}
4747

4848
alloc7 (size: 1, align: 1) {
@@ -54,7 +54,7 @@ alloc8 (size: 1, align: 1) {
5454
}
5555

5656
alloc19 (size: 12, align: 4) {
57-
alloc15+0x3 (4 ptr bytes)╼ ╾alloc16+0x0 (4 ptr bytes)╼ ╾alloc18+0x2 (4 ptr bytes)╼ │ ╾──╼╾──╼╾──╼
57+
─a15+0x3╼ ╾─a16+0x0╼ ╾─a18+0x2╼ │ ╾──╼╾──╼╾──╼
5858
}
5959

6060
alloc15 (size: 4, align: 1) {

src/test/mir-opt/const_allocation3/32bit/rustc.main.ConstProp.after.mir

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,20 +30,20 @@ fn main() -> () {
3030
}
3131

3232
alloc0 (static: FOO, size: 4, align: 4) {
33-
alloc9+0x0 (4 ptr bytes)╼ │ ╾──╼
33+
─a9+0x0──╼ │ ╾──╼
3434
}
3535

3636
alloc9 (size: 168, align: 1) {
3737
0x00 │ ab ab ab ab ab ab ab ab ab ab ab ab ab ab ab ab │ ................
38-
0x10 │ ab ab ab ab ab ab ab ab ab ab ab ab ╾alloc4+0x0 (4 ptr bytes)╼ │ ............╾──╼
38+
0x10 │ ab ab ab ab ab ab ab ab ab ab ab ab ╾─a4+0x0──╼ │ ............╾──╼
3939
0x20 │ 01 ef cd ab 00 00 00 00 00 00 00 00 00 00 00 00 │ ................
4040
0x30 │ 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 │ ................
4141
0x40 │ 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 │ ................
4242
0x50 │ 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 │ ................
4343
0x60 │ 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 │ ................
4444
0x70 │ 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 │ ................
45-
0x80 │ 00 00 00 00 00 00 00 00 00 00 ╾alloc6+0x0 (4 ptr bytes)╼ 00 00 │ ..........╾──╼..
46-
0x90 │ ╾alloc7+0x63 (4 ptr bytes)╼ 00 00 00 00 00 00 00 00 00 00 00 00 │ ╾──╼............
45+
0x80 │ 00 00 00 00 00 00 00 00 00 00 ╾─a6+0x0──╼ 00 00 │ ..........╾──╼..
46+
0x90 │ ╾─a7+0x63╼ 00 00 00 00 00 00 00 00 00 00 00 00 │ ╾──╼............
4747
0xa0 │ 00 00 00 00 00 00 00 00 │ ........
4848
}
4949

0 commit comments

Comments
 (0)