Semantic Diff

#8905 Represent array and list literals with spreads with a Pexp_spread node

2 commits

364e15225cb907c901b0d9c867a12bed2c21a342..pr-8905

b93e55173 · cknitt + Claude · Oct 18, 2026, 18:22 UTC

Represent array and list literals with spreads with a Pexp_spread node

Why

The parser turned [a, ...b] and list{...a, b} into calls of Primitive_array.spread and Primitive_list.spread marked with res.spread, so the formatter had to rebuild the literal from the call and got it wrong: spreads of literals were flattened, attributes on the literal were dropped and an empty list spread printed as an empty element.

Pexp_spread keeps the items and spreads in source order, and the type checker gives it a Texp_spread node. Translation emits new Parray_spread and Plist_spread primitives that compile to the same runtime calls. A list literal whose only spread is its tail stays a chain of ::. The parsetree and typed tree layouts change; the release bumps their magic numbers.

modifiedAST array literal with a spread

let x = [1, ...ys]
parse
Before
After
expressionexpression├─ Pexp_apply└─ expression   ├─ attribute "res.spread"   ├─ Pexp_ident "Primitive_array.spread"   ├─ <arg>   └─ Nolabel└─ Pexp_spread array   ├─ Pspread_item       └─ expression   │  └─ expression         └─ Pexp_array            ├─ expression            │  └─ Pexp_array            │     └─ expression            │        └─ Pexp_constant PConst_int (1,None)            └─ expression               └─ Pexp_ident "ys"   │     └─ Pexp_constant PConst_int (1,None)   └─ Pspread      └─ expression         └─ Pexp_ident "ys"

addedTypedtree Texp_spread

let ys = [2]let x = [1, ...ys]
typed
After
expression└─ Texp_spread array   ├─ expression   │  └─ Texp_array   │     └─ expression   │        └─ Texp_constant Const_int 1   └─ expression      └─ Texp_ident "ys/1001"

modifiedFormat literals with spreads

let x = [...[1, 2], 3, ...[]]let y = @attr list{...xs, ...list{}, 1}
rescript
Before
After
let x = [1, 2, 3, ]let y = list{...xs, , 1}let x = [...[1, 2], 3, ...[]]let y = @attr list{...xs, ...list{}, 1}

Review

  • Why do the generated item groups have ghost locations? The v0 encoding has no marker on them, and the ghost flag tells a group of items from an array or list literal that is spread. A call built by a PPX with real locations decodes to spreads only, which concatenates the same values.
  • Why does the typed node hold the concatenated parts rather than the elements? The parts are the arrays and cons chains the runtime call takes, so the items keep the errors of array and list literals and translation needs no list building of its own.
a97a105b1 · cknitt + Claude · Oct 27, 2026, 07:00 UTC

Move the v0 spread encoding into the PPX bridge and test spread literals

The res.spread call is only the v0 PPX wire format, so it now lives in the bridge: Ast_mapper_to0 builds it (spread_call, spread_marker) and Ast_mapper_from0 decodes it, with the module-name lookup local to the decoder. Ast_spread keeps what the type checker and analysis use: groups, parts and module_name. The ghost locations of the generated parts stay, as for any node the compiler makes up. Behaviour is unchanged.

The new mocha test runs array literals with several spreads, items between them, a leading item and an empty spread, list literals with spreads before the tail and a tail-only spread, and checks that both kinds evaluate their items and spreads left to right; its generated JavaScript pins the Primitive_array.spread and Primitive_list.spread calls and the order of their parts.