Conversation
rescript
@rescript/darwin-arm64
@rescript/darwin-x64
@rescript/linux-arm64
@rescript/linux-x64
@rescript/runtime
@rescript/win32-x64
commit: |
77f91d8 to
8b779af
Compare
|
The big questions were language design related ones, rather than implementation. |
|
Frankly, importing the JavaScript for-of syntax as-is doesn't sound all that appealing. It's not much different from embedding Here are my thoughts on designing for loops:
|
https://github.com/JonoPrest/iterator-rescript/blob/retreat-note/RETREAT_NOTE.md On the last retreat, we only discussed iterator semantics and simulating break/continue; we also need to deal with the early-return or jump (for escaping nested loops) semantics. |
|
Early return keeps on coming up as a topic. |
|
For me, the for..of feature as implemented here would already be useful in and of itself because it
let asyncProcess = async () => {
let results = []
for item of arr {
let result = await processData(item)
results->Array.push(result)
}
results
}
for item for arr {
# vs.
arr->Array.forEach(item =>
I agree that early return would be nice to have, but not sure if we should tie this to for..of. I also agree that it would be a good idea to look into how iterators should work. |
This is already a dangerous assumption. for-of loops are all about iterators, not arrays. It should be designed to interact with iterator types. When it works with array types, it leads users to copy arbitrary iterators into arrays unnecessarily. |
|
@codex review |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 2a96c795ce
ℹ️ 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".
for...of and for await...of loops
Done! 😄 |
|
@codex review |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 4389e9c1c4
ℹ️ 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".
| | Int_switch _ | String_switch _ | ForRange _ | ForOf _ | ForAwaitOf _ | ||
| | If _ | While _ | Block _ | Return _ | Try _ -> |
There was a problem hiding this comment.
Treat iterator loops as side-effecting in JS analyzer
no_side_effect_obj.statement now classifies ForOf and ForAwaitOf via structural traversal, so loops with a pure-looking iterable expression/body are considered removable. Unlike ForRange, these loops invoke iterator protocol methods (Symbol.iterator/next or async variants), which can execute arbitrary user code even when the iterable is just an identifier; this feeds into js_shake's statement pruning and can incorrectly drop runtime-visible effects.
Useful? React with 👍 / 👎.
| | Texp_for_of (_id, _pat, e1, e2) -> | ||
| e1 |> exprNoSideEffects && e2 |> exprNoSideEffects | ||
| | Texp_for_await_of (_id, _pat, e1, e2) -> | ||
| e1 |> exprNoSideEffects && e2 |> exprNoSideEffects |
There was a problem hiding this comment.
Mark iterator loops effectful in reanalyze side-effects
exprNoSideEffects now returns true for Texp_for_of/Texp_for_await_of when only the loop expression/body look pure, but iteration itself can run effectful iterator callbacks on custom iterables. Since SideEffects.checkExpr is used to set declaration side-effect metadata in dead-value reporting, this can misclassify effectful values as effect-free and produce incorrect dead-code diagnostics.
Useful? React with 👍 / 👎.
|
@codex review |
|
Codex Review: Didn't find any major issues. Bravo. ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
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". |
Summary
This PR adds native
for...ofandfor await...ofloop support to ReScript across the full compiler pipeline.It introduces parser, AST, typedtree, lambda IR, and JS backend support for both loop forms, preserves
break/continuesemantics inside nested control-flow, and adds syntax, type-error, analysis, and end-to-end test coverage for the new feature.What changed
for item of items { ... }for await item of items { ... }for...of/for await...ofnodes instead of lowering them too early.for...ofagainstiterable<'a>with a convenience fallback for rawarray<'a>values, and type-checkfor await...ofagainstasyncIterable<'a>._, and report a targeted error for unsupported destructuring patterns on the left-hand side.for...ofandfor await...ofstatements.breakandcontinuestill target the enclosing loop correctly, including when emitted through nested JSswitchstatements.Coverage
break/continuein plain loops and nested switchesfor...ofandfor await...of.