Repository navigation
MIR/codegen: an enum aggregate names its variant by index; the tag it stores is the variant's discriminant (#1455) - #1537
Merged
Conversation
… stores is the variant's discriminant (#1455) `enum Msg: i32: Quit = 0, Move(i32, i32) = 7` failed code generation for `Msg.Move(1, 2)`: "aggregate enum payload missing destination payload type fn=main variant=7". Route: codegen of a compilable input — the MIR, then lldb on the codegen lookup. Evidence. --dump-mir of main: `_2 = aggregate(kind=1, tag=7, fields=[const 1, const 2])` for a two-variant enum, then the match's `switchInt(_7) -> [7: bb5]`. lldb on stage1, breakpoint on Codegen.mir_enum_variant_payload_llvm_type with condition x2 == 7: stops with x1 = 235 (Msg, the `_2: ty235` local) and x2 = 7, called from Codegen.mir_eval_rvalue — the aggregate arm (CodegenDispatch.w:3613), which reads d2 as the variant index. Its comment says so, and the C backend (CCodegen.w:3450/3453) reads d2 as the index and looks the tag up from it. Root cause. MirLower.w lower_static_enum_variant_call stored `enum_variant_discriminant_for_type` as the aggregate's d2, and so did the five other enum-aggregate builders (lower_var, assign_enum_variant_to_place, the `Type.Variant` field access, the call-form constructor, the variant shorthand). For every enum except a payload discriminant enum the discriminant is the index, so only non-index discriminants exposed it. Fix. The aggregate carries the variant index (enum_variant_index_for_type); codegen stores that variant's discriminant as the tag (mir_enum_variant_discriminant — the lookup Display and the drop glue already switch on). A payloadless discriminant-enum constant still lowers to its discriminant. The typed MIR validator passed a variant 7 of 2; it now rejects an enum aggregate whose variant is not an index of the enum (mir_enum_aggregate_variant_index_test pins the rule on hand-built MIR). The MIR dump names d2 `variant=`. Matrix (behav_disc_enum_payload_nonindex): reprs i32, u8, i64, u16; discriminants 0/7, 200/3 (declared out of value order), 40/41/90, 10 then auto-increment; forms: qualified call, shorthand under an expected type, payloadless variant beside payload variants, fn return, a literal payload pattern, is_/as_ accessors, iteration over an array, an Option of the enum. Base: code generation fails for every one. Now: the expected values. da_disc_enum_payload_nonindex: owned str payloads under non-index tags, leak count 0; --validate-all ok.
ehartford
force-pushed
the
fix-1455-disc-payload-tag
branch
from
September 23, 2026 20:43
5844ce2 to
6b7f69f
Compare
Contributor
Author
|
Stack #1547 battery (seed-driven, new seed, cold c_import cache, with :drop-audit/:move-audit) on main ce7508c + #1537 → … → #1546 (head bee5c56; every PR head is a tested commit; includes the #1451 same-named-enum fix and the render.w rebase over #1513): build ok, :fixpoint ok, :drop-audit ok, :move-audit ok, :test ok (no reds), :test-green ok, :last-green ok. Merges cleanly into main 02301f8. Merge the stack: |
ehartford
marked this pull request as ready for review
September 23, 2026 21:34
This was referenced Sep 23, 2026
Merged
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.
enum Msg: i32: Quit = 0, Move(i32, i32) = 7failed code generation forMsg.Move(1, 2): "aggregate enum payload missing destination payloadtype fn=main variant=7". Route: codegen of a compilable input — the MIR,
then lldb on the codegen lookup.
Evidence. --dump-mir of main:
_2 = aggregate(kind=1, tag=7, fields=[const 1, const 2])for a two-variant enum, then the match'sswitchInt(_7) -> [7: bb5]. lldb on stage1, breakpoint onCodegen.mir_enum_variant_payload_llvm_type with condition x2 == 7: stops
with x1 = 235 (Msg, the
_2: ty235local) and x2 = 7, called fromCodegen.mir_eval_rvalue — the aggregate arm (CodegenDispatch.w:3613),
which reads d2 as the variant index. Its comment says so, and the C
backend (CCodegen.w:3450/3453) reads d2 as the index and looks the tag up
from it.
Root cause. MirLower.w lower_static_enum_variant_call stored
enum_variant_discriminant_for_typeas the aggregate's d2, and so did thefive other enum-aggregate builders (lower_var, assign_enum_variant_to_place,
the
Type.Variantfield access, the call-form constructor, the variantshorthand). For every enum except a payload discriminant enum the
discriminant is the index, so only non-index discriminants exposed it.
Fix. The aggregate carries the variant index (enum_variant_index_for_type);
codegen stores that variant's discriminant as the tag
(mir_enum_variant_discriminant — the lookup Display and the drop glue
already switch on). A payloadless discriminant-enum constant still lowers
to its discriminant. The typed MIR validator passed a variant 7 of 2; it
now rejects an enum aggregate whose variant is not an index of the enum
(mir_enum_aggregate_variant_index_test pins the rule on hand-built MIR).
The MIR dump names d2
variant=.Matrix (behav_disc_enum_payload_nonindex): reprs i32, u8, i64, u16;
discriminants 0/7, 200/3 (declared out of value order), 40/41/90, 10 then
auto-increment; forms: qualified call, shorthand under an expected type,
payloadless variant beside payload variants, fn return, a literal payload
pattern, is_/as_ accessors, iteration over an array, an Option of the
enum. Base: code generation fails for every one. Now: the expected values.
da_disc_enum_payload_nonindex: owned str payloads under non-index tags,
leak count 0; --validate-all ok.
Layer of the enums/discriminants stack (#1455 → #1451 → #1452 → #1453 → #1454 → #1441 → #1442 → #1439 → #1456). Draft until the stack's seed-driven battery is green (D56).