Skip to content

Declare the variance of iterator, generator and tagged template types - #8981

Merged
cknitt merged 1 commit into
masterfrom
stdlib-variance
Oct 23, 2026
Merged

cknitt merged 1 commit into
masterfrom
stdlib-variance

Conversation

@cknitt

@cknitt cknitt commented Oct 23, 2026

Copy link
Copy Markdown
Member

Part of the groundwork for generator functions (RFC #8934), following the variance feedback there.

The iterator and generator types were invariant, so a Generator.t<[#A], _, _> couldn't be coerced to a Generator.t<[#A | #B], _, _>, while the equivalent iterable coercion worked. Library types defined as aliases of Generator.t, such as effect types with polymorphic-variant error rows, also weren't generalised under the relaxed value restriction. Yield and return values only flow out of an iterator and resume values only flow in, so this declares:

Type Variance
Iterator.t, AsyncIterator.t, Generator.t, AsyncGenerator.t, IterableIterator.t, AsyncIterableIterator.t, IteratorObject.t <+'yield, +'return, -'next>
Iterator.rawResult, AsyncIterator.rawResult <+'yield, +'return>
Array.arrayLike <+'a> (it is only read)
predef taggedTemplate <-'param, +'output> (it is a function)

Annotations only accept more programs, so existing code keeps compiling.

Audit of the rest of the stdlib

  • Correctly invariant: the mutable containers (array, dict, Map, Set, WeakMap, WeakSet, TypedArray, ref) and the phantom tags (JsxEvent.synthetic, the Dom hierarchy).
  • Already covariant: iterable, asyncIterable, promise, option, list, result, null, undefined, nullable and Lazy.t. Jsx.component is already contravariant.
  • Belt, left out on purpose:
    • The immutable maps (Map, MapDict, MapInt, MapString) could be covariant in their values. But their AVL nodes have mutable fields (mutated only while building fresh trees), so the implementations are inferred invariant. Declaring +'value would need an abstract implementation type and casts throughout those modules.
    • Keys and 'id stay invariant everywhere, because they flow into the comparator.

Caveat on +'return

Generator.returnValue takes a 'return. A generator shared under two types could:

  1. be resumed through returnValue with a wider value while a finally block yields;
  2. then hand that value to the narrower reference.

This case is contrived, and TypeScript's Generator has the same hole. Keeping 'return invariant would lose most of the benefit.

Tests

  • tests/tests/src/stdlib_variance_test.res:
    • coercions for every annotated type;
    • jfrolich's value-restriction repro on top of Generator.t;
    • runtime checks for a widened generator and a narrowed tagged template.
  • super_errors/GeneratorResumeTypeWidening: widening the resume type is still rejected.

🤖 Generated with Claude Code

https://claude.ai/code/session_01Fc7LEuyEfynjaKCVEBPmTF

Why:
    The iterator family's type parameters were invariant, so a generator
    of `[#A]` could not be coerced to one of `[#A | #B]`, and values of
    library types built on `Generator.t` were not generalised under the
    relaxed value restriction. Yields and return values only flow out of
    an iterator and resume values only flow in. Needed for generators
    (#8934).

Typing modified (coercion):
Example:
    let g: Generator.t<[#A], int, [#X | #Y]> = gen()
    let h = (g :> Generator.t<[#A | #B], int, [#X]>)
Before (rule):
    t<y, r, n> ≤ t<y', r', n'> only if y = y', r = r', n = n'
After (rule):
    y ≤ y'   r ≤ r'   n' ≤ n
    ───────────────────────── (Iterator variance)
    t<y, r, n> ≤ t<y', r', n'>

    t ∈ {Iterator, AsyncIterator, Generator, AsyncGenerator, IterableIterator, AsyncIterableIterator, IteratorObject}
    rawResult<+y, +r>   arrayLike<+a>   taggedTemplate<-param, +output>

Review: Is `+'return` sound when `Generator.returnValue` takes a `'return`? Only up to a contrived case: a generator shared under two types, resumed through `returnValue` while a `finally` block yields, hands the wider value to the narrower reference. TypeScript's `Generator` has the same hole.

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

Semantic Diff

1 commit · Full page

Declare the variance of iterator, generator and tagged template types

85cf497 · Christoph Knittel with Claude Opus 5.5 · Oct 23, 2026

Why

The iterator family's type parameters were invariant, so a generator of [#A] could not be coerced to one of [#A | #B], and values of library types built on Generator.t were not generalised under the relaxed value restriction. Yields and return values only flow out of an iterator and resume values only flow in. Needed for generators (#8934).

Typing modified (coercion)

let g: Generator.t<[#A], int, [#X | #Y]> = gen()
let h = (g :> Generator.t<[#A | #B], int, [#X]>)

rule

BeforeAfter
$$\begin{array}{l}\color{red}{t<y,r,n>≤t<y',r',n'>\mathrm{only}\ \mathrm{if}\ y=y',r=r',n=n'}\end{array}$$ $$\begin{array}{l}\dfrac{\color{green}{y≤y'} \qquad \color{green}{r≤r'} \qquad \color{green}{n'≤n}}{\begin{array}{c}\color{green}{t<y,r,n>≤t<y',r',n'>}\end{array}} \quad \color{green}{(\textsf{Iterator variance})} \\[1em] \begin{array}{l}\color{green}{t∈\{\mathrm{Iterator},\mathrm{AsyncIterator},\mathrm{Generator},\mathrm{AsyncGenerator},\mathrm{IterableIterator},\mathrm{AsyncIterableIterator},\mathrm{IteratorObject}\}} \\ \color{green}{\mathrm{rawResult}<+y,+r>} \qquad \color{green}{\mathrm{arrayLike}<+a>} \qquad \color{green}{\mathrm{taggedTemplate}<-\mathrm{param},+\mathrm{output}>}\end{array}\end{array}$$

Review

  • Is +'return sound when Generator.returnValue takes a 'return? Only up to a contrived case: a generator shared under two types, resumed through returnValue while a finally block yields, hands the wider value to the narrower reference. TypeScript's Generator has the same hole.

@cknitt

cknitt commented Oct 23, 2026

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 23, 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-23T06:01:09.174955Z 85cf497 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.

@cknitt
cknitt requested a review from cristianoc October 23, 2026 05:59
@cknitt
cknitt marked this pull request as ready for review October 23, 2026 05:59

@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: 85cf497a87

ℹ️ 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 packages/@rescript/runtime/Stdlib_Generator.resi
@codecov

codecov Bot commented Oct 23, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 81.24%. Comparing base (9e2f371) to head (85cf497).

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #8981      +/-   ##
==========================================
+ Coverage   81.22%   81.24%   +0.01%     
==========================================
  Files         457      457              
  Lines       63862    63863       +1     
==========================================
+ Hits        51875    51888      +13     
+ Misses      11987    11975      -12     
Files with missing lines Coverage Δ
compiler/ml/predef.ml 100.00% <100.00%> (ø)

... 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 23, 2026

Copy link
Copy Markdown

Open in StackBlitz

rescript

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

@rescript/belt

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

@rescript/darwin-arm64

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

@rescript/linux-arm64

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

@rescript/linux-x64

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

@rescript/runtime

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

@rescript/win32-x64

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

commit: 85cf497

@github-actions

Copy link
Copy Markdown

@cknitt
cknitt enabled auto-merge (squash) October 23, 2026 11:04
@cknitt
cknitt disabled auto-merge October 23, 2026 11:04
@cknitt
cknitt merged commit 03d93cc into master Oct 23, 2026
26 checks passed
@cknitt
cknitt deleted the stdlib-variance branch October 23, 2026 11:05
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