Repository navigation
feat: payload enums — construction and typeck - #16
Merged
Merged
Conversation
- AST: Added ExpressionKind::EnumVariant for constructing payload values
- Typeck: Removed unit-only rejection, EnumDef now stores VariantInfo
with payload types. Match typeck validates enum patterns and destructuring.
- Parser: Match-arm enum patterns (EnumType.Variant(payload)) parse correctly.
Removed overly-broad parser heuristic — enum construction is detected
in typeck not parser.
- Codegen: Payload enums get { i16 tag, [data] } LLVM struct layout.
EnumVariant construction emits proper struct with tag+payload.
match codegen is WIP (crashes on pattern matching).
- Module exports: ModuleEnumVariant carries payload_types for serialization.
- Tests: enum_payload_test.ag and enum_mixed_test.ag verify construction.
All 37 existing tests pass.
- Add enum_variant_payload_types tracking (populated in generate_enum_item)
- Fix match handler: zero-init scrutinee alloca, use actual payload types
instead of hardcoded i32 for data extraction
- Add zero-init (const_zero) to emit_enum_construction_impl and
EnumVariant expression handler
- Fix FieldAccess to wrap unit variant discriminants in struct for
enums with payload layouts
- Fix pre-existing PHI node corruption: catch-all wildcard arms created
unreachable blocks that corrupted the end_bb PHI; use catch_all flag
with unreachable terminator
- Cast arm body values to common type at PHI construction to handle
i32-payload vs i64-literal mismatch
- Cast payload values to declared variant type before storing in
enum construction
- Upgrade enum_mixed_test and enum_payload_test with real match
verification (were no-op asserts)
- Add enum_match_{unit,payload,mixed}_test.ag for match coverage
…outs, multi-field, arity validation, imported payloads) - Fix A: correct PrimitiveType size table (I64/F32, F64/I128 were swapped) - Fix B: register imported enum payload layouts in declare_imported_modules - Fix C: support multi-field payload construction with byte-offset GEP loop - Fix D: validate enum-variant construction arity + types in typeck - Fix E: preserve imported payload types from ModuleEnumVariant - Tests: enum_size_test, enum_multi_field_test, enum_arity_error_test - Harness: support expected-compile-failure tests in run_tests.sh Closes Greptile findings #1-5 from PR #16 commit ff84c7e.
| max_payload_size = max_payload_size.max(variant_size); | ||
| } | ||
|
|
||
| if has_payload { |
Contributor
There was a problem hiding this comment.
Payload discriminants alias in i16
When a payload enum has distinct discriminants equal modulo 65536, such as 0 and 65536, this fixed i16 tag truncates both to the same runtime value. Construction and matching then treat the variants as identical, causing a value to select the wrong match arm and interpret its payload as another variant.
Comment on lines
+4376
to
+4384
| let val = self.emit_expression_value(&fields[0])?; | ||
| let val_ptr = self.builder.build_pointer_cast( | ||
| data_ptr, | ||
| self.context.ptr_type(AddressSpace::default()), | ||
| "enum.val.cast", | ||
| ).map_err(|e| CodegenError::new(format!("pointer cast enum: {e}")))?; | ||
| self.builder.build_store(val_ptr, val).map_err(|e| { | ||
| CodegenError::new(format!("store enum payload: {e}")) | ||
| })?; |
Contributor
There was a problem hiding this comment.
When the first payload field is a function call or another side-effecting expression, the preceding field loop evaluates and stores it once and this block evaluates it again before overwriting the same storage. The side effect therefore runs twice, and the enum retains the second result rather than the first.
CierCier
added a commit
that referenced
this pull request
Aug 1, 2026
…outs, multi-field, arity validation, imported payloads) - Fix A: correct PrimitiveType size table (I64/F32, F64/I128 were swapped) - Fix B: register imported enum payload layouts in declare_imported_modules - Fix C: support multi-field payload construction with byte-offset GEP loop - Fix D: validate enum-variant construction arity + types in typeck - Fix E: preserve imported payload types from ModuleEnumVariant - Tests: enum_size_test, enum_multi_field_test, enum_arity_error_test - Harness: support expected-compile-failure tests in run_tests.sh Closes Greptile findings #1-5 from PR #16 commit a6b488b.
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.
Implements payload enums — enums carrying typed data per variant.
Changes
ExpressionKind::EnumVariantfor constructing payload-carrying enum valuesbuild_enum_def.EnumDefnow storesVariantInfowith discriminant and payload types. Match typeck validates enum patterns with destructuring bindingsEnumType.Variant(payload)) now parse correctly{ i16 tag, [data] }LLVM struct layout.EnumVariantconstruction emits proper tagged structModuleEnumVariantcarriespayload_typesfor module serialization/deserializationtests/enum_payload_test.agandtests/enum_mixed_test.agverify construction worksAll existing tests pass (37/37).
Known Limitation
emit_match_expression_valuebut crashes during LLVM IR generation. Match type checking is implemented correctly.Greptile Summary
Adds payload-carrying enum support across the compiler pipeline.
Confidence Score: 3/5
The PR is not yet safe to merge because payload variants can alias through the fixed-width tag and the repaired constructor can evaluate its first payload twice.
Payload construction and matching truncate unrestricted discriminants to i16, while the direct EnumVariant path repeats evaluation of the first constructor argument after already storing it.
agc/src/codegen/llvm_ir.rs
Important Files Changed
Reviews (3): Last reviewed commit: "fix: address Greptile payload-enum findi..." | Re-trigger Greptile