Skip to content

Accept private iterables in for..of - #8937

Merged
cknitt merged 1 commit into
masterfrom
for-of-private-iterables
Oct 21, 2026
Merged

cknitt merged 1 commit into
masterfrom
for-of-private-iterables

Conversation

@cknitt

@cknitt cknitt commented Oct 21, 2026

Copy link
Copy Markdown
Member

Generator.t, IterableIterator.t, IteratorObject.t, AsyncGenerator.t and AsyncIterableIterator.t are private abbreviations of iterable / asyncIterable (#8355). A private abbreviation doesn't unify with its expansion, so iterating one needed a conversion first:

let f = (g: Generator.t<int, string, unit>) =>
  for x of g->Generator.asIterable { … } // `for x of g` was a type error

for..of and for await..of now also unify the collection's type through its private abbreviations, as an explicit :> would, so for x of g works. Only after the usual unification fails is the expansion tried, and a failure still reports the type as written. The generated JavaScript is unchanged.

for await..of now types its collection first and then unifies, like for..of already did, instead of typing it against the expected type. Its existing error fixture is unchanged.

This only covers loops. Function arguments still need asIterable (Array.fromIterable(gen)), since ReScript applies no implicit coercions there. Map.t and Set.t are abstract types, not private iterables, so they still need asIterable as well. Re-declaring them would drop their @notUndefined (which is only allowed on abstract types) and change how option<Map.t<_>> is represented, so it is left for a separate change.

Part of the groundwork for generator functions (#8934).

Test: tests/tests/src/for_of_private_iterable_test.res iterates a generator (including break), an iterator object from Array.values, and an async generator with for await.

🤖 Generated with Claude Code

https://claude.ai/code/session_01Fc7LEuyEfynjaKCVEBPmTF

Why:
    `Generator.t`, `IteratorObject.t` and the other iterator types are
    private abbreviations of `iterable`, so `for..of` rejected them and
    needed `->asIterable` first.

`for..of` and `for await..of` now also unify the collection's type through
its private abbreviations, as an explicit `:>` would. Errors still report
the type as written.

Typing modified (for..of collection):
Before (rule):
    Γ ⊢ e : iterable<τ>    Γ, x : τ ⊢ body : unit
    ───────────────────────────────────────────── (for-of)
    Γ ⊢ `for x of e { body }` : unit
After (rule):
    Γ ⊢ e : σ    σ ≤ iterable<τ>    Γ, x : τ ⊢ body : unit
    ───────────────────────────────────────────────────── (for-of)
    Γ ⊢ `for x of e { body }` : unit

Diagnostic removed (for..of over a generator):
Example:
    let f = (g: Generator.t<int, string, unit>) =>
      for x of g {
        Console.log(x)
      }
Before (diagnostic):
    This has type:
      Generator.t<int, string, unit>
    But it's expected to have type: iterable<'a>

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Fc7LEuyEfynjaKCVEBPmTF
Signed-off-by: Christoph Knittel <ck@cca.io>
@github-actions

Copy link
Copy Markdown

@codecov

codecov Bot commented Oct 21, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 81.14%. Comparing base (d19633d) to head (e3c5eaf).

Additional details and impacted files
@@           Coverage Diff           @@
##           master    #8937   +/-   ##
=======================================
  Coverage   81.13%   81.14%           
=======================================
  Files         456      456           
  Lines       63754    63766   +12     
=======================================
+ Hits        51728    51740   +12     
  Misses      12026    12026           
Files with missing lines Coverage Δ
compiler/ml/typecore.ml 86.84% <100.00%> (+0.06%) ⬆️
🚀 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 21, 2026

Copy link
Copy Markdown

Open in StackBlitz

rescript

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

@rescript/belt

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

@rescript/darwin-arm64

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

@rescript/linux-arm64

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

@rescript/linux-x64

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

@rescript/runtime

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

@rescript/win32-x64

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

commit: e3c5eaf

@github-actions

Copy link
Copy Markdown

@cknitt

cknitt commented Oct 21, 2026

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 21, 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-21T06:45:15.335497Z e3c5eaf 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. Chef's kiss.

Reviewed commit: e3c5eaf08c

ℹ️ 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 marked this pull request as ready for review October 21, 2026 07:56
@cknitt
cknitt requested a review from cristianoc October 21, 2026 07:56

@cristianoc cristianoc left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Pr description hard to follow.
Semantic diff much cleaner.
So yes @cknitt we can do the PR aspect too, soon.

@cknitt
cknitt merged commit 2b7ac3e into master Oct 21, 2026
28 checks passed
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