Repository navigation
Declare the variance of iterator, generator and tagged template types #8981
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+221
−21
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +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. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 18 additions & 0 deletions
18
tests/build_tests/super_errors/expected/GeneratorResumeTypeWidening.res.expected
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
|
|
||
| [1;31mWe've found a bug for you![0m | ||
| [36m/.../fixtures/GeneratorResumeTypeWidening.res[0m:[2m2:51-88[0m | ||
|
|
||
| 1 [2m│[0m // A generator resumed with `[#X]` can't be resumed with `[#X | #Y]`. | ||
| [1;31m2[0m [2m│[0m let widen = (g: Generator.t<int, unit, [#X]>) => ([1;31mg :> Generator.t<int, [0m | ||
| [2m│[0m [1;31munit, [#X | #Y]>)[0m | ||
| 3 [2m│[0m | ||
|
|
||
| Type Generator.t<int, unit, [#X]> = Generator.t<int, unit, [#X]> | ||
| is not a subtype of | ||
| Generator.t<int, unit, [#X | #Y]> = Generator.t<int, unit, [#X | #Y]> | ||
|
|
||
| The second polymorphic variant is [1;33mclosed[0m and doesn't include the constructor: [1;31m#Y[0m. | ||
|
|
||
| Possible solutions: | ||
| - Either make the second variant [1;33mopen[0m so it can accept additional constructors. To do this, make sure the type starts with [1;33m[>[0m instead of [1;33m[[0m | ||
| - Or add the missing constructor to it. |
2 changes: 2 additions & 0 deletions
2
tests/build_tests/super_errors/fixtures/GeneratorResumeTypeWidening.res
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| // A generator resumed with `[#X]` can't be resumed with `[#X | #Y]`. | ||
| let widen = (g: Generator.t<int, unit, [#X]>) => (g :> Generator.t<int, unit, [#X | #Y]>) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,107 @@ | ||
| // Generated by ReScript, PLEASE EDIT WITH CARE | ||
|
|
||
| import * as Mocha from "mocha"; | ||
| import * as Test_utils from "./test_utils.mjs"; | ||
| import * as Stdlib_TaggedTemplate from "@rescript/runtime/lib/es6/Stdlib_TaggedTemplate.mjs"; | ||
|
|
||
| function widenIterator(i) { | ||
| return i; | ||
| } | ||
|
|
||
| function widenAsyncIterator(i) { | ||
| return i; | ||
| } | ||
|
|
||
| function widenRawResult(r) { | ||
| return r; | ||
| } | ||
|
|
||
| function widenAsyncRawResult(r) { | ||
| return r; | ||
| } | ||
|
|
||
| function widenGenerator(g) { | ||
| return g; | ||
| } | ||
|
|
||
| function widenAsyncGenerator(g) { | ||
| return g; | ||
| } | ||
|
|
||
| function widenIterableIterator(i) { | ||
| return i; | ||
| } | ||
|
|
||
| function widenAsyncIterableIterator(i) { | ||
| return i; | ||
| } | ||
|
|
||
| function widenIteratorObject(i) { | ||
| return i; | ||
| } | ||
|
|
||
| function widenArrayLike(a) { | ||
| return a; | ||
| } | ||
|
|
||
| function widenTaggedTemplate(t) { | ||
| return t; | ||
| } | ||
|
|
||
| function fail(param) { | ||
| return ((function* () {})()); | ||
| } | ||
|
|
||
| function map(m, param) { | ||
| return m; | ||
| } | ||
|
|
||
| let Effect = { | ||
| fail: fail, | ||
| map: map | ||
| }; | ||
|
|
||
| let failing = fail("A"); | ||
|
|
||
| let letters = (function* () { | ||
| yield "A" | ||
| }); | ||
|
|
||
| let joinTag = Stdlib_TaggedTemplate.make((strings, params) => strings.join("|") + ":" + params.map(p => p).join(",")); | ||
|
|
||
| Mocha.describe("Stdlib_variance_test", () => { | ||
| Mocha.test("a widened generator yields the same values", () => { | ||
| let widened = letters(); | ||
| let seen = []; | ||
| for (let x of widened) { | ||
| seen.push(x); | ||
| } | ||
| Test_utils.eq("File \"stdlib_variance_test.res\", line 64, characters 7-14", ["A"], seen); | ||
| }); | ||
| Mocha.test("a tag accepting more parameters is used where fewer are expected", () => Test_utils.eq("File \"stdlib_variance_test.res\", line 69, characters 7-14", "a|b:x", joinTag`a${"x"}b`)); | ||
| }); | ||
|
|
||
| let useWithB = failing; | ||
|
|
||
| let useWithC = failing; | ||
|
|
||
| export { | ||
| widenIterator, | ||
| widenAsyncIterator, | ||
| widenRawResult, | ||
| widenAsyncRawResult, | ||
| widenGenerator, | ||
| widenAsyncGenerator, | ||
| widenIterableIterator, | ||
| widenAsyncIterableIterator, | ||
| widenIteratorObject, | ||
| widenArrayLike, | ||
| widenTaggedTemplate, | ||
| Effect, | ||
| failing, | ||
| useWithB, | ||
| useWithC, | ||
| letters, | ||
| joinTag, | ||
| } | ||
| /* failing Not a pure module */ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,71 @@ | ||
| open Mocha | ||
| open Test_utils | ||
|
|
||
| // Iterator-like types are covariant in their yield and return types and | ||
| // contravariant in their resume type, so these coercions type-check. | ||
| let widenIterator = (i: Iterator.t<[#A], [#R], [#X | #Y]>) => | ||
| (i :> Iterator.t<[#A | #B], [#R | #S], [#X]>) | ||
| let widenAsyncIterator = (i: AsyncIterator.t<[#A], [#R], [#X | #Y]>) => | ||
| (i :> AsyncIterator.t<[#A | #B], [#R | #S], [#X]>) | ||
| let widenRawResult = (r: Iterator.rawResult<[#A], [#R]>) => | ||
| (r :> Iterator.rawResult<[#A | #B], [#R | #S]>) | ||
| let widenAsyncRawResult = (r: AsyncIterator.rawResult<[#A], [#R]>) => | ||
| (r :> AsyncIterator.rawResult<[#A | #B], [#R | #S]>) | ||
| let widenGenerator = (g: Generator.t<[#A], [#R], [#X | #Y]>) => | ||
| (g :> Generator.t<[#A | #B], [#R | #S], [#X]>) | ||
| let widenAsyncGenerator = (g: AsyncGenerator.t<[#A], [#R], [#X | #Y]>) => | ||
| (g :> AsyncGenerator.t<[#A | #B], [#R | #S], [#X]>) | ||
| let widenIterableIterator = (i: IterableIterator.t<[#A], [#R], [#X | #Y]>) => | ||
| (i :> IterableIterator.t<[#A | #B], [#R | #S], [#X]>) | ||
| let widenAsyncIterableIterator = (i: AsyncIterableIterator.t<[#A], [#R], [#X | #Y]>) => | ||
| (i :> AsyncIterableIterator.t<[#A | #B], [#R | #S], [#X]>) | ||
| let widenIteratorObject = (i: IteratorObject.t<[#A], [#R], [#X | #Y]>) => | ||
| (i :> IteratorObject.t<[#A | #B], [#R | #S], [#X]>) | ||
| let widenArrayLike = (a: Array.arrayLike<[#A]>) => (a :> Array.arrayLike<[#A | #B]>) | ||
|
|
||
| // A tag is contravariant in its parameters and covariant in its output. | ||
| let widenTaggedTemplate = (t: taggedTemplate<[#A | #B], [#R]>) => | ||
| (t :> taggedTemplate<[#A], [#R | #S]>) | ||
|
|
||
| // A library type defined as an alias of `Generator.t` stays polymorphic under | ||
| // the relaxed value restriction, so one value can be used at several types. | ||
| module Effect: { | ||
| type step<+'e> | ||
| type t<'a, 'e> = Generator.t<step<'e>, 'a, unknown> | ||
| let fail: 'e => t<'a, 'e> | ||
| let map: (t<'a, 'e>, 'a => 'b) => t<'b, 'e> | ||
| } = { | ||
| type step<+'e> | ||
| type t<'a, 'e> = Generator.t<step<'e>, 'a, unknown> | ||
| let fail = _ => %raw(`(function* () {})()`) | ||
| let map = (m, _) => Obj.magic(m) | ||
| } | ||
|
|
||
| let failing = Effect.fail(#A)->Effect.map(x => x + 1) | ||
| let useWithB: Effect.t<int, [#A | #B]> = failing | ||
| let useWithC: Effect.t<int, [#A | #C]> = failing | ||
|
|
||
| let letters: unit => Generator.t<[#A], unit, unit> = %raw(`function* () { | ||
| yield "A" | ||
| }`) | ||
|
|
||
| let joinTag: taggedTemplate<[#x | #y], string> = TaggedTemplate.make(( | ||
| strings, | ||
| params: array<[#x | #y]>, | ||
| ) => strings->Array.join("|") ++ ":" ++ params->Array.map(p => (p :> string))->Array.join(",")) | ||
|
|
||
| describe(__MODULE__, () => { | ||
| test("a widened generator yields the same values", () => { | ||
| let widened = (letters() :> Generator.t<[#A | #B], unit, unit>) | ||
| let seen = [] | ||
| for x of widened { | ||
| seen->Array.push(x) | ||
| } | ||
| eq(__LOC__, [#A], seen) | ||
| }) | ||
|
|
||
| test("a tag accepting more parameters is used where fewer are expected", () => { | ||
| let onlyX = (joinTag :> taggedTemplate<[#x], string>) | ||
| eq(__LOC__, "a|b:x", onlyX`a${#x}b`) | ||
| }) | ||
| }) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.