Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
dtolnay opened this issue Dec 12, 2024 · 2 comments · Fixed by #134498
Closed

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

dtolnay opened this issue Dec 12, 2024 · 2 comments · Fixed by #134498
Labels
-Zdump-mir Unstable option: -Zdump-mir C-bug Category: This is a bug. I-cycle Issue: A query cycle occurred while none was expected requires-nightly This issue requires a nightly compiler in some way. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@dtolnay
Copy link
Member

dtolnay commented Dec 12, 2024

// 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
@dtolnay dtolnay added C-bug Category: This is a bug. I-cycle Issue: A query cycle occurred while none was expected requires-nightly This issue requires a nightly compiler in some way. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Dec 12, 2024
@rustbot rustbot added the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Dec 12, 2024
@jieyouxu jieyouxu added -Zdump-mir Unstable option: -Zdump-mir and removed needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. labels Dec 12, 2024
@compiler-errors
Copy link
Member

cc @RalfJung, I think we probably shouldn't be printing the allocations of statics when dumping MIR (which requires computing the CTFE MIR so they can actually be evaluated), or at least we shouldn't be doing it until after optimized-mir is computed and not during the query itself.

@RalfJung
Copy link
Member

Cc @oli-obk

matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Jan 13, 2025
…ler-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.
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Jan 13, 2025
…ler-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.
@bors bors closed this as completed in 81f7429 Jan 14, 2025
rust-timer added a commit to rust-lang-ci/rust that referenced this issue Jan 14, 2025
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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
-Zdump-mir Unstable option: -Zdump-mir C-bug Category: This is a bug. I-cycle Issue: A query cycle occurred while none was expected requires-nightly This issue requires a nightly compiler in some way. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants