Skip to content

Compiler hangs with uninitialized variable containing a struct that contains itself #153205

@theemathas

Description

@theemathas

The following code causes rustc to hang when built with cargo run --release

trait Apply {
    type Output<T>;
}
struct Identity;
impl Apply for Identity {
    type Output<T> = T;
}

struct Thing<A: Apply>(A::Output<Self>);

/*
Effectively:
struct Thing_Identity(Thing_Identity);
*/

fn foo<A: Apply>() {
    let _x: Thing<A>;
}

fn main() {
    foo::<Identity>();
}

Compiler output before the hang:

   Compiling playground v0.0.1 (/playground)
error[E0391]: cycle detected when computing layout of `Thing<Identity>`
   |
   = note: ...which requires computing layout of `<Identity as Apply>::Output<Thing<Identity>>`...
   = note: ...which again requires computing layout of `Thing<Identity>`, completing the cycle
note: cycle used when optimizing MIR for `main`
  --> src/main.rs:20:1
   |
20 | fn main() {
   | ^^^^^^^^^
   = 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

See also #152885

Meta

Reproducible on the playground with version 1.95.0-nightly (2026-02-24 859951e3c7c9d0322c39)

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-layoutArea: Memory layout of typesA-mir-optArea: MIR optimizationsA-type-systemArea: Type systemC-bugCategory: This is a bug.I-hangIssue: The compiler never terminates, due to infinite loops, deadlock, livelock, etc.I-prioritizeIssue: Indicates that prioritization has been requested for this issue.S-has-bisectionStatus: A bisection has been found for this issueT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.T-typesRelevant to the types team, which will review and decide on the PR/issue.regression-from-stable-to-stablePerformance or correctness regression from one stable version to another.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions