Semantic Diff

#9224 Represent records with the runtime names "0", "1", ... as Record_array

3 commits

0f92894adb6ac53fd1e52b3429dc33853ae5373f..pr-9224

6d7560ed8 · cknitt + Claude · Nov 04, 2026, 19:21 UTC

Represent records with the runtime names "0", "1", ... as Record_array

Why

Whether a record is a JavaScript array was rediscovered from its field names only when js_dump printed a record literal. Every other path treated it as an object: a spread over a record with an optional field or 20 fields became an object spread, and {} printed {} (#9196).

The type declaration now decides once: a record whose fields have the runtime names "0", "1", ... in declaration order gets the representation Record_array, and its blocks carry array to js_dump. A spread over such a record copies its fields one by one. Record rest patterns and coercions whose target is an array record are rejected, as an object spread or an object can't produce an array.

modifiedJS record spread

type t = {@as("0") a?: int, @as("1") b: int}let upd = (r: t) => {...r, b: 2}type e = {@as("0") x?: int}let empty: e = {}
js
Before
After
function upd(r) {function upd(r) { return { ...r, "1": 2 }; return [ r[0], 2 ];}}let empty = {};let empty = [];

addedDiagnostic record rest into an array record

type source = {@as("0") s0: int, @as("1") s1: int, @as("2") s2: int}type head = {@as("0") s0: int, @as("1") s1: int}let split = (r: source) => {  let {s2, ...head as rest} = r  (s2, rest)}
js
Before
let {"2": __rest_field0, ...rest} = r;
diagnostic
After
The rest of a record pattern cannot have a record type represented as an array (fields with the runtime names "0", "1", ...).

addedDiagnostic coercion into an array record

type obj = {@as("0") a: int, @as("1") b: int, x: int}type arr = {@as("0") a: int, @as("1") b: int}let toArr = (x: obj) => (x :> arr)
js
Before
function toArr(x) {  return x;}
diagnostic
After
Type obj is not a subtype of arr The record arr is represented as a JavaScript array (its fields have the runtime names "0", "1", ...), but obj is represented as an object, so it cannot be coerced to it.

Review

  • Why not keep checking the names in js_dump? Only the literal printer looked at them. A representation is matched exhaustively by every phase that builds or reads a record, so each of them has to say what an array record means, and no path can miss it again.
  • Is coercing an array record to an object record still allowed? Yes: an array has its elements as the properties "0", "1", ..., so {"0", "1", "2"} :> {"1"} stays valid. Only the other direction is rejected.
ee87f9dae · cknitt + Claude · Nov 04, 2026, 19:54 UTC

Classify a Record_array payload of an untagged variant as an array

Codex review: an untagged variant payload that is an array record was classified as an object, so R({}), now [], matched an array case. The same already happened on master for R({x: 1}), which is [1].

modifiedDiagnostic untagged variant with an array record and an array

type pair = {@as("0") x?: int, @as("1") y?: int}@unboxed type v = Pair(pair) | List(array<int>)let classify = (v: v) =>  switch v {  | Pair(_) => "pair"  | List(_) => "list"  }
js
Before
function classify(v) {  if (Array.isArray(v)) {    return "list";  } else {    return "pair";  }}
diagnostic
After
This untagged variant definition is invalid: At most one case can be an array, a tuple or a record represented as an array.
b41952a37 · cknitt + Claude · Nov 04, 2026, 20:31 UTC

Reject coercing an array record to a record represented as an object

Review (cristianoc): an array record coerced to an object record stays a JavaScript array, so in @unboxed type v = Pair(pair) | Tail(tail) the value Tail((r :> tail)) passed the array test and matched Pair. A record's representation is now exact: records coerce only to records of the same representation, and the error names the mismatch either way. The untagged dispatch test now runs through Array.map, so the match is not folded at compile time.

modifiedDiagnostic coercion between an array record and an object record

type pair = {@as("0") a: int, @as("1") b: int}type tail = {@as("1") b: int}let toTail = (r: pair) => (r :> tail)
js
Before
function toTail(r) {  return r;}
diagnostic
After
Type pair is not a subtype of tail The record pair is represented as a JavaScript array (its fields have the runtime names "0", "1", ...) and tail as an object, so neither can be coerced to the other.