Skip to content

Commit 55b6063

Browse files
committed
Fix cycle error only occurring with -Zdump-mir
1 parent 6f2ca60 commit 55b6063

4 files changed

+33
-16
lines changed

compiler/rustc_middle/src/mir/pretty.rs

+15-9
Original file line numberDiff line numberDiff line change
@@ -1555,16 +1555,22 @@ 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+
&& tcx.hir().body_const_context(body.source.def_id()).is_some()
1561+
{
1562+
// Statics may be cyclic and evaluating them too early
1563+
// in the MIR pipeline may cause cycle errors even though
1564+
// normal compilation is fine.
1565+
write!(w, ")")?;
1566+
} else {
1567+
match tcx.eval_static_initializer(did) {
1568+
Ok(alloc) => {
1569+
write!(w, ", ")?;
1570+
write_allocation_track_relocs(w, alloc)?;
1571+
}
1572+
Err(_) => write!(w, ", error during initializer evaluation)")?,
15621573
}
1563-
Err(_) => write!(
1564-
w,
1565-
" (static: {}, error during initializer evaluation)",
1566-
tcx.def_path_str(did)
1567-
)?,
15681574
}
15691575
}
15701576
Some(GlobalAlloc::Static(did)) => {
+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
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+
const fn thing() -> &'static Thing {
9+
&MUTUALLY_RECURSIVE
10+
}
11+
12+
pub static MUTUALLY_RECURSIVE: Thing = Thing { next: thing() };
13+
// CHECK: static MUTUALLY_RECURSIVE
14+
15+
fn main() {}

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

+1-3
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

+2-4
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

0 commit comments

Comments
 (0)