Repository navigation
feat: write records and variants as TypeScript (stage 11g) - #105
Merged
Merged
Conversation
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
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.
accord buildrefused every program with a record, a variant with fields or amatch, 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), thenexpr.rs(fields,match, equality) andboundary.rs.How they cross
Decided before the code, after comparing the alternatives with outside reviewers; recorded in row 10:
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", unchangedtypeof x === "string"before switching on$kind.$kind, notkind. No Accord name holds a$, so it never collides with a field, and neither the language noraccord buildhas to forbid one.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.What the generated module does
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.i64is abigint, so a type that holds one has an unexported declaration for the body,Type$. A structured result is copied back byouter<Type>$, eachbigintchecked to be a safe integer.equal$), and so doesinover a list of them.matchon a name or a field narrows in place; amatchon 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 undertsc5.9--strict --noUncheckedIndexedAccess --exactOptionalPropertyTypes, and in Node it gives the examples' results, rejects an unknown key and a missing field.conformance/typescript/match-value: amatchon a call's result. Generated before the fix, the module failedtsc("Property 'limit' does not exist on type 'Bound$'"); the expected output, written before the fix, typechecks, and the fix matches it.cargo fmt --all --checkpassed;cargo clippy --workspace --all-targets --locked -- -D warningspassed;cargo test --workspace --locked: 126 passed, 0 failed, 2 ignored (as on main);score.py --self-testandmaterials.py: passed.full-ci).Not established
matchbug slipped past the differential test; both cases here were typechecked by hand only. Atscstep needs the TypeScript package in CI, a new dependency, so it isn't in this change.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