Skip to content

Sema: a by-value cycle through Option[E] (a generic spelling) escapes the dependency-loop check; TypeLayout recurses until the stack overflows #1439

Closed

Description

@ehartford

Summary

A by-value cycle spelled through a generic value type is not caught by Sema's dependency-loop check, and the compiler then overflows its stack computing the (infinite) layout. enum E: A(o: Option[E]) is an infinite-size type; the compiler segfaults instead of reporting it.

Repro (origin/main 1c76062)

enum E:
    A(o: Option[E])
    B

fn main:
    let e = E.B
    print("unreachable")

with check / with run exit 139 (EXC_BAD_ACCESS, stack guard). The same cycle through a tuple (A(t: (i32, F)) with F: C(e: E)) is reported correctly ("dependency loop with length 2").

Root cause (lldb, base stage2)

  • src/SemaDecl.w collect_value_type_deps (the comment at ~1108): NK_TYPE_GENERIC is treated as an indirection and never followed ("NK_TYPE_PTR, NK_TYPE_REF, NK_TYPE_SLICE, NK_TYPE_GENERIC ... all provide indirection — do not follow"). Option[E], Result[E, X], and a user generic that holds its parameter by value (type Wrap[T] { t: T }) embed their arguments by value, so the cycle E -> Option[E] -> E never becomes an edge.
  • The eager layout pass then recurses forever: breakpoint on Sema.type_layout_size_of after 5000 hits shows the repeating frames TypeLayout.w:359 (type_layout_size_of) <- TypeLayout.w:295 (type_layout_enum_size_of, payload loop) <- TypeLayout.w:270 (generic-inst enum branch) <- TypeLayout.w:413 (type_layout_size_of, TY_GENERIC_INST) <- TypeLayout.w:295 ...

Expected

error: dependency loop with length 1 (or an infinite-size diagnostic naming Option[E]), as for the tuple spelling. Following a generic's arguments must stay precise: Vec[E], Box[E], HashMap[K, E] are indirections and must stay legal, so the check needs to know which parameters a generic holds by value (Option/Result: all; a user generic: those its fields/payloads hold by value). A recursion guard in TypeLayout would turn any remaining miss into a loud error instead of a stack overflow.

Found while fixing #1430 (codegen's side now refuses to size a bodiless placeholder; this cycle dies in Sema before codegen).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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