Skip to content

MIR/codegen: a discriminant enum's discriminant is its repr type; a local's slot is its declared type (#1444) - #1460

Merged
ehartford merged 1 commit into
fix-1406-join-view-originfrom
fix-1444-disc-repr-match
Sep 23, 2026
Merged

ehartford merged 1 commit into
fix-1406-join-view-originfrom
fix-1444-disc-repr-match

Conversation

@ehartford

@ehartford ehartford commented Sep 23, 2026 •

Copy link
Copy Markdown
Contributor

Closes #1444. Stack #1463, above #1459.

Bug. match on an enum Kind: u8 returned the first arm (10 10 instead of 20 10). Narrow reprs crashed or printed nothing across the matrix.

Evidence. WITH_DUMP_MIR_CLEANUP_FN=kname: the discriminant temp is alloca i8, it receives store i32, and after SROA the switch is switch i8 undef. lldb on with-stage2 ir (breakpoint on mir_get_or_create_local_ptr, requested type i8 and declared type i32 in x24) stops for local 3, called from CodegenDispatch.w:5413.

Root cause. Two lines. MirLower lower_enum_discriminant typed every discriminant temp i32, whatever the repr. CodegenDispatch.w:5412 sized a memory local's first slot from the uncoerced value, while the store writes the value coerced to the declared type.

Fix.

  • The discriminant temp is the enum's repr type.
  • A memory local's slot is created after the coercion, typed as the value the store writes: the declared type when the coercion applies, the value's own type when it does not. Two earlier cuts typed the slot by the declared type. The first crashed LLVM on Unit-valued match bindings (behav_c_import_nullable_ptr_none caught it). The second put a Task handle { i32, ptr } into alloca i64 / alloca %Payload, because MIR types the handle's local as the awaited T (MIR types a Task handle local as the awaited T; lower_single_await reads .f0 of an i32/i64-typed local (validator silent) #1464): behav_fn_abi_async failed the LLVM verifier and async_tuple_await segfaulted. The combined battery on batch-z caught those, and a layer bisect on batch-z pinned them to this commit.
  • Tool gap: the typed MIR validator now rejects a discriminant assigned to anything but its repr. With the old lowering it reports discriminant of a ty=235 enum is its repr ty=6, assigned to ty=3. Pinned by mir_discriminant_repr_dest_test.

Matrix. Covers every repr (u8 i8 u16 i16 u32 i32 u64 i64), with values at and past the narrow reprs' sign bit, and these forms: shorthand, qualified, _, &Kind, statement, field, ==. It also covers guards, tuple subjects, Option payloads, if let, and a u8 payload enum. All are correct for values inside i32. A stage2 from the fixed stage1 passes 109 enum/match/codegen tests.

Fixtures. behav_disc_enum_repr_match, behav_disc_enum_u8_match_forms, da_disc_enum_u8_payload_match (leak count=0), mir_discriminant_repr_dest_test.

Filed. #1451 (negative and above-i32 discriminants; switch tables are i32), #1452 (parser panics), #1453 (from_int type), #1454 (unsigned as casts), #1455 (payload disc enum codegen).

Draft until the batch battery is green.

…r type; a local's slot holds the value stored to it (#1444)

`enum Kind: u8` with `match k: .Red => 10, .Blue => 20` returned 10 for
Blue: the match read garbage. Route: codegen of a compilable input —
IR at the join, then lldb on stage2.

Evidence. WITH_DUMP_MIR_CLEANUP_FN=kname: before SROA the discriminant
temp is `alloca i8` and receives `store i32 %7` (the loaded u8, sext to
i32), then `load i8`; after SROA the switch is `switch i8 undef`. lldb on
`with-stage2 ir` with a breakpoint on Codegen.mir_get_or_create_local_ptr
(condition: the requested type is i8 and differs from the declared type
in x24) stops for local 3 with x2 = i8, x24 = i32, called from
CodegenDispatch.w:5413.

Root cause, two lines. MirLower.w lower_enum_discriminant typed every
discriminant temp `i32`, whatever the enum's repr (§4.4a: the repr type
determines the storage). And CodegenDispatch.w:5412 sized a memory local's
first slot from the uncoerced value (`alloca_ty = value_ty`) while the
store below writes the value coerced to the declared type, so an i32
went through a 1-byte slot.

Fix. The discriminant temp is the enum's repr (i32 for every other enum),
and a memory local's slot is created after the coercion, typed as the
value the store writes. Two earlier cuts typed the slot by the declared
type: `void` crashed LLVM on a Unit-valued match binding
(behav_c_import_nullable_ptr_none), and a Task handle `{ i32, ptr }`
lives in a local MIR types as the awaited T (#1464), so `alloca i64` /
`alloca %Payload` took the handle and the fiber id was read as an i64
(behav_fn_abi_async: LLVM verifier; async_tuple_await: SIGSEGV; both
caught by the combined battery). The slot now holds exactly the stored
bytes: the declared type when the coercion applies, the value's own type
when it does not (which is the old behavior for those). The typed MIR
validator accepted the i32 temp; it now rejects a discriminant assigned to anything but the repr —
with only the validator and codegen changes built, the old lowering fails
loudly: "invalid MIR before codegen: discriminant of a ty=235 enum is its
repr ty=6, assigned to ty=3". mir_discriminant_repr_dest_test pins the
rule on hand-built MIR.

Matrix: reprs u8 i8 u16 i16 u32 i32 u64 i64, each with values 1, 2, one
at or past the narrow repr's sign bit (u8 200/255, u16 40000/65535, i8
100/127, i16 30000/32767) and one near i32::MAX; forms: shorthand arms,
qualified arms, `_` arm, `&Kind` subject, statement match, field subject,
`==`. All correct for values inside the i32 range (base: u8/i8/u16/i16
crash or print nothing, u32/u64/i64 wrong past the first arms). Also
correct now, trapping on base: a guard arm, a tuple subject, `Option`
payload patterns, `if let`, and a u8 payload enum. A stage2 built by the
fixed stage1 self-checks src/main.w and passes the 109 enum/match/codegen
tests in test/behavior, test/spec, test/codegen and test/debug_alloc.

Still wrong and filed: discriminants below 0 or above i32::MAX become the
variant index or truncate, and MIR switch tables are i32 (#1451); the
parser panics at a repr-maximum last variant and on u64 values above
i64::MAX (#1452); `from_int` returns Option[repr] (#1453); an unsigned
repr sign-extends in `as` casts (#1454); a payload disc enum with
non-index discriminants fails codegen (#1455).
@ehartford
ehartford force-pushed the fix-1444-disc-repr-match branch from e5a0a66 to 3bc7ff7 Compare September 23, 2026 09:32
@ehartford
ehartford force-pushed the fix-1444-disc-repr-match branch from 3bc7ff7 to bfbf4d4 Compare September 23, 2026 09:57
@ehartford
ehartford removed this pull request from stack #1463 September 23, 2026 09:58
@ehartford
ehartford added this pull request to stack #1466 September 23, 2026 09:58
@ehartford

Copy link
Copy Markdown
Contributor Author

Stack #1466 battery (seed-driven, new seed, cold c_import cache, with :drop-audit/:move-audit) on main 1c76062 + #1418 → #1419 → #1425 → #1428 → #1449 → #1377 → #1459 → #1460 → #1461 → #1462 (head cf36211, including the #1449/#1462 Codegen resolution and the fixed #1460): build ok, :fixpoint ok, :drop-audit ok, :move-audit ok, :test ok (no reds), :test-green ok, :last-green ok. Each branch holds exactly its tested commits on the layer below. Merge the whole stack: gh stack merge 1462 --squash.

@ehartford
ehartford marked this pull request as ready for review September 23, 2026 09:58
@ehartford
ehartford merged commit b905a7c into main Sep 23, 2026
@ehartford
ehartford deleted the fix-1444-disc-repr-match branch September 24, 2026 16:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Silent wrong arm: match on a u8-repr discriminant enum picks the first arm for in-range values

1 participant