Skip to content

feat: write records and variants as TypeScript (stage 11g) - #105

Merged
Sequela02 merged 4 commits into
mainfrom
feat/stage-11-typescript-records
Nov 28, 2026
Merged

Sequela02 merged 4 commits into
mainfrom
feat/stage-11-typescript-records

Conversation

@Sequela02

Copy link
Copy Markdown
Contributor

accord build refused every program with a record, a variant with fields or a match, and the semver port (11h) needs all three. Slice 11g of stage 11 (#83) writes them as TypeScript, with boundary checks, and settles how they cross the boundary (row 10 of the native code constraints).

Where to start reading: crates/accord/src/typescript/structured.rs (declarations, checks and copies), then expr.rs (fields, match, equality) and boundary.rs.

How they cross

Decided before the code, after comparing the alternatives with outside reviewers; recorded in row 10:

Accord TypeScript
record Window { low: i64, high: Bound } { readonly low: number; readonly high: Bound }
enum Bound { Open, Below(limit: i64) } "open" | { readonly $kind: "below"; readonly limit: number }
enum Light { Red, Amber } "red" | "amber", unchanged
  • A variant without fields stays its string, in an enum with fields too. So giving an enum its first variant with fields leaves its other values as they were, and the existing ports, whose own types are string unions, are unaffected. The cost: callers test typeof x === "string" before switching on $kind.
  • The tag is $kind, not kind. No Accord name holds a $, so it never collides with a field, and neither the language nor accord build has to forbid one.
  • An unknown key is a TypeError, found at the first one. TypeScript's types let an object with extra properties through, so only the runtime check enforces the declared shape.
  • Values aren't frozen, as lists aren't. No constructors or type guards are exported until a caller needs them.

What the generated module does

  • Each structured parameter is checked and copied by check<Type>$: each field read once, so a getter can't pass the check and give the body another value. Messages name where: contains: window.high.limit must be an i64.
  • Inside the module an i64 is a bigint, so a type that holds one has an unexported declaration for the body, Type$. A structured result is copied back by outer<Type>$, each bigint checked to be a safe integer.
  • Equality on records and variants compares field by field (equal$), and so does in over a list of them.
  • A match on a name or a field narrows in place; a match on any other value binds it once in an arrow function, which TypeScript narrows and which runs the value once.

Verification

  • conformance/typescript/structured: the expected module was written by hand before the generator and is matched byte for byte. It typechecks under tsc 5.9 --strict --noUncheckedIndexedAccess --exactOptionalPropertyTypes, and in Node it gives the examples' results, rejects an unknown key and a missing field.
  • conformance/typescript/match-value: a match on a call's result. Generated before the fix, the module failed tsc ("Property 'limit' does not exist on type 'Bound$'"); the expected output, written before the fix, typechecks, and the fix matches it.
  • The differential test now builds structured inputs (records and variants over a few values of each field) and reads structured results; generated modules agree with the interpreter on every input.
  • On Windows with the pinned solvers: cargo fmt --all --check passed; cargo clippy --workspace --all-targets --locked -- -D warnings passed; cargo test --workspace --locked: 126 passed, 0 failed, 2 ignored (as on main); score.py --self-test and materials.py: passed.
  • Linux and macOS: pending (CI, full-ci).

Not established

  • Generated modules aren't typechecked in CI. Node runs them with types stripped, which is how the match bug slipped past the differential test; both cases here were typechecked by hand only. A tsc step needs the TypeScript package in CI, a new dependency, so it isn't in this change.
  • A record or variant from another file is imported for the signatures and declared again, unexported, for the body; no case covers it yet.
  • The ports' scripts (scripts/check-*.sh) weren't run; none of them uses records or variants with fields.

Risk note

One-way door for the format once published: how records and variants cross is a public interface, recorded in row 10. Nothing is published yet, so it is still cheap to change. Blast radius: programs without records or variants with fields get the same modules as before, as the existing TypeScript cases show, byte for byte. Evidence: the checks above.

Refs #83

Atrium: 01a1e493-7617-708c-8d15-bf81b0301b67

Stage 11g (#83) writes records and variants with fields as TypeScript.
The expected module is written before the generator: a record as a plain
object, a variant with fields as an object tagged by $kind, a variant
without fields as its string, each checked and converted at the boundary,
an unknown key rejected, and an i64 field a bigint inside and a safe
integer outside. The module typechecks under tsc 5.9 --strict.

Row 10 of the native code constraints records how they cross, decided
after a review of the alternatives with outside reviewers.

Refs #83
TypeScript narrows a name or a field, not a call's result, so a match on
one must bind the value once. Found by typechecking a generated module
with tsc 5.9 --strict: Node strips types, so the differential test runs
the module without checking them.

Refs #83
accord build refused every program with a record, a variant with fields
or a match, which the semver port (11h) needs. A record is now a plain
object of its fields; a variant with fields an object tagged by $kind,
which no Accord field can be named; a variant without fields stays its
string, so an enum gaining its first variant with fields keeps its other
values. Each is checked and copied at the boundary, each field read once,
an unknown key rejected at the first; an i64 field is a bigint inside and
a safe integer outside. Equality compares field by field, and a match on
a value TypeScript can't narrow binds it once.

Refs #83
@Sequela02 Sequela02 added the full-ci Run Windows and macOS CI once before merging label Nov 28, 2026
@Sequela02
Sequela02 merged commit 8e4ec4e into main Nov 28, 2026
5 checks passed
@Sequela02
Sequela02 deleted the feat/stage-11-typescript-records branch November 28, 2026 09:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

full-ci Run Windows and macOS CI once before merging

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant