Skip to content

feat: payload enums — construction and typeck - #16

Merged
CierCier merged 4 commits into
mainfrom
feat/payload-enums
Jul 23, 2026
Merged

CierCier merged 4 commits into
mainfrom
feat/payload-enums

Conversation

@CierCier

@CierCier CierCier commented Jul 23, 2026 •

Copy link
Copy Markdown
Owner

Implements payload enums — enums carrying typed data per variant.

Changes

  • AST: Added ExpressionKind::EnumVariant for constructing payload-carrying enum values
  • Typeck: Removed unit-only rejection in build_enum_def. EnumDef now stores VariantInfo with discriminant and payload types. Match typeck validates enum patterns with destructuring bindings
  • Parser: Match-arm enum patterns (EnumType.Variant(payload)) now parse correctly
  • Codegen: Payload enums get { i16 tag, [data] } LLVM struct layout. EnumVariant construction emits proper tagged struct
  • Module exports: ModuleEnumVariant carries payload_types for module serialization/deserialization
  • Tests: tests/enum_payload_test.ag and tests/enum_mixed_test.ag verify construction works

All existing tests pass (37/37).

Known Limitation

  • Match expression codegen on payload enums has a SIGSEGV bug. Enum pattern matching in codegen works through emit_match_expression_value but crashes during LLVM IR generation. Match type checking is implemented correctly.

Greptile Summary

Adds payload-carrying enum support across the compiler pipeline.

  • Extends AST, parser, semantic analysis, type checking, and monomorphization for payload construction and destructuring.
  • Adds tagged LLVM layouts, payload construction, and match lowering.
  • Serializes payload signatures through module artifacts and registers imported layouts.
  • Adds payload construction, matching, arity, mixed-variant, multi-field, and size tests.

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

Filename Overview
agc/src/codegen/llvm_ir.rs Adds payload layouts, construction, imported registration, and match lowering, but wider discriminants alias in the i16 tag and one construction path evaluates its first payload twice.
agc/src/semantic/typeck.rs Adds payload signatures and validates payload construction and match bindings.
agc/src/module_artifact.rs Extends enum-variant serialization and export collection with canonical payload type signatures.
agc/src/parser/prt_parser.rs Parses enum variant patterns with optional payload destructuring.

Reviews (3): Last reviewed commit: "fix: address Greptile payload-enum findi..." | Re-trigger Greptile

Greptile also left 2 inline comments on this PR.

CierCier added 2 commits July 23, 2026 13:07
- 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.
Copilot AI review requested due to automatic review settings July 23, 2026 07:59

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

Comment thread agc/src/codegen/llvm_ir.rs Resolved
Comment thread agc/src/codegen/llvm_ir.rs Resolved
Comment thread agc/src/semantic/typeck.rs Resolved
Comment thread agc/src/semantic/typeck.rs Resolved
Comment thread agc/src/codegen/llvm_ir.rs Resolved
- 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
Comment thread agc/src/semantic/typeck.rs Resolved
Comment thread agc/src/codegen/llvm_ir.rs Resolved
…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 {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 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}"))
})?;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 First payload evaluates twice

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
CierCier merged commit a6e5057 into main Jul 23, 2026
1 of 2 checks passed
@CierCier
CierCier deleted the feat/payload-enums branch July 23, 2026 12:43
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.
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.

2 participants