Repository navigation
MIR/codegen: a discriminant enum's discriminant is its repr type; a local's slot is its declared type (#1444) - #1460
Merged
Conversation
…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
force-pushed
the
fix-1444-disc-repr-match
branch
from
September 23, 2026 09:32
e5a0a66 to
3bc7ff7
Compare
ehartford
force-pushed
the
fix-1444-disc-repr-match
branch
from
September 23, 2026 09:57
3bc7ff7 to
bfbf4d4
Compare
This was referenced Sep 23, 2026
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: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #1444. Stack #1463, above #1459.
Bug.
matchon anenum Kind: u8returned the first arm (10 10instead of20 10). Narrow reprs crashed or printed nothing across the matrix.Evidence.
WITH_DUMP_MIR_CLEANUP_FN=kname: the discriminant temp isalloca i8, it receivesstore i32, and after SROA the switch isswitch i8 undef. lldb onwith-stage2 ir(breakpoint onmir_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_discriminanttyped every discriminant tempi32, 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.
behav_c_import_nullable_ptr_nonecaught it). The second put a Task handle{ i32, ptr }intoalloca 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_asyncfailed the LLVM verifier andasync_tuple_awaitsegfaulted. The combined battery on batch-z caught those, and a layer bisect on batch-z pinned them to this commit.discriminant of a ty=235 enum is its repr ty=6, assigned to ty=3. Pinned bymir_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_inttype), #1454 (unsignedascasts), #1455 (payload disc enum codegen).Draft until the batch battery is green.