Skip to content

New feature: break/continue in loops#8348

Merged
cknitt merged 6 commits intomasterfrom
break-continue
Apr 17, 2026
Merged

New feature: break/continue in loops#8348
cknitt merged 6 commits intomasterfrom
break-continue

Conversation

@cknitt
Copy link
Copy Markdown
Member

@cknitt cknitt commented Apr 17, 2026

Summary

Add support for break and continue in ReScript while and for loops.

This change makes break and continue real keywords, supports using them from within a switch nested inside a loop, and emits native JS break / continue in the generated output.

Implementation

  • Add break / continue as reserved syntax tokens in the ReScript parser.
  • Add dedicated AST / typedtree / lambda nodes for loop control.
  • Keep parsetree0 frozen and bridge the new nodes through reserved internal extensions in the v0 mappers.
  • Typecheck break / continue as loop-only control flow and reject them in nested function bodies.
  • Reuse warning 21 for break / continue in statement position, matching existing throw(...) behavior, with clearer wording for non-continuing control flow.
  • Lower loop control into JS break / continue.
  • Support breaking/continuing from a nested switch by generating loop labels when needed.
  • Allocate loop labels lazily so only loops that actually need labeled control flow consume loop_<n> ids.

Notes

  • break and continue are now reserved keywords, so existing identifier uses must be escaped or renamed.
  • The backend only emits loop labels when required for correctness, which keeps the generated JS cleaner.
  • String-switch test cases were added alongside integer-switch cases because they preserve a JS switch in the output and therefore exercise the labeled codegen path directly.

Tests

  • Syntax tests for parsing and printing break / continue.
  • Super-error tests for:
    • break / continue outside loops
    • nested-function rejection
    • reserved-keyword binding errors
    • warning 21 on unconditional break / continue statements with following dead code
  • Build test coverage for lambda output and generated JS label emission.
  • Runtime tests in tests/tests for:
    • while + break / continue
    • for + break / continue
    • nested switch targeting the enclosing loop
    • JS-switch cases that exercise labeled loop control

Verification

  • make
  • make lib
  • node tests/build_tests/super_errors/input.js
  • node tests/build_tests/loop_control/input.js
  • node cli/rescript.js build in tests/tests
  • npx mocha -t 10000 tests/tests/src/loop_control_test.mjs

@cknitt cknitt requested a review from zth April 17, 2026 14:00
@cknitt
Copy link
Copy Markdown
Member Author

cknitt commented Apr 17, 2026

@codex review

Copy link
Copy Markdown

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

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: b63aebad8f

ℹ️ 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 compiler/core/lam_compile.ml Outdated
@pkg-pr-new
Copy link
Copy Markdown

pkg-pr-new bot commented Apr 17, 2026

Open in StackBlitz

rescript

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

@rescript/darwin-arm64

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

@rescript/darwin-x64

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

@rescript/linux-arm64

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

@rescript/linux-x64

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

@rescript/runtime

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

@rescript/win32-x64

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

commit: 484e6e7

@cknitt
Copy link
Copy Markdown
Member Author

cknitt commented Apr 17, 2026

@codex review

Copy link
Copy Markdown

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

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: 58f81dc3c7

ℹ️ 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 compiler/ml/typecore.ml Outdated
@cknitt
Copy link
Copy Markdown
Member Author

cknitt commented Apr 17, 2026

@codex review

@chatgpt-codex-connector
Copy link
Copy Markdown

Codex Review: Didn't find any major issues. 🚀

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

@cknitt cknitt merged commit 1642c25 into master Apr 17, 2026
32 checks passed
@cknitt cknitt deleted the break-continue branch April 17, 2026 15:58
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.

2 participants