Skip to content

MIR/codegen: an enum aggregate names its variant by index; the tag it stores is the variant's discriminant (#1455) - #1537

Merged
ehartford merged 1 commit into
mainfrom
fix-1455-disc-payload-tag
Sep 23, 2026
Merged

ehartford merged 1 commit into
mainfrom
fix-1455-disc-payload-tag

Conversation

@ehartford

Copy link
Copy Markdown
Contributor

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.


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).

@ehartford
ehartford added this pull request to stack #1547 September 23, 2026 17:06
… 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
ehartford force-pushed the fix-1455-disc-payload-tag branch from 5844ce2 to 6b7f69f Compare September 23, 2026 20:43
@ehartford

Copy link
Copy Markdown
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: gh stack merge 1546 --squash.

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.

1 participant