Skip to content

Codegen: an enum whose payload type is declared later is mis-laid-out — silent truncation (wrong value) or LLVM TypeSize crash; blocks §10.9 composition with a facade's <R>Error #1430

Closed

Description

@ehartford

Summary

A non-generic enum whose variant payload contains a type declared later in the declaration order gets a wrong layout:

  • If the later type is a struct, the compiler crashes with an LLVM error.
  • If the later type is another enum, the payload is silently truncated and the program reads wrong values (a miscompile).

Repros (seed at 8c859a7 / main 04e4934)

Enum payload of a later-declared enum: the match yields got 0, not got 7.

enum Outer:
    Inner(i: InnerE)

fn main:
    let o = Outer.Inner(InnerE.A(7))
    match o:
        Outer.Inner(InnerE.A(k)) => print(f"got {k}")

enum InnerE:
    A(n: i32)

Enum payload of a later-declared struct: LLVM ERROR: Cannot implicitly convert a scalable size to a fixed-width size in TypeSize::operator ScalarTy(), exit 134.

enum OuterE:
    Has(i: Inner)

fn main:
    match OuterE.Has(Inner { x: 7, y: 8 }):
        OuterE.Has(v) => print(f"struct-in-enum {v.y}")

type Inner { x: i64, y: i64 }

§10.9 error composition: BUG: no payload value for enum type 135 variant 0 field 0 while formatting [src/CodegenDispatch.w:2966 Codegen.gen_display_enum_variant].

error AppError from DbError

fn main:
    let e = AppError.Db(DbError.Failed(3))
    print("x")

error DbError =
    | Failed(status: i32)

A struct holding a later-declared struct is fine (type Outer { i: Inner } before type Inner).

Root cause (read, not yet proven in lldb)

  • src/Codegen.w declare_enum_type computes each variant's payload struct and then let sz = self.abi_size_of(payload_ty), which fixes the enum's [N x i8] payload array at definition time.
  • Pass 0b (src/Codegen.w ~6272-6308) defines struct and enum bodies in declaration order.
  • A payload type declared later is still a predeclared named struct with no body at that point. Its size is either 0, for a not-yet-bodied enum (so the payload array is dropped and the data is truncated), or it triggers the LLVM TypeSize crash, for a not-yet-bodied struct.
  • Struct bodies are unaffected because LLVM sizes named struct fields lazily. The enum path is the only one that sizes eagerly.

Why it matters now

Modeled C (D51) renders facade types (<facade NAME>) after every user declaration, so:

  • A user enum holding a facade resource by value crashes the compiler: enum Conn: Open(db: Database) | Closed → LLVM ERROR. Checked on stage1 of modeled-c-stage5, and the same shape applies since stage 4.
  • Stage 5's ok projection renders error DatabaseError = after the user's error AppError from DatabaseError, the §10.9 composition Eric asked for (2026-09-23, Modeled C stage 5: production and status (ruling §15-§19) #1426), so that composition hits the Display BUG above.

Fix direction

  • Define enum bodies after every struct and union body.
  • Order the enums among themselves so that an enum carrying another enum by value is defined after it. A cycle by value is an infinite-size type and must be a loud error.
  • This is an enum layout change, so it belongs alone in its batch (ABI isolation). It only changes enums that are broken today.

Expected

Declaration order never changes layout: got 7, no crash, and the error composes.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions