Skip to content

Sum types end-to-end: Result, Option, user-declared enums #48

@proofmancer

Description

@proofmancer

The v0.3 type checker treats generic types like Result<u64> as opaque heads. Codegen rejects them. This issue lands sum types end-to-end.

Scope

Three layers move in lockstep:

Grammar

  • enum Name { Variant1, Variant2(T), Variant3 { f: T, g: T } } declaration syntax
  • Built-in Result<T, E> and Option<T> shipped as stdlib enums once self-hosting allows
  • Constructor expressions: Ok(x), None, Some(x)
  • Pattern matching with constructors: match x { Ok(v) => ..., Err(e) => ... }

Type system

  • Resolve enum declarations as new nominal types
  • Constructor expressions have type of their enum head
  • Pattern arms scope their bindings into the arm body
  • Exhaustiveness check (basic; refined later)

Codegen

  • Layout decision: tag-then-payload, 4-byte tag + payload aligned to widest variant
  • Ok(x) emits tag-write + payload-write to a freshly-allocated heap slot
  • match destructures via tag-test + payload-load

Hard prerequisite

Memory model RFC (#42) must land first. Sum types need an allocator, and the allocator design is gated on the memory model.

Acceptance

  • Grammar additions parsed by tree-sitter and the C parser; both stay in lockstep
  • Type checker validates constructor calls, pattern arms, and exhaustiveness on small examples
  • Codegen emits valid WASM that round-trips Result<u64> through a hostcall
  • examples/counter.cv (the aspirational version with Result<u64>) now actually compiles and runs

Metadata

Metadata

Assignees

No one assigned

    Labels

    compilerCompiler / parser / codegen workdesignRFC-style design discussions

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions