Skip to content

Commit 03d93cc

Browse files
cknittclaude
andauthored
Declare the variance of iterator, generator and tagged template types (#8981)
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. Claude-Session: https://claude.ai/code/session_01Fc7LEuyEfynjaKCVEBPmTF Signed-off-by: Christoph Knittel <ck@cca.io> Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
1 parent f62f83c commit 03d93cc

22 files changed

Lines changed: 221 additions & 21 deletions

‎changes/stdlib-variance.feature.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
- Iterator and generator types are covariant in their yield and return types and contravariant in their resume type, `Array.arrayLike` is covariant, and `taggedTemplate` is contravariant in its parameters and covariant in its output.

‎compiler/ml/predef.ml‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -367,7 +367,8 @@ let common_initial_env add_type add_extension empty_env =
367367
decl_abstr with
368368
type_params = [tvar1; tvar2];
369369
type_arity = 2;
370-
type_variance = [Variance.full; Variance.full];
370+
type_variance =
371+
[Variance.conjugate Variance.covariant; Variance.covariant];
371372
}
372373
in
373374

‎packages/@rescript/runtime/Stdlib_Array.res‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
type t<'a> = array<'a>
2-
type arrayLike<'a>
2+
type arrayLike<+'a>
33

44
@new external makeUninitializedUnsafe: int => array<'a> = "Array"
55
@set external truncateToLengthUnsafe: (array<'a>, int) => unit = "length"

‎packages/@rescript/runtime/Stdlib_Array.resi‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ Type representing an array of value `'a`.
88
*/
99
type t<'a> = array<'a>
1010

11-
type arrayLike<'a>
11+
type arrayLike<+'a>
1212

1313
/**
1414
`asIterable(array)` views `array` as an `Iterable.t`.

‎packages/@rescript/runtime/Stdlib_AsyncGenerator.res‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
type t<'yield, 'return, 'next> = Stdlib_AsyncIterable.t<'yield>
1+
type t<+'yield, +'return, -'next> = Stdlib_AsyncIterable.t<'yield>
22

33
external asAsyncIterable: t<'yield, 'return, 'next> => Stdlib_AsyncIterable.t<'yield> = "%identity"
44
external asAsyncIterableIterator: t<'yield, 'return, 'next> => Stdlib_AsyncIterableIterator.t<

‎packages/@rescript/runtime/Stdlib_AsyncGenerator.resi‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ See [`AsyncGenerator`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/R
1010
/**
1111
Type representing an async generator object.
1212
*/
13-
type t<'yield, 'return, 'next> = private Stdlib_AsyncIterable.t<'yield>
13+
type t<+'yield, +'return, -'next> = private Stdlib_AsyncIterable.t<'yield>
1414

1515
/**
1616
`asAsyncIterable(generator)` views `generator` as an `AsyncIterable.t`.

‎packages/@rescript/runtime/Stdlib_AsyncIterableIterator.res‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
type t<'yield, 'return, 'next> = Stdlib_AsyncIterable.t<'yield>
1+
type t<+'yield, +'return, -'next> = Stdlib_AsyncIterable.t<'yield>
22

33
let make: (unit => promise<Stdlib_Iterator.result<'yield, 'return>>) => t<
44
'yield,

‎packages/@rescript/runtime/Stdlib_AsyncIterableIterator.resi‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ See [`AsyncIterator`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Re
1010
/**
1111
Type representing an async iterable iterator.
1212
*/
13-
type t<'yield, 'return, 'next> = private Stdlib_AsyncIterable.t<'yield>
13+
type t<+'yield, +'return, -'next> = private Stdlib_AsyncIterable.t<'yield>
1414

1515
/**
1616
`make(nextFn)` creates an async iterable iterator from a function returning the next result.

‎packages/@rescript/runtime/Stdlib_AsyncIterator.res‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
@notUndefined
2-
type t<'yield, 'return, 'next>
2+
type t<+'yield, +'return, -'next>
33

4-
type rawResult<'yield, 'return>
4+
type rawResult<+'yield, +'return>
55

66
type result<'yield, 'return> = Stdlib_Iterator.result<'yield, 'return>
77

‎packages/@rescript/runtime/Stdlib_AsyncIterator.resi‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,14 @@ Type representing an async iterator protocol object.
1515
- `'next` is the type that can be sent back into `next(value)`
1616
*/
1717
@notUndefined
18-
type t<'yield, 'return, 'next>
18+
type t<+'yield, +'return, -'next>
1919

2020
/**
2121
Result returned by `AsyncIterator.next`.
2222
*/
2323
type result<'yield, 'return> = Stdlib_Iterator.result<'yield, 'return>
2424

25-
type rawResult<'yield, 'return>
25+
type rawResult<+'yield, +'return>
2626

2727
/**
2828
Low-level raw JavaScript async iterator result shape.

0 commit comments

Comments
 (0)