Skip to content

emit-c maps every fieldless enum to int32_t regardless of repr type #990

Closed

Description

@eas4ai

Classification

Silent backend divergence: layout inflation for u8/u16 tags, value truncation for wide discriminants (audit finding CCGEN-004).

Source chain

c_type maps EVERY non-payload enum to int32_t (src/CCodegen.w:1893-1896) while LLVM uses { repr_ty, ... } (spec §13.3:2198-2199). In-tree : u8/ enums exist (test/bundle_interface/wi_demo.w:23, enum_discriminant_specified.w). Probes at 450733e (.audit/probes/readonly_pass_20260903/enumrepr{,2}_probe.w): LLVM reports sizeof[Big:i64]==8, sizeof[Small:u8]==1, and 7-billion discriminants check and run. On emit-c all three become 4-byte tags: u8/u16 inflate (struct/FFI layout divergence vs LLVM) and i64 discriminants TRUNCATE (silent value corruption). Same silent-wrong-type class as #969-adjacent residuals, but reached from ordinary user enums. (Checked: dyn dispatch is explicitly LLVM-only by design with loud fail+abort (:6102, #301) — not affected.)

Repair boundary

Map the tag from the enum's repr type in c_type (and audit the payload-enum tag path identically); add an emit-c differential test over repr widths incl. >i32max discriminants.

Activity

  1. ehartford commented on Sep 16, 2026

    @ehartford
    Contributor

    Review: reproduced, with two adjacent findings that change the picture

    Verdict. The central claim is established by running the same program through both backends: the C backend maps every fieldless enum to int32_t regardless of its declared representation type, so u8 enums inflate (a two-u8 struct is 8 bytes instead of 2) and i64 enums shrink to 4 bytes. Two things the report did not anticipate: (1) a 7-billion discriminant is already wrong before either backend, because Sema keeps discriminant values in an i32 map and falls back to the variant index when the value is absent, so both backends print 1 for Big.Huge; (2) the LLVM backend sign-extends a u8 enum when widening (Small.B as i32 gives -56 where the C backend gives 200), so the "wide discriminant truncation" divergence the report reasons about is masked by an earlier truncation, while a different LLVM-side divergence exists. Also, a match on a fieldless repr enum does not compile through the C backend at all ((_57).tag on an int32_t), which is loud rather than silent and is a separate defect.

    Tested revision and compiler

    item value
    source main @ 5809ac1 (2026-09-14); the c_type enum branch is unchanged since the reported 450733e
    compiler with build of 5809ac1 in a detached worktree, self-reports with v0.15.2.1
    C compiler Apple cc against the worktree's out/lib/{rt_core,rt_darwin_aarch64,compat_runtime,panic_runtime,fiber_stubs,cimport_stubs}.o (the set build/emit_c.w:582-587 links)
    platform macOS Darwin 25.6.0, arm64

    Repro (match-free so the C compiles)

    enum Small: u8 { A = 1, B = 200 }
    enum Big: i64 { Low = 1, Huge = 7000000000 }
    enum Color: i32 { Red = 1, Green = 2, Blue = 4 }
    type Pair { s: Small, t: Small }
    fn main:
        let b = Big.Huge
        let s = Small.B
        let c = Color.Blue
        print(f"sizeof Small={sizeof[Small]()} Big={sizeof[Big]()} Pair={sizeof[Pair]()} Color={sizeof[Color]()}")
        print(f"big_as_i64={b as i64} small_as_u8={s as u8} small_as_i32={s as i32} color_as_i32={c as i32}")
    
    LLVM (with run) C backend (--emit-c, cc, run) expected
    sizeof Small / Big / Pair / Color 1 / 8 / 2 / 4 4 / 4 / 8 / 4 1 / 8 / 2 / 4
    big_as_i64 1 1 7000000000
    small_as_u8 200 200 200
    small_as_i32 -56 200 200
    color_as_i32 4 4 4

    Emitted C: struct Pair { int32_t s; int32_t t; };.

    Adding match b: Big.Huge => ... | Big.Low => ... makes the LLVM binary print low (consistent with the constant being 1) and makes the C build fail: error: member reference base type 'int32_t' (aka 'int') is not a structure or union at _59 = (_57).tag;.

    Root cause

    • C backend: src/CCodegen.w:1892-1895: if tk == TY_ENUM: if payload: struct name else: return "int32_t". The enum's repr type (disc_repr_types) is never consulted. Established by the sizes above.
    • Discriminant width (both backends): src/SemaCheck.w:13405-13423 enum_variant_discriminant_for_type returns i32 and reads disc_values (an i32-valued map); when the qualified variant is not in the map it returns the variant index. --dump-mir shows _1 = const 1ty136 for Big.Huge (index 1), so the 7-billion value never reached the map. That the literal is dropped rather than truncated to 0xA13B8600 is my reading of the fallback path, not something I traced into the declaration pass; treat the exact drop site as a hypothesis. The reflection path (type_reflection_variant_discriminant, :21651) is i64, so the two APIs disagree on width.
    • LLVM u8 widening: Small.B as i32 = -56 means the value was extended as signed; not traced further, reported here as observed.

    Spec

    §13.3 (spec lines 2155-2199): the representation type "determines the underlying storage", and the LLVM layout is "{ repr_ty, ... } with the tag being the discriminant value". The C backend does not honour storage width; Sema does not honour discriminant values outside i32. Both are non-conforming. The @[specified] attribute is documented for "wire formats, file formats, FFI constants" (u16 example), which is exactly where a 4-byte C tag breaks a struct's ABI.

    Related PRs

    None since 450733e touch the c_type enum branch or enum_variant_discriminant_for_type. #983 (this queue) is the other C-backend divergence class.

    Would I champion a fix? Yes, three parts.

    1. C backend width: c_type maps a fieldless repr enum to the C integer of its repr type (uint8_t, int64_t, ...), and the payload-enum tag field uses the same mapping. Acceptance: the size row above matches LLVM on both backends; an emit-c differential test over u8, u16, i32, i64 reprs.
    2. Discriminant width: disc_values and enum_variant_discriminant_for_type carry i64 (or the repr type's width), and a discriminant outside the repr range is a compile error. Acceptance: big_as_i64 prints 7000000000 and match selects Huge; a value like 300 on a u8 enum is rejected at declaration.
    3. LLVM widening: casting a u8/u16 repr enum to a wider integer zero-extends. Acceptance: small_as_i32 prints 200 on both backends.
    4. C match on fieldless repr enums: lower without .tag (or fail loudly with a named diagnostic instead of emitting invalid C).

    Untested

    Linux and Windows not executed. The payload-enum tag path was read (:1893) but not exercised through the C backend.

  2. ehartford commented on Oct 25, 2026

    @ehartford
    Contributor

    Triage 2026-10-25, with v0.15.3.0 (main a261b65): reproduced. LLVM honors the representation type and emit-C does not.

    $ cat main.w
    enum Small: u8:
        A
        B
    enum Big: i64:
        X = 7000000000
        Y = 1
    fn main:
        print(sizeof[Small]())
        print(sizeof[Big]())
        let b = Big.X
        print(b as i64)
    $ with run main.w
    1
    8
    7000000000
    $ with build main.w --emit-c -o main.c   # main():
        _1 = (int64_t)sizeof(int32_t );
        _5 = (int64_t)sizeof(int32_t );
        _9 = 7000000000;
        _11 = (_10).tag;
    

    post-release: part of the emit-C lane campaign (#955).

  3. added this to the post-v0.16.0 milestone on Oct 28, 2026
  4. added a commit that references this issue on Nov 11, 2026
  5. ehartford commented on Nov 11, 2026

    @ehartford
    Contributor

    Merged to main ae20d89 (#3250): emit-C gives a fieldless enum its declared width (Sema.enum_repr_type). Fixture test/emit_c/enum_repr_width.w.

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions