forked from microsoft/TypeScript
-
Notifications
You must be signed in to change notification settings - Fork 14
le/symbols as weak map keys #76
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
acutmore
merged 26 commits into
bloomberg:le/symbol-as-weak-keys
from
leoelm:le/symbols-as-weak-map-keys
May 4, 2023
Merged
Changes from 21 commits
Commits
Show all changes
26 commits
Select commit
Hold shift + click to select a range
3bac33d
Update 2021 weakref types
leoelm 5871e83
Add symbol to WeakRef and FinalizationRegistry
leoelm c0161d9
Update WeakSet keys
leoelm 0596a4c
Add WeakMap symbol support
leoelm 776c6a7
Resolve compilation and testing issues
leoelm 4b02a2c
Add some test changes
leoelm 0e809bf
Add testing for WeakRef and FinalizationRegistry
leoelm 2241306
Undo lib changes
leoelm 18ec290
Undo lib changes
leoelm 0198657
Address PR comments
leoelm a103a0b
Update 2021 weakref types
leoelm e898ad0
Add symbol to WeakRef and FinalizationRegistry
leoelm 8b90d0a
Update WeakSet keys
leoelm 485aea5
Add WeakMap symbol support
leoelm 6350617
Resolve compilation and testing issues
leoelm b1a8c37
Add some test changes
leoelm 8413425
Add testing for WeakRef and FinalizationRegistry
leoelm f8470f1
Undo lib changes
leoelm ff83b5c
Undo lib changes
leoelm c3275ca
Address PR comments
leoelm 0809ac2
Merge branch 'le/symbols-as-weak-map-keys' of github.com:leoelm/TypeS…
leoelm 8c2fd56
Address PR comments
leoelm 2eb0354
Add baselines
leoelm 7563f5e
Address PR comments
leoelm 71b47cb
Address PR comments
leoelm c966981
Merge branch 'le/symbol-as-weak-keys' into le/symbols-as-weak-map-keys
leoelm 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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| interface WeakKeyTypes { | ||
| symbol: symbol; | ||
| } | ||
|
|
||
| interface WeakSet<T extends WeakKey> {} | ||
|
|
||
| interface WeakSetConstructor { | ||
| new <T extends WeakKey = WeakKey>(values?: readonly T[] | null): WeakSet<T>; | ||
| } | ||
|
|
||
| interface WeakMap<K extends WeakKey, V> {} | ||
|
|
||
| interface WeakMapConstructor { | ||
| new <K extends WeakKey = WeakKey, V = any>(entries?: readonly [K, V][] | null): WeakMap<K, V>; | ||
| readonly prototype: WeakMap<WeakKey, any>; | ||
| } |
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 |
|---|---|---|
| @@ -1,2 +1,4 @@ | ||
| /// <reference lib="es2022" /> | ||
| /// <reference lib="es2023.array" /> | ||
| /// <reference lib="es2023.collection" /> | ||
| /// <reference lib="es2023.weakref" /> |
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,49 @@ | ||
| interface WeakKeyTypes { | ||
| symbol: symbol; | ||
| } | ||
|
|
||
| interface FinalizationRegistry<T> { | ||
| readonly [Symbol.toStringTag]: "FinalizationRegistry"; | ||
|
|
||
| /** | ||
| * Registers an object with the registry. | ||
| * @param target The target object to register. | ||
| * @param heldValue The value to pass to the finalizer for this object. This cannot be the | ||
| * target object. | ||
| * @param unregisterToken The token to pass to the unregister method to unregister the target | ||
| * object. If provided (and not undefined), this must be an object. If not provided, the target | ||
| * cannot be unregistered. | ||
| */ | ||
| registerSymbol(target: symbol, heldValue: T, unregisterToken?: symbol): void; | ||
acutmore marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| /** | ||
| * Unregisters an object from the registry. | ||
| * @param unregisterToken The token that was used as the unregisterToken argument when calling | ||
| * register to register the target object. | ||
| */ | ||
| unregisterSymbol(unregisterToken: symbol): void; | ||
| } | ||
|
|
||
| interface FinalizationRegistryConstructor { | ||
| readonly prototype: FinalizationRegistry<any>; | ||
|
|
||
| /** | ||
| * Creates a finalization registry with an associated cleanup callback | ||
| * @param cleanupCallback The callback to call after an object in the registry has been reclaimed. | ||
| */ | ||
| new<T>(cleanupCallback: (heldValue: T) => void): FinalizationRegistry<T>; | ||
| } | ||
|
|
||
| declare var FinalizationRegistry: FinalizationRegistryConstructor; | ||
|
|
||
| interface WeakRef<T extends WeakKey> { | ||
| /** | ||
| * Returns the WeakRef instance's target object, or undefined if the target object has been | ||
| * reclaimed. | ||
| */ | ||
| deref(): T | undefined; | ||
| } | ||
|
|
||
| interface WeakRefConstructor { | ||
| new<T extends WeakKey>(target: T): WeakRef<T>; | ||
| } | ||
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,37 @@ | ||
| //// [acceptSymbolAsWeakType.ts] | ||
| const s: symbol = Symbol('s'); | ||
|
|
||
| const ws = new WeakSet([s]); | ||
| ws.add(s); | ||
| ws.has(s); | ||
| ws.delete(s); | ||
|
|
||
| const wm = new WeakMap([[s, false]]); | ||
| wm.set(s, true); | ||
| wm.has(s); | ||
| wm.get(s); | ||
| wm.delete(s); | ||
|
|
||
| const wr = new WeakRef(s); | ||
| wr.deref(); | ||
|
|
||
| const f = new FinalizationRegistry<symbol>(() => {}); | ||
| f.registerSymbol(s, null); | ||
| f.unregisterSymbol(s); | ||
|
|
||
| //// [acceptSymbolAsWeakType.js] | ||
| const s = Symbol('s'); | ||
| const ws = new WeakSet([s]); | ||
| ws.add(s); | ||
| ws.has(s); | ||
| ws.delete(s); | ||
| const wm = new WeakMap([[s, false]]); | ||
| wm.set(s, true); | ||
| wm.has(s); | ||
| wm.get(s); | ||
| wm.delete(s); | ||
| const wr = new WeakRef(s); | ||
| wr.deref(); | ||
| const f = new FinalizationRegistry(() => { }); | ||
| f.registerSymbol(s, null); | ||
| f.unregisterSymbol(s); |
99 changes: 99 additions & 0 deletions
99
tests/baselines/reference/dissallowSymbolAsWeakType.errors.txt
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,99 @@ | ||
| tests/cases/compiler/dissallowSymbolAsWeakType.ts(3,12): error TS2769: No overload matches this call. | ||
| Overload 1 of 2, '(iterable: Iterable<object>): WeakSet<object>', gave the following error. | ||
| Argument of type 'symbol[]' is not assignable to parameter of type 'Iterable<object>'. | ||
| The types returned by '[Symbol.iterator]().next(...)' are incompatible between these types. | ||
| Type 'IteratorResult<symbol, any>' is not assignable to type 'IteratorResult<object, any>'. | ||
| Type 'IteratorYieldResult<symbol>' is not assignable to type 'IteratorResult<object, any>'. | ||
| Type 'IteratorYieldResult<symbol>' is not assignable to type 'IteratorYieldResult<object>'. | ||
| Type 'symbol' is not assignable to type 'object'. | ||
| Overload 2 of 2, '(values?: readonly object[]): WeakSet<object>', gave the following error. | ||
| Type 'symbol' is not assignable to type 'object'. | ||
| tests/cases/compiler/dissallowSymbolAsWeakType.ts(4,8): error TS2345: Argument of type 'symbol' is not assignable to parameter of type 'object'. | ||
| tests/cases/compiler/dissallowSymbolAsWeakType.ts(5,8): error TS2345: Argument of type 'symbol' is not assignable to parameter of type 'object'. | ||
| tests/cases/compiler/dissallowSymbolAsWeakType.ts(6,11): error TS2345: Argument of type 'symbol' is not assignable to parameter of type 'object'. | ||
| tests/cases/compiler/dissallowSymbolAsWeakType.ts(8,12): error TS2769: No overload matches this call. | ||
| Overload 1 of 2, '(iterable: Iterable<readonly [object, boolean]>): WeakMap<object, boolean>', gave the following error. | ||
| Argument of type '[symbol, false][]' is not assignable to parameter of type 'Iterable<readonly [object, boolean]>'. | ||
| The types returned by '[Symbol.iterator]().next(...)' are incompatible between these types. | ||
| Type 'IteratorResult<[symbol, false], any>' is not assignable to type 'IteratorResult<readonly [object, boolean], any>'. | ||
| Type 'IteratorYieldResult<[symbol, false]>' is not assignable to type 'IteratorResult<readonly [object, boolean], any>'. | ||
| Type 'IteratorYieldResult<[symbol, false]>' is not assignable to type 'IteratorYieldResult<readonly [object, boolean]>'. | ||
| Type '[symbol, false]' is not assignable to type 'readonly [object, boolean]'. | ||
| Type at position 0 in source is not compatible with type at position 0 in target. | ||
| Type 'symbol' is not assignable to type 'object'. | ||
| Overload 2 of 2, '(entries?: readonly [object, boolean][]): WeakMap<object, boolean>', gave the following error. | ||
| Type 'symbol' is not assignable to type 'object'. | ||
| tests/cases/compiler/dissallowSymbolAsWeakType.ts(9,8): error TS2345: Argument of type 'symbol' is not assignable to parameter of type 'object'. | ||
| tests/cases/compiler/dissallowSymbolAsWeakType.ts(10,8): error TS2345: Argument of type 'symbol' is not assignable to parameter of type 'object'. | ||
| tests/cases/compiler/dissallowSymbolAsWeakType.ts(11,8): error TS2345: Argument of type 'symbol' is not assignable to parameter of type 'object'. | ||
| tests/cases/compiler/dissallowSymbolAsWeakType.ts(12,11): error TS2345: Argument of type 'symbol' is not assignable to parameter of type 'object'. | ||
| tests/cases/compiler/dissallowSymbolAsWeakType.ts(14,24): error TS2345: Argument of type 'symbol' is not assignable to parameter of type 'object'. | ||
| tests/cases/compiler/dissallowSymbolAsWeakType.ts(18,12): error TS2345: Argument of type 'symbol' is not assignable to parameter of type 'object'. | ||
| tests/cases/compiler/dissallowSymbolAsWeakType.ts(19,14): error TS2345: Argument of type 'symbol' is not assignable to parameter of type 'object'. | ||
|
|
||
|
|
||
| ==== tests/cases/compiler/dissallowSymbolAsWeakType.ts (12 errors) ==== | ||
| const s: symbol = Symbol('s'); | ||
|
|
||
| const ws = new WeakSet([s]); | ||
| ~~~~~~~~~~~~~~~~ | ||
| !!! error TS2769: No overload matches this call. | ||
| !!! error TS2769: Overload 1 of 2, '(iterable: Iterable<object>): WeakSet<object>', gave the following error. | ||
| !!! error TS2769: Argument of type 'symbol[]' is not assignable to parameter of type 'Iterable<object>'. | ||
| !!! error TS2769: The types returned by '[Symbol.iterator]().next(...)' are incompatible between these types. | ||
| !!! error TS2769: Type 'IteratorResult<symbol, any>' is not assignable to type 'IteratorResult<object, any>'. | ||
| !!! error TS2769: Type 'IteratorYieldResult<symbol>' is not assignable to type 'IteratorResult<object, any>'. | ||
| !!! error TS2769: Type 'IteratorYieldResult<symbol>' is not assignable to type 'IteratorYieldResult<object>'. | ||
| !!! error TS2769: Type 'symbol' is not assignable to type 'object'. | ||
| !!! error TS2769: Overload 2 of 2, '(values?: readonly object[]): WeakSet<object>', gave the following error. | ||
| !!! error TS2769: Type 'symbol' is not assignable to type 'object'. | ||
| ws.add(s); | ||
| ~ | ||
| !!! error TS2345: Argument of type 'symbol' is not assignable to parameter of type 'object'. | ||
| ws.has(s); | ||
| ~ | ||
| !!! error TS2345: Argument of type 'symbol' is not assignable to parameter of type 'object'. | ||
| ws.delete(s); | ||
| ~ | ||
| !!! error TS2345: Argument of type 'symbol' is not assignable to parameter of type 'object'. | ||
|
|
||
| const wm = new WeakMap([[s, false]]); | ||
| ~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
| !!! error TS2769: No overload matches this call. | ||
| !!! error TS2769: Overload 1 of 2, '(iterable: Iterable<readonly [object, boolean]>): WeakMap<object, boolean>', gave the following error. | ||
| !!! error TS2769: Argument of type '[symbol, false][]' is not assignable to parameter of type 'Iterable<readonly [object, boolean]>'. | ||
| !!! error TS2769: The types returned by '[Symbol.iterator]().next(...)' are incompatible between these types. | ||
| !!! error TS2769: Type 'IteratorResult<[symbol, false], any>' is not assignable to type 'IteratorResult<readonly [object, boolean], any>'. | ||
| !!! error TS2769: Type 'IteratorYieldResult<[symbol, false]>' is not assignable to type 'IteratorResult<readonly [object, boolean], any>'. | ||
| !!! error TS2769: Type 'IteratorYieldResult<[symbol, false]>' is not assignable to type 'IteratorYieldResult<readonly [object, boolean]>'. | ||
| !!! error TS2769: Type '[symbol, false]' is not assignable to type 'readonly [object, boolean]'. | ||
| !!! error TS2769: Type at position 0 in source is not compatible with type at position 0 in target. | ||
| !!! error TS2769: Type 'symbol' is not assignable to type 'object'. | ||
| !!! error TS2769: Overload 2 of 2, '(entries?: readonly [object, boolean][]): WeakMap<object, boolean>', gave the following error. | ||
| !!! error TS2769: Type 'symbol' is not assignable to type 'object'. | ||
| wm.set(s, true); | ||
| ~ | ||
| !!! error TS2345: Argument of type 'symbol' is not assignable to parameter of type 'object'. | ||
| wm.has(s); | ||
| ~ | ||
| !!! error TS2345: Argument of type 'symbol' is not assignable to parameter of type 'object'. | ||
| wm.get(s); | ||
| ~ | ||
| !!! error TS2345: Argument of type 'symbol' is not assignable to parameter of type 'object'. | ||
| wm.delete(s); | ||
| ~ | ||
| !!! error TS2345: Argument of type 'symbol' is not assignable to parameter of type 'object'. | ||
|
|
||
| const wr = new WeakRef(s); | ||
| ~ | ||
| !!! error TS2345: Argument of type 'symbol' is not assignable to parameter of type 'object'. | ||
| wr.deref(); | ||
|
|
||
| const f = new FinalizationRegistry(() => {}); | ||
| f.register(s, null); | ||
| ~ | ||
| !!! error TS2345: Argument of type 'symbol' is not assignable to parameter of type 'object'. | ||
| f.unregister(s); | ||
| ~ | ||
| !!! error TS2345: Argument of type 'symbol' is not assignable to parameter of type 'object'. | ||
|
|
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,38 @@ | ||
| //// [dissallowSymbolAsWeakType.ts] | ||
| const s: symbol = Symbol('s'); | ||
|
|
||
| const ws = new WeakSet([s]); | ||
| ws.add(s); | ||
| ws.has(s); | ||
| ws.delete(s); | ||
|
|
||
| const wm = new WeakMap([[s, false]]); | ||
| wm.set(s, true); | ||
| wm.has(s); | ||
| wm.get(s); | ||
| wm.delete(s); | ||
|
|
||
| const wr = new WeakRef(s); | ||
| wr.deref(); | ||
|
|
||
| const f = new FinalizationRegistry(() => {}); | ||
| f.register(s, null); | ||
| f.unregister(s); | ||
|
|
||
|
|
||
| //// [dissallowSymbolAsWeakType.js] | ||
| const s = Symbol('s'); | ||
| const ws = new WeakSet([s]); | ||
| ws.add(s); | ||
| ws.has(s); | ||
| ws.delete(s); | ||
| const wm = new WeakMap([[s, false]]); | ||
| wm.set(s, true); | ||
| wm.has(s); | ||
| wm.get(s); | ||
| wm.delete(s); | ||
| const wr = new WeakRef(s); | ||
| wr.deref(); | ||
| const f = new FinalizationRegistry(() => { }); | ||
| f.register(s, null); | ||
| f.unregister(s); |
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,22 @@ | ||
| // @lib: esnext | ||
| // @target: esnext | ||
acutmore marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| const s: symbol = Symbol('s'); | ||
|
|
||
| const ws = new WeakSet([s]); | ||
| ws.add(s); | ||
| ws.has(s); | ||
| ws.delete(s); | ||
|
|
||
| const wm = new WeakMap([[s, false]]); | ||
| wm.set(s, true); | ||
| wm.has(s); | ||
| wm.get(s); | ||
| wm.delete(s); | ||
|
|
||
| const wr = new WeakRef(s); | ||
| wr.deref(); | ||
|
|
||
| const f = new FinalizationRegistry<symbol>(() => {}); | ||
| f.registerSymbol(s, null); | ||
| f.unregisterSymbol(s); | ||
acutmore marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
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.