Repository navigation
Monomorphize generic overload calls and call generic methods through Pick views #1511
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
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
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
6 changes: 6 additions & 0 deletions
6
...mpiler/test/ts7/baselines/order-parity/tests/corpus/6495-generic-method-overloads.ts.json
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,6 @@ | ||
| { | ||
| "order": [ | ||
| "<repo>/tests/corpus/6495-generic-method-overloads.ts" | ||
| ], | ||
| "diags": [] | ||
| } |
6 changes: 6 additions & 0 deletions
6
...es/compiler/test/ts7/baselines/order-parity/tests/corpus/6496-overload-signatures.ts.json
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,6 @@ | ||
| { | ||
| "order": [ | ||
| "<repo>/tests/corpus/6496-overload-signatures.ts" | ||
| ], | ||
| "diags": [] | ||
| } |
6 changes: 6 additions & 0 deletions
6
...piler/test/ts7/baselines/order-parity/tests/corpus/6497-generic-overload-bindings.ts.json
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,6 @@ | ||
| { | ||
| "order": [ | ||
| "<repo>/tests/corpus/6497-generic-overload-bindings.ts" | ||
| ], | ||
| "diags": [] | ||
| } |
6 changes: 0 additions & 6 deletions
6
packages/compiler/test/ts7/baselines/order-parity/tests/diagnostics/overload-fences.ts.json
This file was deleted.
Oops, something went wrong.
6 changes: 6 additions & 0 deletions
6
...piler/test/ts7/baselines/order-parity/tests/diagnostics/pick-view-generic-methods.ts.json
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,6 @@ | ||
| { | ||
| "order": [ | ||
| "<repo>/tests/diagnostics/pick-view-generic-methods.ts" | ||
| ], | ||
| "diags": [] | ||
| } |
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,54 @@ | ||
| // Calls that resolve to OVERLOAD signatures of a generic method or | ||
| // function: the implementation monomorphizes under what the overload | ||
| // proves for its type parameters (a parameter the overload never mentions | ||
| // is unknown), directly and through a `Pick` view of the class, whose | ||
| // live receiver keeps the instance's state. `T | Promise<T>` callbacks | ||
| // bind T from either arm. | ||
| interface Reply { | ||
| code: number; | ||
| text: string; | ||
| } | ||
| type Mapper<T> = (reply: Reply) => T | Promise<T>; | ||
|
|
||
| class Store { | ||
| loads = 0; | ||
| async load<T>(key: string, map: Mapper<T>): Promise<T>; | ||
| async load(key: string): Promise<Reply>; | ||
| async load<T>(key: string, map?: Mapper<T>): Promise<T | Reply> { | ||
| this.loads++; | ||
| const reply: Reply = { code: 200, text: key }; | ||
| if (map === undefined) return reply; | ||
| return await map(reply); | ||
| } | ||
| } | ||
|
|
||
| interface Services { | ||
| store: Pick<Store, "load">; | ||
| } | ||
|
|
||
| function pick<T>(items: T[], index: number): T; | ||
| function pick<T>(items: T[]): T; | ||
| function pick<T>(items: T[], index = 0): T { | ||
| return items[index]!; | ||
| } | ||
|
|
||
| async function settle<T>(reply: Reply, map?: Mapper<T>): Promise<T | Reply> { | ||
| return map ? await map(reply) : reply; | ||
| } | ||
|
|
||
| async function main(): Promise<void> { | ||
| const store = new Store(); | ||
| const plain = await store.load("alpha"); | ||
| console.log(plain.code, plain.text); | ||
| const length = await store.load("beta", (reply) => reply.text.length); | ||
| console.log(length * 2); | ||
| const services: Services = { store }; | ||
| const viaView = await services.store.load("gamma"); | ||
| const upper = await services.store.load("delta", async (reply) => reply.text.toUpperCase()); | ||
| console.log(viaView.text, upper, store.loads); | ||
| console.log(pick(["x", "y"]), pick([1, 2, 3], 2)); | ||
| console.log(JSON.stringify(await settle<Reply>({ code: 1, text: "s" }))); | ||
| console.log(await settle({ code: 2, text: "tt" }, (reply) => reply.code + reply.text.length)); | ||
| } | ||
|
|
||
| main(); |
8 changes: 4 additions & 4 deletions
8
tests/diagnostics/overload-fences.ts → tests/corpus/6496-overload-signatures.ts
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| // Bindings of generic overload calls: explicit type arguments name the | ||
| // overload's type parameters, which can be declared in another order than | ||
| // the implementation's, and keyof-constrained parameters bound to | ||
| // different literals through an overload read their own fields. | ||
| function tag<B, A>(value: A, label: B): string; | ||
| function tag<A, B>(value: A, label?: B): string { | ||
| return `${typeof value}:${typeof label}:${String(value)}${String(label)}`; | ||
| } | ||
| console.log(tag<string, number>(5, "x")); | ||
| console.log(tag<boolean, string>("y", true)); | ||
|
|
||
| class Labels { | ||
| prefix = "#"; | ||
| render<B, A>(value: A, label: B): string; | ||
| render<A, B>(value: A, label?: B): string { | ||
| return `${this.prefix}${typeof value}/${typeof label}`; | ||
| } | ||
| } | ||
| console.log(new Labels().render<number, string>("z", 7)); | ||
|
|
||
| interface Row { | ||
| a: number; | ||
| b: number; | ||
| name: string; | ||
| } | ||
| function read<T, K extends keyof T>(row: T, key: K): T[K]; | ||
| function read<T, K extends keyof T>(row: T, key: K, fallback: T[K]): T[K]; | ||
| function read<T, K extends keyof T>(row: T, key: K, fallback?: T[K]): T[K] { | ||
| const value = row[key]; | ||
| return value === undefined ? fallback! : value; | ||
| } | ||
| const row: Row = { a: 1, b: 2, name: "r" }; | ||
| console.log(read(row, "a"), read(row, "b"), read(row, "name")); | ||
| console.log(read(row, "b", 0) + read(row, "a", 0)); |
Oops, something went wrong.
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.