You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
emit-c maps every fieldless enum to int32_t regardless of repr type #990
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.
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}")
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-13423enum_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.
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.
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.
LLVM widening: casting a u8/u16 repr enum to a wider integer zero-extends. Acceptance: small_as_i32 prints 200 on both backends.
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.
Activity
ehartford commentedon Sep 16, 2026
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_tregardless of its declared representation type, sou8enums inflate (a two-u8struct is 8 bytes instead of 2) andi64enums 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 ani32map and falls back to the variant index when the value is absent, so both backends print1forBig.Huge; (2) the LLVM backend sign-extends au8enum when widening (Small.B as i32gives-56where the C backend gives200), so the "wide discriminant truncation" divergence the report reasons about is masked by an earlier truncation, while a different LLVM-side divergence exists. Also, amatchon a fieldless repr enum does not compile through the C backend at all ((_57).tagon anint32_t), which is loud rather than silent and is a separate defect.Tested revision and compiler
main@ 5809ac1 (2026-09-14); thec_typeenum branch is unchanged since the reported 450733ewith buildof 5809ac1 in a detached worktree, self-reportswith v0.15.2.1ccagainst the worktree'sout/lib/{rt_core,rt_darwin_aarch64,compat_runtime,panic_runtime,fiber_stubs,cimport_stubs}.o(the setbuild/emit_c.w:582-587links)Repro (match-free so the C compiles)
with run)--emit-c,cc, run)sizeof Small / Big / Pair / Colorbig_as_i64small_as_u8small_as_i32color_as_i32Emitted C:
struct Pair { int32_t s; int32_t t; };.Adding
match b: Big.Huge => ... | Big.Low => ...makes the LLVM binary printlow(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 unionat_59 = (_57).tag;.Root cause
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.src/SemaCheck.w:13405-13423enum_variant_discriminant_for_typereturnsi32and readsdisc_values(ani32-valued map); when the qualified variant is not in the map it returns the variant index.--dump-mirshows_1 = const 1ty136forBig.Huge(index 1), so the 7-billion value never reached the map. That the literal is dropped rather than truncated to0xA13B8600is 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) isi64, so the two APIs disagree on width.u8widening: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 outsidei32. Both are non-conforming. The@[specified]attribute is documented for "wire formats, file formats, FFI constants" (u16example), which is exactly where a 4-byte C tag breaks a struct's ABI.Related PRs
None since 450733e touch the
c_typeenum branch orenum_variant_discriminant_for_type. #983 (this queue) is the other C-backend divergence class.Would I champion a fix? Yes, three parts.
c_typemaps 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 overu8,u16,i32,i64reprs.disc_valuesandenum_variant_discriminant_for_typecarryi64(or the repr type's width), and a discriminant outside the repr range is a compile error. Acceptance:big_as_i64prints 7000000000 andmatchselectsHuge; a value like300on au8enum is rejected at declaration.u8/u16repr enum to a wider integer zero-extends. Acceptance:small_as_i32prints 200 on both backends.matchon 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.ehartford commentedon Oct 25, 2026
Triage 2026-10-25,
with v0.15.3.0(main a261b65): reproduced. LLVM honors the representation type and emit-C does not.post-release: part of the emit-C lane campaign (#955).
Issues batch C: emit-C bit intrinsics (#1006) and enum widths (#990) (#…
ehartford commentedon Nov 11, 2026
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.