Fix cycle error only occurring with -Zdump-mir

This commit is contained in:
Oli Scherer 2024-12-18 18:37:54 +00:00
parent 88ab2d8acb
commit 15c01eb22c
4 changed files with 37 additions and 16 deletions

View file

@ -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) => {
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, " (static: {}, ", tcx.def_path_str(did))?;
write!(w, ", ")?;
write_allocation_track_relocs(w, alloc)?;
}
Err(_) => write!(
w,
" (static: {}, error during initializer evaluation)",
tcx.def_path_str(did)
)?,
Err(_) => write!(w, ", error during initializer evaluation)")?,
}
}
}
Some(GlobalAlloc::Static(did)) => {

View file

@ -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();
}

View file

@ -15,6 +15,4 @@ const BAR::promoted[0]: &[&i32; 1] = {
}
}
ALLOC0 (static: Y, size: 4, align: 4) {
2a 00 00 00 *...
}
ALLOC0 (static: Y)

View file

@ -38,9 +38,7 @@
bb2 (cleanup): {
resume;
}
- }
-
- ALLOC0 (static: Y, size: 4, align: 4) {
- 2a 00 00 00 │ *...
}
-
- ALLOC0 (static: Y)