Skip to content

Enum declared around the first use of a generic enum instance: variant rows interleave, match segfaults #1448

Closed

Description

@ehartford

Summary

An enum whose payload is the first use of a generic enum instance segfaults at runtime, in declaration order: the instance's variant rows are pushed into the middle of the enclosing enum's variant range while codegen declares it.

Repro (origin/main 1c76062)

enum H[T]:
    X(t: T)
    Y

enum Plain:
    P(h: H[i64])
    Q(n: i64)

fn show_h(h: H[i64]):
    match h:
        H.X(t) => print(f"X {t}")
        H.Y => print("Y")

fn main:
    let p = Plain.Q(2222222222222)
    match p:
        Plain.P(h) => show_h(h)
        Plain.Q(n) => print(f"Q {n}")
    let p2 = Plain.P(H.X(4444444444444))
    match p2:
        Plain.P(h) => show_h(h)
        Plain.Q(n) => print(f"Q {n}")

Exit 139; expected Q 2222222222222 / X 4444444444444. Declaring type Pre { h: H[i64] } above Plain (so H[i64] is instantiated before Plain is declared) makes it print the expected output.

Root cause

Codegen.declare_enum_type took v_starts = enum_variant_names.len() and then pushed one row per variant after resolving that variant's payload. Resolving H[i64] runs get_or_create_generic_enum_type, which pushes H's two rows first, so Plain's recorded range [v_starts, v_starts + 2) is H's X/Y rows and Plain's own rows land after them. get_or_create_generic_enum_type and declare_disc_enum_type had the same shape. Structs already reserve their rows before resolving fields (reserve_struct_fields).

Status

Fixed in the #1430 PR: defining type bodies on first reference makes this recursion the normal case, so every enum path now reserves its variant rows before resolving payloads (reserve_enum_variants). The repro above is covered by the #1430 fixtures' shapes; filed so the symptom is on record.

Activity

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