Skip to content

Support ReScript 13 control flow and spread syntax - #310

Open
aspeddro wants to merge 8 commits into
rescript-lang:mainfrom
aspeddro:feat/rescript-13-control-flow
Open

aspeddro wants to merge 8 commits into
rescript-lang:mainfrom
aspeddro:feat/rescript-13-control-flow

Conversation

@aspeddro

@aspeddro aspeddro commented Nov 15, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

Support ReScript 13 iteration, cleanup clauses, dictionary spreads, and record rest destructuring. Highlight loop-control expressions while preserving compatibility with older code that uses break and continue as identifiers.

Iterable and asynchronous loops

Support variable and wildcard bindings, parenthesized headers, and expression-based iterables while preserving numeric loop syntax.

for x of xs { log(x) }
for item of xs->Array.map(x => x + 1) { log(item) }
for await (x of xs) { log(x) }

Try/finally

Support cleanup blocks with or without catch, including nested expressions and comments between clauses. Recognize finally as a cleanup clause only in an active try context when a block follows it, preserving ordinary identifiers and JSX attribute boundaries.

try work() finally { cleanup() }

try work() catch {
| _ => recover()
}
/* cleanup */
finally { cleanup() }

// `finally` remains a separate JSX attribute.
<C value=try f() catch { | _ => () } finally=true />

// These remain three separate statements.
let finally = ()
finally
{ () }

Dictionary spreads

Allow spreads before, between, and after entries, including nested dictionary literals.

dict{...foo, "bar": 2, ...qux}
dict{"a": 1, ...dict{"b": 2}}

Backward-compatible break and continue highlighting

Parse break and continue as value_identifier nodes and highlight standalone expression statements as keyword.repeat. Do not reserve either name or introduce dedicated loop-control nodes. Bindings and member access retain normal variable highlighting.

for x of xs {
  if skip(x) { continue }
  if done(x) { break }
  work(x)
}

// Legacy identifiers remain supported.
let continue = ref(true)
while continue.contents {
  continue := false
}

The original reserved-keyword implementation in b15a6d1 was reverted in 74b76f9. This intentionally leaves version-specific keyword restrictions to the ReScript compiler. A standalone legacy reference named break or continue also receives keyword highlighting because its syntax is indistinguishable from loop control.

Record rest destructuring

Support inferred and explicitly typed rest bindings, including qualified generic types, function parameters, and nested patterns. Reject multiple rest bindings in the same record pattern.

let {a, ...rest} = value
let {a, ...M.Sub.t<'a, int> as rest} = value
let f = ({?name, ...rest}) => rest

Validation

  • All 197 corpus tests pass, including compatibility and contextual-finally regressions.
  • All syntax-highlighting tests pass, including keyword and legacy-variable assertions.
  • ESLint, Prettier checks, and git diff --check pass.
  • All 2,084 wild-project files parse successfully, including the previously failing HyperIndex fixture that uses continue as a variable.

Scope

Dollar identifiers and the other previously audited syntax gaps are outside this PR's scope.

References

Support ReScript 13 iteration over synchronous and asynchronous sources,
including parenthesized headers, without changing numeric loop trees.
Keep cleanup clauses attached across newlines and comments while
preserving finally as an identifier when no block follows it.
Allow ReScript dictionary composition to mix entries and spreads in
source order, including nested dictionaries and trailing commas.
Use ReScript 13 keyword semantics instead of treating loop control as
identifier references, while retaining escaped identifier support.
Parse inferred and explicitly typed rest bindings in record patterns,
including qualified generic types, while rejecting duplicate rests.
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 15, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

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

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-15T07:00:27.103652Z 74b76f9 New commits
ℹ️ 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 chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 10ca34478e

ℹ️ 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".

Comment thread grammar.js Resolved
Require a following cleanup block before recognizing a finally clause.
Semicolon lookahead alone cannot distinguish JSX attribute boundaries.

Cover adjacent attributes, newlines, comments, and cleanup clauses
followed by a finally attribute without changing existing syntax trees.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 4f963f08ae

ℹ️ 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".

Comment thread src/scanner.c Resolved
Preserve statement boundaries when finally is an ordinary identifier.
Only the guarded cleanup-clause scanner paths should recognize it as
a continuation. Add a regression for an identifier followed by a block.
Revert b15a6d1 to keep older
ReScript code compatible. Highlight standalone break and continue
expressions as keywords without reserving either identifier.

Cover loop control, legacy bindings, and member access in parsing and
highlighting tests. All 2,084 wild-project files parse successfully.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 74b76f9674

ℹ️ 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".

Comment thread grammar.js
choice(
seq(
'for',
optional('await'),

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Preserve await as a numeric-loop binding name

For an existing numeric loop such as for await in 0 to 3 { () }, the newly available await literal wins over value_identifier immediately after for. The parser therefore commits to the asynchronous iterable branch and errors at in, whereas the parent parser accepts this loop (await is not globally reserved). Keep the async prefix contextual so the numeric branch can still use this binding name.

Useful? React with 👍 / 👎.

Comment thread src/scanner.c
if (!scan_keyword(lexer, "finally")) return false;
for (;;) {
scan_whitespace(lexer, true);
if (lexer->lookahead != '/') return lexer->lookahead == '{';

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Distinguish JSX spread props from cleanup blocks

Unlike the repaired finally=true boundary, a bare finally prop followed by a spread prop still fails: <C value=try f() catch { | _ => () } finally {...props} /> is accepted by the parent parser but now errors at .... The {-only lookahead treats the spread prop's braces as a cleanup block and emits FINALLY_CLAUSE_START, preventing the valid jsx_attribute followed by jsx_expression interpretation. Preserve that alternative when the following braces cannot form a cleanup block.

Useful? React with 👍 / 👎.

Comment thread grammar.js
record_rest_pattern: ($) =>
seq(
'...',
optional(seq($._non_function_inline_type, 'as')),

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Preserve unit as an inferred record-rest binding

For let {a, ...unit} = value, the optional _non_function_inline_type prefix makes unit tokenize as unit_type instead of the non-reserved value_identifier. The parser then requires as and rejects the closing }, so the new inferred-rest syntax fails for a binding name that works in ordinary record patterns and let bindings. Retain the identifier interpretation when there is no following as.

Useful? React with 👍 / 👎.

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