Skip to content

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

Draft
cknitt wants to merge 1 commit into
masterfrom
parsetree-spread
Draft

cknitt wants to merge 1 commit into
masterfrom
parsetree-spread

Conversation

@cknitt

@cknitt cknitt commented Oct 18, 2026

Copy link
Copy Markdown
Member

Part of #8624 (res.spread item).

The parser used to turn [a, ...b] and list{...a, b} into calls of Primitive_array.spread / Primitive_list.spread with a res.spread attribute on the function. They are now a Pexp_spread (kind, elements) node with items and spreads in source order, typed as Texp_spread, and translated to new Parray_spread / Plist_spread Lambda primitives that compile to the same runtime calls. A list literal whose only spread is its tail (list{a, ...b}) stays a chain of ::, as before.

Effects

  • JS output: unchanged. tests/tests rebuilds byte-identical, and every item/spread sequence up to length 4 (arrays and lists, top level and in functions) compiles to the same JS with the old and new bsc.
  • Formatter: fixes. The old printer read the call back and lost information:
    • [...[1, 2], 3, ...[]] printed as [1, 2, 3, ] (invalid), and list{...xs, ...list{}, 1} as list{...xs, , 1}
    • attributes on spread literals were dropped (@attr [1, ...a] → [1, ...a])
    • comments in list spreads moved to the end of the line
  • Errors: spreading the wrong collection type now reports the spread (This has type: list<int>, but it's expected to have type: array<int>) instead of "This array item has type list…". With a type annotation, the mismatch is reported at the element, as for a plain array literal.
  • PPX (v0 wire): unchanged; to0 emits the old call and from0 decodes it back. The generated item groups get ghost locations, so a group of items can be told apart from a spread array/list literal, and the round trip is exact. A spread call built by a PPX with real locations decodes to spreads only, which concatenates the same values. Calls without the marker stay calls.
  • Editor: pipe completion still types the literal as the runtime call (the same approach as tagged templates); semantic tokens no longer need the attribute check.
  • Magic numbers: ResImpl/ResIntf and cmt bumped. The new Lambda primitives come after the last constant constructor, as Pdict_spread did.

New tests

  • ast-mapping/SpreadLiterals.res and an ounit case for the v0 wire shape, the exact round trip, PPX-built calls and unmarked calls
  • printer cases for literal spreads, attributes, line breaking and list comments
  • super_errors: array_spread_type_mismatch, list_spread_type_mismatch, array_spread_annotation_mismatch
  • analysis: SpreadLiterals.res (AST dump, hints, highlighting, pipe completion)

A stacked follow-up will fix a formatter bug that predates this PR: a line comment inside a literal used as a switch/if/try/loop operand moves on every reformat.

🤖 Generated with Claude Code

https://claude.ai/code/session_01EUpL6dKCiSwAeriDGRHqNr

@cknitt
cknitt added this pull request to stack #8907 October 18, 2026 18:28
@codecov

codecov Bot commented Oct 18, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 81.86047% with 39 lines in your changes missing coverage. Please review.
✅ Project coverage is 81.02%. Comparing base (1eb91bb) to head (5c58e38).

Files with missing lines Patch % Lines
compiler/ml/printast.ml 0.00% 8 Missing ⚠️
tests/ounit_tests/ounit_ast_mapper0_tests.ml 82.85% 6 Missing ⚠️
compiler/ml/printtyped.ml 0.00% 4 Missing ⚠️
compiler/ml/rec_check.ml 0.00% 4 Missing ⚠️
analysis/src/hint.ml 25.00% 3 Missing ⚠️
analysis/reanalyze/src/arnold.ml 0.00% 2 Missing ⚠️
compiler/ml/ast_spread.ml 94.44% 2 Missing ⚠️
compiler/ml/printlambda.ml 0.00% 2 Missing ⚠️
compiler/ml/typecore.ml 83.33% 2 Missing ⚠️
analysis/src/dump_ast.ml 90.00% 1 Missing ⚠️
... and 5 more
Additional details and impacted files
@@            Coverage Diff             @@
##           master    #8905      +/-   ##
==========================================
- Coverage   81.03%   81.02%   -0.01%     
==========================================
  Files         455      456       +1     
  Lines       63630    63710      +80     
==========================================
+ Hits        51561    51620      +59     
- Misses      12069    12090      +21     
Files with missing lines Coverage Δ
analysis/reanalyze/src/side_effects.ml 78.46% <100.00%> (+0.33%) ⬆️
analysis/src/completion_front_end.ml 81.61% <100.00%> (+0.04%) ⬆️
analysis/src/semantic_tokens.ml 88.63% <ø> (-0.16%) ⬇️
compiler/core/lam_analysis.ml 78.76% <100.00%> (ø)
compiler/core/lam_compile_primitive.ml 81.25% <100.00%> (+0.11%) ⬆️
compiler/ext/config.ml 100.00% <ø> (ø)
compiler/ml/ast_helper.ml 88.25% <100.00%> (+0.03%) ⬆️
compiler/ml/ast_iterator.ml 92.74% <100.00%> (+0.04%) ⬆️
compiler/ml/ast_mapper.ml 98.46% <100.00%> (+0.01%) ⬆️
compiler/ml/ast_mapper_to0.ml 79.32% <100.00%> (+0.07%) ⬆️
... and 24 more

... and 2 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@pkg-pr-new

pkg-pr-new Bot commented Oct 18, 2026 •

Copy link
Copy Markdown

Open in StackBlitz

rescript

npm i https://pkg.pr.new/rescript@8905

@rescript/belt

npm i https://pkg.pr.new/@rescript/belt@8905

@rescript/darwin-arm64

npm i https://pkg.pr.new/@rescript/darwin-arm64@8905

@rescript/darwin-x64

npm i https://pkg.pr.new/@rescript/darwin-x64@8905

@rescript/linux-arm64

npm i https://pkg.pr.new/@rescript/linux-arm64@8905

@rescript/linux-x64

npm i https://pkg.pr.new/@rescript/linux-x64@8905

@rescript/runtime

npm i https://pkg.pr.new/@rescript/runtime@8905

@rescript/win32-x64

npm i https://pkg.pr.new/@rescript/win32-x64@8905

commit: 5c58e38

@github-actions

Copy link
Copy Markdown

@cknitt

cknitt commented Oct 18, 2026

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 18, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-18T19:01:22.918342Z aa5c53b Manual request
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. More of your lovely PRs please.

Reviewed commit: aa5c53bae8

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

The parser no longer turns [a, ...b] and list{...a, b} into calls of
Primitive_array.spread and Primitive_list.spread marked with res.spread.
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 ::.

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

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

Source: the old formatter printed [1, 2, 3, ] and list{...xs, , 1}
without the attribute; the new one prints the input unchanged
(tests/syntax_tests/data/printer/expr).

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.

Review: 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.

Area: syntax printer typechecker lambda codegen analysis reanalyze tests
Output: unchanged, tests/tests JS byte-identical after a rebuild; a
 combinatorial harness of all item/spread sequences up to length 4 compiles
 to identical JS with the old and new bsc
Formatting: changed spreads of array and list literals and attributes on
 literals with spreads are kept; comments in list spreads stay in place
Formats: ast cmt
PPX: unchanged wire: the v0 encoding is the res.spread call it was, decoded
 back to Pexp_spread (ast-mapping fixture SpreadLiterals.res)
Editor: unchanged: pipe completion types the literal as the runtime call;
 SpreadLiterals.res covers AST dump, hints, highlighting and completion
Tests: +tests/syntax_tests/data/ast-mapping/SpreadLiterals.res
 +tests/analysis_tests/tests/src/SpreadLiterals.res
 +tests/build_tests/super_errors/fixtures/array_spread_type_mismatch.res
 +tests/build_tests/super_errors/fixtures/list_spread_type_mismatch.res
 +tests/build_tests/super_errors/fixtures/array_spread_annotation_mismatch.res
 ~tests/ounit_tests/ounit_ast_mapper0_tests.ml
 ~tests/syntax_tests/data/printer/expr/array.res
 ~tests/syntax_tests/data/printer/expr/list.res
 ~tests/syntax_tests/data/printer/comments/array.res
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01EUpL6dKCiSwAeriDGRHqNr
Signed-off-by: Christoph Knittel <ck@cca.io>
@github-actions

Copy link
Copy Markdown

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant