Skip to content

Commit

Permalink
Unrolled build for rust-lang#134498
Browse files Browse the repository at this point in the history
Rollup merge of rust-lang#134498 - oli-obk:push-wmxynprsyxvr, r=compiler-errors

Fix cycle error only occurring with -Zdump-mir

fixes rust-lang#134205

During mir dumping, we evaluate static items to render their allocations. If a static item refers to itself, its own MIR will have a reference to itself, so during mir dumping we end up evaluating the static again, causing us to try to build MIR again (mir dumping happens during MIR building).

Thus I disabled evaluation of statics during MIR dumps in case the MIR body isn't far enough along yet to be able to be guaranteed cycle free.
  • Loading branch information
rust-timer authored Jan 14, 2025
2 parents 1ab85fb + 15c01eb commit ce6d8fb
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 16 deletions.
24 changes: 15 additions & 9 deletions compiler/rustc_middle/src/mir/pretty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1555,16 +1555,22 @@ pub fn write_allocations<'tcx>(
write!(w, " (vtable: impl {dyn_ty} for {ty})")?
}
Some(GlobalAlloc::Static(did)) if !tcx.is_foreign_item(did) => {
match tcx.eval_static_initializer(did) {
Ok(alloc) => {
write!(w, " (static: {}, ", tcx.def_path_str(did))?;
write_allocation_track_relocs(w, alloc)?;
write!(w, " (static: {}", tcx.def_path_str(did))?;
if body.phase <= MirPhase::Runtime(RuntimePhase::PostCleanup)
&& tcx.hir().body_const_context(body.source.def_id()).is_some()
{
// Statics may be cyclic and evaluating them too early
// in the MIR pipeline may cause cycle errors even though
// normal compilation is fine.
write!(w, ")")?;
} else {
match tcx.eval_static_initializer(did) {
Ok(alloc) => {
write!(w, ", ")?;
write_allocation_track_relocs(w, alloc)?;
}
Err(_) => write!(w, ", error during initializer evaluation)")?,
}
Err(_) => write!(
w,
" (static: {}, error during initializer evaluation)",
tcx.def_path_str(did)
)?,
}
}
Some(GlobalAlloc::Static(did)) => {
Expand Down
19 changes: 19 additions & 0 deletions tests/mir-opt/building/dump_mir_cycle.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#[derive(Debug)]
pub struct Thing {
pub next: &'static Thing,
}

pub static THING: Thing = Thing { next: &THING };
// CHECK: alloc{{.+}} (static: THING)

const fn thing() -> &'static Thing {
&MUTUALLY_RECURSIVE
}

pub static MUTUALLY_RECURSIVE: Thing = Thing { next: thing() };
// CHECK: alloc{{.+}} (static: MUTUALLY_RECURSIVE)

fn main() {
// Generate optimized MIR for the const fn, too.
thing();
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,4 @@ const BAR::promoted[0]: &[&i32; 1] = {
}
}

ALLOC0 (static: Y, size: 4, align: 4) {
2a 00 00 00*...
}
ALLOC0 (static: Y)
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,7 @@
bb2 (cleanup): {
resume;
}
- }
-
- ALLOC0 (static: Y, size: 4, align: 4) {
- 2a 00 00 00 │ *...
}
-
- ALLOC0 (static: Y)

0 comments on commit ce6d8fb

Please sign in to comment.