Skip to content

-Zdump-mir introduces cycle in static containing static reference #134205

Closed
@dtolnay

Description

@dtolnay
// src/lib.rs

pub struct Thing {
    pub next: &'static Thing,
}

pub static THING: Thing = Thing { next: &THING };
$ cargo clean
$ RUSTFLAGS=-Zdump-mir= cargo build

error[E0391]: cycle detected when evaluating initializer of static `THING`
 --> src/lib.rs:7:1
  |
7 | pub static THING: Thing = Thing { next: &THING };
  | ^^^^^^^^^^^^^^^^^^^^^^^
  |
note: ...which requires caching mir of `THING` for CTFE...
 --> src/lib.rs:7:1
  |
7 | pub static THING: Thing = Thing { next: &THING };
  | ^^^^^^^^^^^^^^^^^^^^^^^
note: ...which requires elaborating drops for `THING`...
 --> src/lib.rs:7:1
  |
7 | pub static THING: Thing = Thing { next: &THING };
  | ^^^^^^^^^^^^^^^^^^^^^^^
note: ...which requires borrow-checking `THING`...
 --> src/lib.rs:7:1
  |
7 | pub static THING: Thing = Thing { next: &THING };
  | ^^^^^^^^^^^^^^^^^^^^^^^
note: ...which requires promoting constants in MIR for `THING`...
 --> src/lib.rs:7:1
  |
7 | pub static THING: Thing = Thing { next: &THING };
  | ^^^^^^^^^^^^^^^^^^^^^^^
note: ...which requires const checking `THING`...
 --> src/lib.rs:7:1
  |
7 | pub static THING: Thing = Thing { next: &THING };
  | ^^^^^^^^^^^^^^^^^^^^^^^
note: ...which requires building MIR for `THING`...
 --> src/lib.rs:7:1
  |
7 | pub static THING: Thing = Thing { next: &THING };
  | ^^^^^^^^^^^^^^^^^^^^^^^
  = note: ...which again requires evaluating initializer of static `THING`, completing the cycle
  = note: cycle used when running analysis passes on this crate
  = note: see https://rustc-dev-guide.rust-lang.org/overview.html#queries and https://rustc-dev-guide.rust-lang.org/query.html for more information

For more information about this error, try `rustc --explain E0391`.
error: could not compile `repro` (lib) due to 1 previous error

The error does not occur if you build without -Zdump-mir first.

$ cargo clean
$ cargo build
$ RUSTFLAGS=-Zdump-mir= cargo build

Meta

rustc 1.85.0-nightly (21fe748be 2024-12-11)
binary: rustc
commit-hash: 21fe748be15271ea5804e0507cd699b675efe038
commit-date: 2024-12-11
host: x86_64-unknown-linux-gnu
release: 1.85.0-nightly
LLVM version: 19.1.5

Metadata

Metadata

Assignees

No one assigned

    Labels

    -Zdump-mirUnstable option: Dump MIR.C-bugCategory: This is a bug.I-cycleIssue: A query cycle occurred while none was expectedT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.requires-nightlyThis issue requires a nightly compiler in some way.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions