Skip to content

Commit 8cf8338

Browse files
committed
Fix cycle error only occurring with -Zdump-mir
1 parent 8d2c06d commit 8cf8338

19 files changed

+54
-310
lines changed

compiler/rustc_middle/src/mir/pretty.rs

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1555,16 +1555,20 @@ pub fn write_allocations<'tcx>(
15551555
write!(w, " (vtable: impl {dyn_ty} for {ty})")?
15561556
}
15571557
Some(GlobalAlloc::Static(did)) if !tcx.is_foreign_item(did) => {
1558-
match tcx.eval_static_initializer(did) {
1559-
Ok(alloc) => {
1560-
write!(w, " (static: {}, ", tcx.def_path_str(did))?;
1561-
write_allocation_track_relocs(w, alloc)?;
1558+
write!(w, " (static: {}", tcx.def_path_str(did))?;
1559+
if body.phase <= MirPhase::Runtime(RuntimePhase::PostCleanup) {
1560+
// Statics may be cyclic and evaluating them too early
1561+
// in the MIR pipeline may cause cycle errors even though
1562+
// normal compilation is fine.
1563+
write!(w, ")")?;
1564+
} else {
1565+
match tcx.eval_static_initializer(did) {
1566+
Ok(alloc) => {
1567+
write!(w, ", ")?;
1568+
write_allocation_track_relocs(w, alloc)?;
1569+
}
1570+
Err(_) => write!(w, ", error during initializer evaluation)")?,
15621571
}
1563-
Err(_) => write!(
1564-
w,
1565-
" (static: {}, error during initializer evaluation)",
1566-
tcx.def_path_str(did)
1567-
)?,
15681572
}
15691573
}
15701574
Some(GlobalAlloc::Static(did)) => {

tests/mir-opt/building/custom/consts.statics.built.after.mir

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,6 @@ fn statics() -> () {
1212
}
1313
}
1414

15-
ALLOC1 (static: T, size: 4, align: 4) {
16-
0a 0a 0a 0a │ ....
17-
}
15+
ALLOC1 (static: T)
1816

19-
ALLOC0 (static: S, size: 4, align: 4) {
20-
05 05 05 05 │ ....
21-
}
17+
ALLOC0 (static: S)
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
pub struct Thing {
2+
pub next: &'static Thing,
3+
}
4+
5+
pub static THING: Thing = Thing { next: &THING };
6+
// CHECK: static THING
7+
8+
fn main() {}

tests/mir-opt/const_allocation.main.GVN.after.32bit.mir

Lines changed: 2 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ fn main() -> () {
88
bb0: {
99
StorageLive(_1);
1010
StorageLive(_2);
11-
_2 = const {ALLOC9: &&[(Option<i32>, &[&str])]};
11+
_2 = const {ALLOC0: &&[(Option<i32>, &[&str])]};
1212
_1 = copy (*_2);
1313
StorageDead(_2);
1414
StorageDead(_1);
@@ -17,43 +17,4 @@ fn main() -> () {
1717
}
1818
}
1919

20-
ALLOC9 (static: FOO, size: 8, align: 4) {
21-
╾ALLOC0<imm>╼ 03 00 00 00 │ ╾──╼....
22-
}
23-
24-
ALLOC0 (size: 48, align: 4) {
25-
0x00 │ 00 00 00 00 __ __ __ __ ╾ALLOC1<imm>╼ 00 00 00 00 │ ....░░░░╾──╼....
26-
0x10 │ 00 00 00 00 __ __ __ __ ╾ALLOC2<imm>╼ 02 00 00 00 │ ....░░░░╾──╼....
27-
0x20 │ 01 00 00 00 2a 00 00 00 ╾ALLOC3<imm>╼ 03 00 00 00 │ ....*...╾──╼....
28-
}
29-
30-
ALLOC1 (size: 0, align: 4) {}
31-
32-
ALLOC2 (size: 16, align: 4) {
33-
╾ALLOC4<imm>╼ 03 00 00 00 ╾ALLOC5<imm>╼ 03 00 00 00 │ ╾──╼....╾──╼....
34-
}
35-
36-
ALLOC4 (size: 3, align: 1) {
37-
66 6f 6f │ foo
38-
}
39-
40-
ALLOC5 (size: 3, align: 1) {
41-
62 61 72 │ bar
42-
}
43-
44-
ALLOC3 (size: 24, align: 4) {
45-
0x00 │ ╾ALLOC6<imm>╼ 03 00 00 00 ╾ALLOC7<imm>╼ 03 00 00 00 │ ╾──╼....╾──╼....
46-
0x10 │ ╾ALLOC8<imm>╼ 04 00 00 00 │ ╾──╼....
47-
}
48-
49-
ALLOC6 (size: 3, align: 1) {
50-
6d 65 68 │ meh
51-
}
52-
53-
ALLOC7 (size: 3, align: 1) {
54-
6d 6f 70 │ mop
55-
}
56-
57-
ALLOC8 (size: 4, align: 1) {
58-
6d c3 b6 70 │ m..p
59-
}
20+
ALLOC0 (static: FOO)

tests/mir-opt/const_allocation.main.GVN.after.64bit.mir

Lines changed: 2 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ fn main() -> () {
88
bb0: {
99
StorageLive(_1);
1010
StorageLive(_2);
11-
_2 = const {ALLOC9: &&[(Option<i32>, &[&str])]};
11+
_2 = const {ALLOC0: &&[(Option<i32>, &[&str])]};
1212
_1 = copy (*_2);
1313
StorageDead(_2);
1414
StorageDead(_1);
@@ -17,47 +17,4 @@ fn main() -> () {
1717
}
1818
}
1919

20-
ALLOC9 (static: FOO, size: 16, align: 8) {
21-
╾ALLOC0<imm>╼ 03 00 00 00 00 00 00 00 │ ╾──────╼........
22-
}
23-
24-
ALLOC0 (size: 72, align: 8) {
25-
0x00 │ 00 00 00 00 __ __ __ __ ╾ALLOC1<imm>╼ │ ....░░░░╾──────╼
26-
0x10 │ 00 00 00 00 00 00 00 00 00 00 00 00 __ __ __ __ │ ............░░░░
27-
0x20 │ ╾ALLOC2<imm>╼ 02 00 00 00 00 00 00 00 │ ╾──────╼........
28-
0x30 │ 01 00 00 00 2a 00 00 00 ╾ALLOC3<imm>╼ │ ....*...╾──────╼
29-
0x40 │ 03 00 00 00 00 00 00 00 │ ........
30-
}
31-
32-
ALLOC1 (size: 0, align: 8) {}
33-
34-
ALLOC2 (size: 32, align: 8) {
35-
0x00 │ ╾ALLOC4<imm>╼ 03 00 00 00 00 00 00 00 │ ╾──────╼........
36-
0x10 │ ╾ALLOC5<imm>╼ 03 00 00 00 00 00 00 00 │ ╾──────╼........
37-
}
38-
39-
ALLOC4 (size: 3, align: 1) {
40-
66 6f 6f │ foo
41-
}
42-
43-
ALLOC5 (size: 3, align: 1) {
44-
62 61 72 │ bar
45-
}
46-
47-
ALLOC3 (size: 48, align: 8) {
48-
0x00 │ ╾ALLOC6<imm>╼ 03 00 00 00 00 00 00 00 │ ╾──────╼........
49-
0x10 │ ╾ALLOC7<imm>╼ 03 00 00 00 00 00 00 00 │ ╾──────╼........
50-
0x20 │ ╾ALLOC8<imm>╼ 04 00 00 00 00 00 00 00 │ ╾──────╼........
51-
}
52-
53-
ALLOC6 (size: 3, align: 1) {
54-
6d 65 68 │ meh
55-
}
56-
57-
ALLOC7 (size: 3, align: 1) {
58-
6d 6f 70 │ mop
59-
}
60-
61-
ALLOC8 (size: 4, align: 1) {
62-
6d c3 b6 70 │ m..p
63-
}
20+
ALLOC0 (static: FOO)

tests/mir-opt/const_allocation2.main.GVN.after.32bit.mir

Lines changed: 2 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ fn main() -> () {
88
bb0: {
99
StorageLive(_1);
1010
StorageLive(_2);
11-
_2 = const {ALLOC9: &&[(Option<i32>, &[&u8])]};
11+
_2 = const {ALLOC0: &&[(Option<i32>, &[&u8])]};
1212
_1 = copy (*_2);
1313
StorageDead(_2);
1414
StorageDead(_1);
@@ -17,42 +17,4 @@ fn main() -> () {
1717
}
1818
}
1919

20-
ALLOC9 (static: FOO, size: 8, align: 4) {
21-
╾ALLOC0<imm>╼ 03 00 00 00 │ ╾──╼....
22-
}
23-
24-
ALLOC0 (size: 48, align: 4) {
25-
0x00 │ 00 00 00 00 __ __ __ __ ╾ALLOC1<imm>╼ 00 00 00 00 │ ....░░░░╾──╼....
26-
0x10 │ 00 00 00 00 __ __ __ __ ╾ALLOC2<imm>╼ 02 00 00 00 │ ....░░░░╾──╼....
27-
0x20 │ 01 00 00 00 2a 00 00 00 ╾ALLOC3<imm>╼ 03 00 00 00 │ ....*...╾──╼....
28-
}
29-
30-
ALLOC1 (size: 0, align: 4) {}
31-
32-
ALLOC2 (size: 8, align: 4) {
33-
╾ALLOC4<imm>╼ ╾ALLOC5<imm>╼ │ ╾──╼╾──╼
34-
}
35-
36-
ALLOC4 (size: 1, align: 1) {
37-
05 │ .
38-
}
39-
40-
ALLOC5 (size: 1, align: 1) {
41-
06 │ .
42-
}
43-
44-
ALLOC3 (size: 12, align: 4) {
45-
╾ALLOC6+0x3<imm>╼ ╾ALLOC7<imm>╼ ╾ALLOC8+0x2<imm>╼ │ ╾──╼╾──╼╾──╼
46-
}
47-
48-
ALLOC6 (size: 4, align: 1) {
49-
2a 45 15 6f │ *E.o
50-
}
51-
52-
ALLOC7 (size: 1, align: 1) {
53-
2a │ *
54-
}
55-
56-
ALLOC8 (size: 4, align: 1) {
57-
2a 45 15 6f │ *E.o
58-
}
20+
ALLOC0 (static: FOO)

tests/mir-opt/const_allocation2.main.GVN.after.64bit.mir

Lines changed: 2 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ fn main() -> () {
88
bb0: {
99
StorageLive(_1);
1010
StorageLive(_2);
11-
_2 = const {ALLOC9: &&[(Option<i32>, &[&u8])]};
11+
_2 = const {ALLOC0: &&[(Option<i32>, &[&u8])]};
1212
_1 = copy (*_2);
1313
StorageDead(_2);
1414
StorageDead(_1);
@@ -17,45 +17,4 @@ fn main() -> () {
1717
}
1818
}
1919

20-
ALLOC9 (static: FOO, size: 16, align: 8) {
21-
╾ALLOC0<imm>╼ 03 00 00 00 00 00 00 00 │ ╾──────╼........
22-
}
23-
24-
ALLOC0 (size: 72, align: 8) {
25-
0x00 │ 00 00 00 00 __ __ __ __ ╾ALLOC1<imm>╼ │ ....░░░░╾──────╼
26-
0x10 │ 00 00 00 00 00 00 00 00 00 00 00 00 __ __ __ __ │ ............░░░░
27-
0x20 │ ╾ALLOC2<imm>╼ 02 00 00 00 00 00 00 00 │ ╾──────╼........
28-
0x30 │ 01 00 00 00 2a 00 00 00 ╾ALLOC3<imm>╼ │ ....*...╾──────╼
29-
0x40 │ 03 00 00 00 00 00 00 00 │ ........
30-
}
31-
32-
ALLOC1 (size: 0, align: 8) {}
33-
34-
ALLOC2 (size: 16, align: 8) {
35-
╾ALLOC4<imm>╼ ╾ALLOC5<imm>╼ │ ╾──────╼╾──────╼
36-
}
37-
38-
ALLOC4 (size: 1, align: 1) {
39-
05 │ .
40-
}
41-
42-
ALLOC5 (size: 1, align: 1) {
43-
06 │ .
44-
}
45-
46-
ALLOC3 (size: 24, align: 8) {
47-
0x00 │ ╾ALLOC6+0x3<imm>╼ ╾ALLOC7<imm>╼ │ ╾──────╼╾──────╼
48-
0x10 │ ╾ALLOC8+0x2<imm>╼ │ ╾──────╼
49-
}
50-
51-
ALLOC6 (size: 4, align: 1) {
52-
2a 45 15 6f │ *E.o
53-
}
54-
55-
ALLOC7 (size: 1, align: 1) {
56-
2a │ *
57-
}
58-
59-
ALLOC8 (size: 4, align: 1) {
60-
2a 45 15 6f │ *E.o
61-
}
20+
ALLOC0 (static: FOO)

tests/mir-opt/const_allocation3.main.GVN.after.32bit.mir

Lines changed: 2 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ fn main() -> () {
88
bb0: {
99
StorageLive(_1);
1010
StorageLive(_2);
11-
_2 = const {ALLOC4: &&Packed};
11+
_2 = const {ALLOC0: &&Packed};
1212
_1 = copy (*_2);
1313
StorageDead(_2);
1414
StorageDead(_1);
@@ -17,36 +17,4 @@ fn main() -> () {
1717
}
1818
}
1919

20-
ALLOC4 (static: FOO, size: 4, align: 4) {
21-
╾ALLOC0<imm>╼ │ ╾──╼
22-
}
23-
24-
ALLOC0 (size: 168, align: 1) {
25-
0x00 │ ab ab ab ab ab ab ab ab ab ab ab ab ab ab ab ab │ ................
26-
0x10 │ ab ab ab ab ab ab ab ab ab ab ab ab ╾ALLOC1<imm>╼ │ ............╾──╼
27-
0x20 │ 01 ef cd ab 00 00 00 00 00 00 00 00 00 00 00 00 │ ................
28-
0x30 │ 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 │ ................
29-
0x40 │ 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 │ ................
30-
0x50 │ 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 │ ................
31-
0x60 │ 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 │ ................
32-
0x70 │ 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 │ ................
33-
0x80 │ 00 00 00 00 00 00 00 00 00 00 ╾ALLOC2╼ 00 00 │ ..........╾──╼..
34-
0x90 │ ╾ALLOC3+0x63<imm>╼ 00 00 00 00 00 00 00 00 00 00 00 00 │ ╾──╼............
35-
0xa0 │ 00 00 00 00 00 00 00 00 │ ........
36-
}
37-
38-
ALLOC1 (size: 4, align: 4) {
39-
2a 00 00 00 │ *...
40-
}
41-
42-
ALLOC2 (fn: main)
43-
44-
ALLOC3 (size: 100, align: 1) {
45-
0x00 │ 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 │ ................
46-
0x10 │ 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 │ ................
47-
0x20 │ 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 │ ................
48-
0x30 │ 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 │ ................
49-
0x40 │ 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 │ ................
50-
0x50 │ 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 │ ................
51-
0x60 │ 00 00 00 00 │ ....
52-
}
20+
ALLOC0 (static: FOO)

tests/mir-opt/const_allocation3.main.GVN.after.64bit.mir

Lines changed: 2 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ fn main() -> () {
88
bb0: {
99
StorageLive(_1);
1010
StorageLive(_2);
11-
_2 = const {ALLOC2: &&Packed};
11+
_2 = const {ALLOC0: &&Packed};
1212
_1 = copy (*_2);
1313
StorageDead(_2);
1414
StorageDead(_1);
@@ -17,37 +17,4 @@ fn main() -> () {
1717
}
1818
}
1919

20-
ALLOC2 (static: FOO, size: 8, align: 8) {
21-
╾ALLOC0<imm>╼ │ ╾──────╼
22-
}
23-
24-
ALLOC0 (size: 180, align: 1) {
25-
0x00 │ ab ab ab ab ab ab ab ab ab ab ab ab ab ab ab ab │ ................
26-
0x10 │ ab ab ab ab ab ab ab ab ab ab ab ab ╾ALLOC3<imm> (8 ptr bytes) │ ............╾───
27-
0x20 │ ──────────╼ 01 ef cd ab 00 00 00 00 00 00 00 00 │ ───╼............
28-
0x30 │ 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 │ ................
29-
0x40 │ 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 │ ................
30-
0x50 │ 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 │ ................
31-
0x60 │ 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 │ ................
32-
0x70 │ 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 │ ................
33-
0x80 │ 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ╾──── │ ..............╾─
34-
0x90 │ ─────ALLOC4─────╼ 00 00 ╾ALLOC1+0x63<imm>╼ │ ─────╼..╾──────╼
35-
0xa0 │ 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 │ ................
36-
0xb0 │ 00 00 00 00 │ ....
37-
}
38-
39-
ALLOC3 (size: 4, align: 4) {
40-
2a 00 00 00 │ *...
41-
}
42-
43-
ALLOC4 (fn: main)
44-
45-
ALLOC1 (size: 100, align: 1) {
46-
0x00 │ 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 │ ................
47-
0x10 │ 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 │ ................
48-
0x20 │ 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 │ ................
49-
0x30 │ 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 │ ................
50-
0x40 │ 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 │ ................
51-
0x50 │ 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 │ ................
52-
0x60 │ 00 00 00 00 │ ....
53-
}
20+
ALLOC0 (static: FOO)

tests/mir-opt/const_promotion_extern_static.BAR-promoted[0].SimplifyCfg-pre-optimizations.after.mir

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,4 @@ const BAR::promoted[0]: &[&i32; 1] = {
1515
}
1616
}
1717

18-
ALLOC0 (static: Y, size: 4, align: 4) {
19-
2a 00 00 00 │ *...
20-
}
18+
ALLOC0 (static: Y)

tests/mir-opt/const_promotion_extern_static.BAR.PromoteTemps.diff

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,7 @@
3838
bb2 (cleanup): {
3939
resume;
4040
}
41-
- }
42-
-
43-
- ALLOC0 (static: Y, size: 4, align: 4) {
44-
- 2a 00 00 00 │ *...
4541
}
42+
-
43+
- ALLOC0 (static: Y)
4644

tests/mir-opt/const_prop/mutable_variable_no_prop.main.GVN.diff

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,5 @@
3737
}
3838
}
3939

40-
ALLOC0 (static: STATIC, size: 4, align: 4) {
41-
42 42 42 42 │ BBBB
42-
}
40+
ALLOC0 (static: STATIC)
4341

0 commit comments

Comments
 (0)