Skip to content

Commit 3fccdb3

Browse files
committed
Pointer printing: do not print 0 offset
1 parent 09c817e commit 3fccdb3

18 files changed

+107
-99
lines changed

src/librustc_middle/mir/interpret/pointer.rs

+18-10
Original file line numberDiff line numberDiff line change
@@ -89,27 +89,35 @@ pub struct Pointer<Tag = ()> {
8989

9090
static_assert_size!(Pointer, 16);
9191

92+
/// Print the address of a pointer (without the tag)
93+
fn print_ptr_addr<Tag>(ptr: &Pointer<Tag>, f: &mut fmt::Formatter<'_>) -> fmt::Result {
94+
// Forward `alternate` flag to `alloc_id` printing.
95+
if f.alternate() {
96+
write!(f, "{:#?}", ptr.alloc_id)?;
97+
} else {
98+
write!(f, "{:?}", ptr.alloc_id)?;
99+
}
100+
// Print offset only if it is non-zero.
101+
if ptr.offset.bytes() > 0 {
102+
write!(f, "+0x{:x}", ptr.offset.bytes())?;
103+
}
104+
Ok(())
105+
}
106+
92107
// We want the `Debug` output to be readable as it is used by `derive(Debug)` for
93108
// all the Miri types.
94109
// We have to use `Debug` output for the tag, because `()` does not implement
95110
// `Display` so we cannot specialize that.
96111
impl<Tag: fmt::Debug> fmt::Debug for Pointer<Tag> {
97112
default fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
98-
if f.alternate() {
99-
write!(f, "{:#?}+0x{:x}[{:?}]", self.alloc_id, self.offset.bytes(), self.tag)
100-
} else {
101-
write!(f, "{:?}+0x{:x}[{:?}]", self.alloc_id, self.offset.bytes(), self.tag)
102-
}
113+
print_ptr_addr(self, f)?;
114+
write!(f, "[{:?}]", self.tag)
103115
}
104116
}
105117
// Specialization for no tag
106118
impl fmt::Debug for Pointer<()> {
107119
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
108-
if f.alternate() {
109-
write!(f, "{:#?}+0x{:x}", self.alloc_id, self.offset.bytes())
110-
} else {
111-
write!(f, "{:?}+0x{:x}", self.alloc_id, self.offset.bytes())
112-
}
120+
print_ptr_addr(self, f)
113121
}
114122
}
115123

src/test/mir-opt/byte_slice/rustc.main.SimplifyCfg-elaborate-drops.after.mir

+2-2
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@ fn main() -> () {
1616
_1 = const b"foo"; // scope 0 at $DIR/byte_slice.rs:5:13: 5:19
1717
// ty::Const
1818
// + ty: &[u8; 3]
19-
// + val: Value(Scalar(alloc0+0x0))
19+
// + val: Value(Scalar(alloc0))
2020
// mir::Constant
2121
// + span: $DIR/byte_slice.rs:5:13: 5:19
22-
// + literal: Const { ty: &[u8; 3], val: Value(Scalar(alloc0+0x0)) }
22+
// + literal: Const { ty: &[u8; 3], val: Value(Scalar(alloc0)) }
2323
StorageLive(_2); // scope 1 at $DIR/byte_slice.rs:6:9: 6:10
2424
_2 = [const 5u8, const 120u8]; // scope 1 at $DIR/byte_slice.rs:6:13: 6:24
2525
// ty::Const

src/test/mir-opt/const-promotion-extern-static/rustc.BAR-promoted[0].ConstProp.after.mir

+3-3
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,13 @@ promoted[0] in BAR: &[&i32; 1] = {
77
let mut _3: &i32; // in scope 0 at $DIR/const-promotion-extern-static.rs:9:33: 9:34
88

99
bb0: {
10-
_3 = const {alloc0+0x0: &i32}; // scope 0 at $DIR/const-promotion-extern-static.rs:9:33: 9:34
10+
_3 = const {alloc0: &i32}; // scope 0 at $DIR/const-promotion-extern-static.rs:9:33: 9:34
1111
// ty::Const
1212
// + ty: &i32
13-
// + val: Value(Scalar(alloc0+0x0))
13+
// + val: Value(Scalar(alloc0))
1414
// mir::Constant
1515
// + span: $DIR/const-promotion-extern-static.rs:9:33: 9:34
16-
// + literal: Const { ty: &i32, val: Value(Scalar(alloc0+0x0)) }
16+
// + literal: Const { ty: &i32, val: Value(Scalar(alloc0)) }
1717
_2 = _3; // scope 0 at $DIR/const-promotion-extern-static.rs:9:32: 9:34
1818
_1 = [move _2]; // scope 0 at $DIR/const-promotion-extern-static.rs:9:31: 9:35
1919
_0 = &_1; // scope 0 at $DIR/const-promotion-extern-static.rs:9:31: 9:35

src/test/mir-opt/const-promotion-extern-static/rustc.BAR.PromoteTemps.diff

+3-3
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,16 @@
1616
- StorageLive(_3); // scope 0 at $DIR/const-promotion-extern-static.rs:9:31: 9:35
1717
- StorageLive(_4); // scope 0 at $DIR/const-promotion-extern-static.rs:9:32: 9:34
1818
- StorageLive(_5); // scope 0 at $DIR/const-promotion-extern-static.rs:9:33: 9:34
19-
- _5 = const {alloc0+0x0: &i32}; // scope 0 at $DIR/const-promotion-extern-static.rs:9:33: 9:34
19+
- _5 = const {alloc0: &i32}; // scope 0 at $DIR/const-promotion-extern-static.rs:9:33: 9:34
2020
+ _6 = const BAR::promoted[0]; // scope 0 at $DIR/const-promotion-extern-static.rs:9:31: 9:35
2121
// ty::Const
2222
- // + ty: &i32
23-
- // + val: Value(Scalar(alloc0+0x0))
23+
- // + val: Value(Scalar(alloc0))
2424
+ // + ty: &[&i32; 1]
2525
+ // + val: Unevaluated(DefId(0:6 ~ const_promotion_extern_static[317d]::BAR[0]), [], Some(promoted[0]))
2626
// mir::Constant
2727
- // + span: $DIR/const-promotion-extern-static.rs:9:33: 9:34
28-
- // + literal: Const { ty: &i32, val: Value(Scalar(alloc0+0x0)) }
28+
- // + literal: Const { ty: &i32, val: Value(Scalar(alloc0)) }
2929
- _4 = &(*_5); // scope 0 at $DIR/const-promotion-extern-static.rs:9:32: 9:34
3030
- _3 = [move _4]; // scope 0 at $DIR/const-promotion-extern-static.rs:9:31: 9:35
3131
- _2 = &_3; // scope 0 at $DIR/const-promotion-extern-static.rs:9:31: 9:35

src/test/mir-opt/const-promotion-extern-static/rustc.FOO-promoted[0].ConstProp.after.mir

+3-3
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,13 @@ promoted[0] in FOO: &[&i32; 1] = {
99
}
1010

1111
bb0: {
12-
_3 = const {alloc2+0x0: &i32}; // scope 0 at $DIR/const-promotion-extern-static.rs:13:42: 13:43
12+
_3 = const {alloc2: &i32}; // scope 0 at $DIR/const-promotion-extern-static.rs:13:42: 13:43
1313
// ty::Const
1414
// + ty: &i32
15-
// + val: Value(Scalar(alloc2+0x0))
15+
// + val: Value(Scalar(alloc2))
1616
// mir::Constant
1717
// + span: $DIR/const-promotion-extern-static.rs:13:42: 13:43
18-
// + literal: Const { ty: &i32, val: Value(Scalar(alloc2+0x0)) }
18+
// + literal: Const { ty: &i32, val: Value(Scalar(alloc2)) }
1919
_2 = _3; // scope 0 at $DIR/const-promotion-extern-static.rs:13:41: 13:43
2020
_1 = [move _2]; // scope 0 at $DIR/const-promotion-extern-static.rs:13:31: 13:46
2121
_0 = &_1; // scope 0 at $DIR/const-promotion-extern-static.rs:13:31: 13:46

src/test/mir-opt/const-promotion-extern-static/rustc.FOO.PromoteTemps.diff

+3-3
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,16 @@
1818
- StorageLive(_3); // scope 0 at $DIR/const-promotion-extern-static.rs:13:31: 13:46
1919
- StorageLive(_4); // scope 0 at $DIR/const-promotion-extern-static.rs:13:32: 13:45
2020
- StorageLive(_5); // scope 1 at $DIR/const-promotion-extern-static.rs:13:42: 13:43
21-
- _5 = const {alloc2+0x0: &i32}; // scope 1 at $DIR/const-promotion-extern-static.rs:13:42: 13:43
21+
- _5 = const {alloc2: &i32}; // scope 1 at $DIR/const-promotion-extern-static.rs:13:42: 13:43
2222
+ _6 = const FOO::promoted[0]; // scope 0 at $DIR/const-promotion-extern-static.rs:13:31: 13:46
2323
// ty::Const
2424
- // + ty: &i32
25-
- // + val: Value(Scalar(alloc2+0x0))
25+
- // + val: Value(Scalar(alloc2))
2626
+ // + ty: &[&i32; 1]
2727
+ // + val: Unevaluated(DefId(0:7 ~ const_promotion_extern_static[317d]::FOO[0]), [], Some(promoted[0]))
2828
// mir::Constant
2929
- // + span: $DIR/const-promotion-extern-static.rs:13:42: 13:43
30-
- // + literal: Const { ty: &i32, val: Value(Scalar(alloc2+0x0)) }
30+
- // + literal: Const { ty: &i32, val: Value(Scalar(alloc2)) }
3131
- _4 = &(*_5); // scope 1 at $DIR/const-promotion-extern-static.rs:13:41: 13:43
3232
- _3 = [move _4]; // scope 0 at $DIR/const-promotion-extern-static.rs:13:31: 13:46
3333
- _2 = &_3; // scope 0 at $DIR/const-promotion-extern-static.rs:13:31: 13:46

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

+10-10
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,13 @@ fn main() -> () {
88
bb0: {
99
StorageLive(_1); // scope 0 at $DIR/const_allocation.rs:8:5: 8:8
1010
StorageLive(_2); // scope 0 at $DIR/const_allocation.rs:8:5: 8:8
11-
_2 = const {alloc0+0x0: &&[(std::option::Option<i32>, &[&str])]}; // scope 0 at $DIR/const_allocation.rs:8:5: 8:8
11+
_2 = const {alloc0: &&[(std::option::Option<i32>, &[&str])]}; // scope 0 at $DIR/const_allocation.rs:8:5: 8:8
1212
// ty::Const
1313
// + ty: &&[(std::option::Option<i32>, &[&str])]
14-
// + val: Value(Scalar(alloc0+0x0))
14+
// + val: Value(Scalar(alloc0))
1515
// mir::Constant
1616
// + span: $DIR/const_allocation.rs:8:5: 8:8
17-
// + literal: Const { ty: &&[(std::option::Option<i32>, &[&str])], val: Value(Scalar(alloc0+0x0)) }
17+
// + literal: Const { ty: &&[(std::option::Option<i32>, &[&str])], val: Value(Scalar(alloc0)) }
1818
_1 = (*_2); // scope 0 at $DIR/const_allocation.rs:8:5: 8:8
1919
StorageDead(_2); // scope 0 at $DIR/const_allocation.rs:8:8: 8:9
2020
StorageDead(_1); // scope 0 at $DIR/const_allocation.rs:8:8: 8:9
@@ -30,19 +30,19 @@ fn main() -> () {
3030
}
3131

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

3636
alloc17 (size: 48, align: 4) {
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 │ ....*...╾──╼....
37+
0x00 │ 00 00 00 00 __ __ __ __ ╾─alloc4──╼ 00 00 00 00 │ ....░░░░╾──╼....
38+
0x10 │ 00 00 00 00 __ __ __ __ ╾─alloc8──╼ 02 00 00 00 │ ....░░░░╾──╼....
39+
0x20 │ 01 00 00 00 2a 00 00 00 ╾─alloc13─╼ 03 00 00 00 │ ....*...╾──╼....
4040
}
4141

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

4444
alloc8 (size: 16, align: 4) {
45-
╾─a7+0x0──╼ 03 00 00 00 ╾─a9+0x0──╼ 03 00 00 00 │ ╾──╼....╾──╼....
45+
╾─alloc7──╼ 03 00 00 00 ╾─alloc9──╼ 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 │ ╾─a12+0x0─╼ 03 00 00 00 ╾─a14+0x0─╼ 03 00 00 00 │ ╾──╼....╾──╼....
58-
0x10 │ ╾─a15+0x0─╼ 04 00 00 00 │ ╾──╼....
57+
0x00 │ ╾─alloc12─╼ 03 00 00 00 ╾─alloc14─╼ 03 00 00 00 │ ╾──╼....╾──╼....
58+
0x10 │ ╾─alloc15─╼ 04 00 00 00 │ ╾──╼....
5959
}
6060

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

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

+12-12
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,13 @@ fn main() -> () {
88
bb0: {
99
StorageLive(_1); // scope 0 at $DIR/const_allocation.rs:8:5: 8:8
1010
StorageLive(_2); // scope 0 at $DIR/const_allocation.rs:8:5: 8:8
11-
_2 = const {alloc0+0x0: &&[(std::option::Option<i32>, &[&str])]}; // scope 0 at $DIR/const_allocation.rs:8:5: 8:8
11+
_2 = const {alloc0: &&[(std::option::Option<i32>, &[&str])]}; // scope 0 at $DIR/const_allocation.rs:8:5: 8:8
1212
// ty::Const
1313
// + ty: &&[(std::option::Option<i32>, &[&str])]
14-
// + val: Value(Scalar(alloc0+0x0))
14+
// + val: Value(Scalar(alloc0))
1515
// mir::Constant
1616
// + span: $DIR/const_allocation.rs:8:5: 8:8
17-
// + literal: Const { ty: &&[(std::option::Option<i32>, &[&str])], val: Value(Scalar(alloc0+0x0)) }
17+
// + literal: Const { ty: &&[(std::option::Option<i32>, &[&str])], val: Value(Scalar(alloc0)) }
1818
_1 = (*_2); // scope 0 at $DIR/const_allocation.rs:8:5: 8:8
1919
StorageDead(_2); // scope 0 at $DIR/const_allocation.rs:8:8: 8:9
2020
StorageDead(_1); // scope 0 at $DIR/const_allocation.rs:8:8: 8:9
@@ -30,22 +30,22 @@ fn main() -> () {
3030
}
3131

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

3636
alloc17 (size: 72, align: 8) {
37-
0x00 │ 00 00 00 00 __ __ __ __ ╾─────alloc4+0x0──────╼ │ ....░░░░╾──────╼
37+
0x00 │ 00 00 00 00 __ __ __ __ ╾───────alloc4────────╼ │ ....░░░░╾──────╼
3838
0x10 │ 00 00 00 00 00 00 00 00 00 00 00 00 __ __ __ __ │ ............░░░░
39-
0x20 │ ╾─────alloc8+0x0──────╼ 02 00 00 00 00 00 00 00 │ ╾──────╼........
40-
0x30 │ 01 00 00 00 2a 00 00 00 ╾─────alloc13+0x0─────╼ │ ....*...╾──────╼
39+
0x20 │ ╾───────alloc8────────╼ 02 00 00 00 00 00 00 00 │ ╾──────╼........
40+
0x30 │ 01 00 00 00 2a 00 00 00 ╾───────alloc13───────╼ │ ....*...╾──────╼
4141
0x40 │ 03 00 00 00 00 00 00 00 │ ........
4242
}
4343

4444
alloc4 (size: 0, align: 8) {}
4545

4646
alloc8 (size: 32, align: 8) {
47-
0x00 │ ╾─────alloc7+0x0──────╼ 03 00 00 00 00 00 00 00 │ ╾──────╼........
48-
0x10 │ ╾─────alloc9+0x0──────╼ 03 00 00 00 00 00 00 00 │ ╾──────╼........
47+
0x00 │ ╾───────alloc7────────╼ 03 00 00 00 00 00 00 00 │ ╾──────╼........
48+
0x10 │ ╾───────alloc9────────╼ 03 00 00 00 00 00 00 00 │ ╾──────╼........
4949
}
5050

5151
alloc7 (size: 3, align: 1) {
@@ -57,9 +57,9 @@ alloc9 (size: 3, align: 1) {
5757
}
5858

5959
alloc13 (size: 48, align: 8) {
60-
0x00 │ ╾─────alloc12+0x0─────╼ 03 00 00 00 00 00 00 00 │ ╾──────╼........
61-
0x10 │ ╾─────alloc14+0x0─────╼ 03 00 00 00 00 00 00 00 │ ╾──────╼........
62-
0x20 │ ╾─────alloc15+0x0─────╼ 04 00 00 00 00 00 00 00 │ ╾──────╼........
60+
0x00 │ ╾───────alloc12───────╼ 03 00 00 00 00 00 00 00 │ ╾──────╼........
61+
0x10 │ ╾───────alloc14───────╼ 03 00 00 00 00 00 00 00 │ ╾──────╼........
62+
0x20 │ ╾───────alloc15───────╼ 04 00 00 00 00 00 00 00 │ ╾──────╼........
6363
}
6464

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

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

+9-9
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,13 @@ fn main() -> () {
88
bb0: {
99
StorageLive(_1); // scope 0 at $DIR/const_allocation2.rs:5:5: 5:8
1010
StorageLive(_2); // scope 0 at $DIR/const_allocation2.rs:5:5: 5:8
11-
_2 = const {alloc0+0x0: &&[(std::option::Option<i32>, &[&u8])]}; // scope 0 at $DIR/const_allocation2.rs:5:5: 5:8
11+
_2 = const {alloc0: &&[(std::option::Option<i32>, &[&u8])]}; // scope 0 at $DIR/const_allocation2.rs:5:5: 5:8
1212
// ty::Const
1313
// + ty: &&[(std::option::Option<i32>, &[&u8])]
14-
// + val: Value(Scalar(alloc0+0x0))
14+
// + val: Value(Scalar(alloc0))
1515
// mir::Constant
1616
// + span: $DIR/const_allocation2.rs:5:5: 5:8
17-
// + literal: Const { ty: &&[(std::option::Option<i32>, &[&u8])], val: Value(Scalar(alloc0+0x0)) }
17+
// + literal: Const { ty: &&[(std::option::Option<i32>, &[&u8])], val: Value(Scalar(alloc0)) }
1818
_1 = (*_2); // scope 0 at $DIR/const_allocation2.rs:5:5: 5:8
1919
StorageDead(_2); // scope 0 at $DIR/const_allocation2.rs:5:8: 5:9
2020
StorageDead(_1); // scope 0 at $DIR/const_allocation2.rs:5:8: 5:9
@@ -30,19 +30,19 @@ fn main() -> () {
3030
}
3131

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

3636
alloc21 (size: 48, align: 4) {
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 │ ....*...╾──╼....
37+
0x00 │ 00 00 00 00 __ __ __ __ ╾─alloc4──╼ 00 00 00 00 │ ....░░░░╾──╼....
38+
0x10 │ 00 00 00 00 __ __ __ __ ╾─alloc9──╼ 02 00 00 00 │ ....░░░░╾──╼....
39+
0x20 │ 01 00 00 00 2a 00 00 00 ╾─alloc19─╼ 03 00 00 00 │ ....*...╾──╼....
4040
}
4141

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

4444
alloc9 (size: 8, align: 4) {
45-
╾─a7+0x0──╼ ╾─a8+0x0──╼ │ ╾──╼╾──╼
45+
╾─alloc7──╼ ╾─alloc8──╼ │ ╾──╼╾──╼
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-
╾─a15+0x3─╼ ╾─a16+0x0─╼ ╾─a18+0x2─╼ │ ╾──╼╾──╼╾──╼
57+
╾─a15+0x3─╼ ╾─alloc16─╼ ╾─a18+0x2─╼ │ ╾──╼╾──╼╾──╼
5858
}
5959

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

0 commit comments

Comments
 (0)