Skip to content
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

Add TReturn/TNext to Iterable et al #58243

Merged
merged 29 commits into from
Jul 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
ef2e1d9
Add TReturn/TNext to Iterable et al
rbuckton Apr 18, 2024
1a64ac9
Fix assignability checks, update baselines
rbuckton Apr 18, 2024
7aa2281
Fix lint
rbuckton Apr 18, 2024
be600a8
Revert incorrect lint fix
rbuckton Apr 18, 2024
afbfa69
Merge branch 'main' into iterator-default-return
rbuckton Apr 18, 2024
8182bfc
Fix failing tests
rbuckton Apr 18, 2024
ad451d1
Elide type arguments that match defaults for inferred Iterable et al
rbuckton Apr 19, 2024
ec82647
Set TReturn default to 'any', provide explicit type arguments where n…
rbuckton Apr 19, 2024
5cb0e10
Fix broken fourslash test
rbuckton Apr 19, 2024
52ed319
Use intrinsic type for builtin iterator return, PR feedback
rbuckton May 6, 2024
bb513be
Use BuiltinIteratorReturn in es2020.bigint and es2022.intl
rbuckton May 7, 2024
2d58aa5
Use BuiltinIteratorReturn in es2020.string and es2020.symbol.wellknown
rbuckton May 7, 2024
0bfb236
Update baselines
rbuckton May 7, 2024
e98303f
Use new --strictBuiltinIteratorReturn flag
rbuckton May 17, 2024
3a59acb
Merge branch 'main' into iterator-default-return
rbuckton May 17, 2024
8578a4d
Update baselines
rbuckton May 17, 2024
51151ee
Change default for 'TNext' to 'any'
rbuckton May 17, 2024
595707d
Change inferred return type of generators to 'undefined'
rbuckton May 20, 2024
2a4edbc
Revert and use 'void' for BuiltinIteratorReturn
rbuckton May 21, 2024
7d32efc
Merge branch 'main' into iterator-default-return
rbuckton May 23, 2024
db12e2f
Merge branch 'main' into iterator-default-return
rbuckton Jun 25, 2024
fe2ff63
Split the difference, only use 'undefined' for built-ins
rbuckton Jun 25, 2024
63bc5fb
Add test and address self-build issue
rbuckton Jun 26, 2024
eece783
Merge branch 'main' into iterator-default-return
rbuckton Jul 17, 2024
a0d5046
Merge branch 'main' into iterator-default-return
rbuckton Jul 17, 2024
2a6af13
Merge branch 'main' into iterator-default-return
rbuckton Jul 17, 2024
46eba67
Add isolatedDeclarations test
rbuckton Jul 18, 2024
b8f04f6
Fix typo in comment
rbuckton Jul 18, 2024
1a3669a
Move initialization of 'typeParameterCount'
rbuckton Jul 19, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
135 changes: 63 additions & 72 deletions src/compiler/checker.ts

Large diffs are not rendered by default.

10 changes: 10 additions & 0 deletions src/compiler/commandLineParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -915,6 +915,16 @@ const commandOptionsWithoutBuild: CommandLineOption[] = [
description: Diagnostics.Check_for_class_properties_that_are_declared_but_not_set_in_the_constructor,
defaultValueDescription: Diagnostics.false_unless_strict_is_set,
},
{
name: "strictBuiltinIteratorReturn",
type: "boolean",
affectsSemanticDiagnostics: true,
affectsBuildInfo: true,
strictFlag: true,
category: Diagnostics.Type_Checking,
description: Diagnostics.Built_in_iterators_are_instantiated_with_a_TReturn_type_of_undefined_instead_of_any,
defaultValueDescription: Diagnostics.false_unless_strict_is_set,
},
{
name: "noImplicitThis",
type: "boolean",
Expand Down
4 changes: 4 additions & 0 deletions src/compiler/diagnosticMessages.json
Original file line number Diff line number Diff line change
Expand Up @@ -6368,6 +6368,10 @@
"code": 6719
},

"Built-in iterators are instantiated with a 'TReturn' type of 'undefined' instead of 'any'.": {
"category": "Message",
"code": 6720
},
"Default catch clause variables as 'unknown' instead of 'any'.": {
"category": "Message",
"code": 6803
Expand Down
3 changes: 2 additions & 1 deletion src/compiler/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5237,7 +5237,7 @@ export interface TypeChecker {
/** @internal */ createPromiseType(type: Type): Type;
/** @internal */ getPromiseType(): Type;
/** @internal */ getPromiseLikeType(): Type;
/** @internal */ getAsyncIterableType(): Type | undefined;
/** @internal */ getAnyAsyncIterableType(): Type | undefined;

/**
* Returns true if the "source" type is assignable to the "target" type.
Expand Down Expand Up @@ -7407,6 +7407,7 @@ export interface CompilerOptions {
strictBindCallApply?: boolean; // Always combine with strict property
strictNullChecks?: boolean; // Always combine with strict property
strictPropertyInitialization?: boolean; // Always combine with strict property
strictBuiltinIteratorReturn?: boolean; // Always combine with strict property
stripInternal?: boolean;
/** @deprecated */
suppressExcessPropertyErrors?: boolean;
Expand Down
7 changes: 7 additions & 0 deletions src/compiler/utilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9006,6 +9006,12 @@ export const computedOptions = createComputedCompilerOptions({
return getStrictOptionValue(compilerOptions, "strictPropertyInitialization");
},
},
strictBuiltinIteratorReturn: {
dependencies: ["strict"],
computeValue: compilerOptions => {
return getStrictOptionValue(compilerOptions, "strictBuiltinIteratorReturn");
},
},
alwaysStrict: {
dependencies: ["strict"],
computeValue: compilerOptions => {
Expand Down Expand Up @@ -9093,6 +9099,7 @@ export type StrictOptionName =
| "strictFunctionTypes"
| "strictBindCallApply"
| "strictPropertyInitialization"
| "strictBuiltinIteratorReturn"
| "alwaysStrict"
| "useUnknownInCatchVariables";

Expand Down
3 changes: 3 additions & 0 deletions src/harness/collectionsImpl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ export class SortedMap<K, V> {
this._copyOnWrite = false;
}
}
return undefined;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

will all generators need to return undefined now? do you have an estimate of how much code that breaks?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

from reading type baselines, it looks like their return type changes to any instead of undefined

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is specifically because this is an implementation of the native Map<K, V> interface, which is now specified to return IterableIterator<T, BuiltinIteratorReturn> (which is IterableIterator<T, undefined> under the new flag).

}

public *values() {
Expand All @@ -154,6 +155,7 @@ export class SortedMap<K, V> {
this._copyOnWrite = false;
}
}
return undefined;
}

public *entries() {
Expand All @@ -179,6 +181,7 @@ export class SortedMap<K, V> {
this._copyOnWrite = false;
}
}
return undefined;
}

public [Symbol.iterator]() {
Expand Down
46 changes: 23 additions & 23 deletions src/lib/dom.iterable.d.ts
Original file line number Diff line number Diff line change
@@ -1,30 +1,30 @@
/// <reference lib="dom" />

interface DOMTokenList {
[Symbol.iterator](): IterableIterator<string>;
[Symbol.iterator](): IterableIterator<string, BuiltinIteratorReturn>;
}

interface Headers {
[Symbol.iterator](): IterableIterator<[string, string]>;
[Symbol.iterator](): IterableIterator<[string, string], BuiltinIteratorReturn>;
/**
* Returns an iterator allowing to go through all key/value pairs contained in this object.
*/
entries(): IterableIterator<[string, string]>;
entries(): IterableIterator<[string, string], BuiltinIteratorReturn>;
/**
* Returns an iterator allowing to go through all keys f the key/value pairs contained in this object.
*/
keys(): IterableIterator<string>;
keys(): IterableIterator<string, BuiltinIteratorReturn>;
/**
* Returns an iterator allowing to go through all values of the key/value pairs contained in this object.
*/
values(): IterableIterator<string>;
values(): IterableIterator<string, BuiltinIteratorReturn>;
}

interface NodeList {
/**
* Returns an array of key, value pairs for every entry in the list
*/
entries(): IterableIterator<[number, Node]>;
entries(): IterableIterator<[number, Node], BuiltinIteratorReturn>;
/**
* Performs the specified action for each node in an list.
* @param callbackfn A function that accepts up to three arguments. forEach calls the callbackfn function one time for each element in the list.
Expand All @@ -34,21 +34,21 @@ interface NodeList {
/**
* Returns an list of keys in the list
*/
keys(): IterableIterator<number>;
keys(): IterableIterator<number, BuiltinIteratorReturn>;

/**
* Returns an list of values in the list
*/
values(): IterableIterator<Node>;
values(): IterableIterator<Node, BuiltinIteratorReturn>;

[Symbol.iterator](): IterableIterator<Node>;
[Symbol.iterator](): IterableIterator<Node, BuiltinIteratorReturn>;
}

interface NodeListOf<TNode extends Node> {
/**
* Returns an array of key, value pairs for every entry in the list
*/
entries(): IterableIterator<[number, TNode]>;
entries(): IterableIterator<[number, TNode], BuiltinIteratorReturn>;

/**
* Performs the specified action for each node in an list.
Expand All @@ -59,55 +59,55 @@ interface NodeListOf<TNode extends Node> {
/**
* Returns an list of keys in the list
*/
keys(): IterableIterator<number>;
keys(): IterableIterator<number, BuiltinIteratorReturn>;
/**
* Returns an list of values in the list
*/
values(): IterableIterator<TNode>;
values(): IterableIterator<TNode, BuiltinIteratorReturn>;

[Symbol.iterator](): IterableIterator<TNode>;
[Symbol.iterator](): IterableIterator<TNode, BuiltinIteratorReturn>;
}

interface HTMLCollectionBase {
[Symbol.iterator](): IterableIterator<Element>;
[Symbol.iterator](): IterableIterator<Element, BuiltinIteratorReturn>;
}

interface HTMLCollectionOf<T extends Element> {
[Symbol.iterator](): IterableIterator<T>;
[Symbol.iterator](): IterableIterator<T, BuiltinIteratorReturn>;
}

interface FormData {
/**
* Returns an array of key, value pairs for every entry in the list
*/
entries(): IterableIterator<[string, string | File]>;
entries(): IterableIterator<[string, string | File], BuiltinIteratorReturn>;
/**
* Returns a list of keys in the list
*/
keys(): IterableIterator<string>;
keys(): IterableIterator<string, BuiltinIteratorReturn>;
/**
* Returns a list of values in the list
*/
values(): IterableIterator<string | File>;
values(): IterableIterator<string | File, BuiltinIteratorReturn>;

[Symbol.iterator](): IterableIterator<string | File>;
[Symbol.iterator](): IterableIterator<string | File, BuiltinIteratorReturn>;
}

interface URLSearchParams {
/**
* Returns an array of key, value pairs for every entry in the search params
*/
entries(): IterableIterator<[string, string]>;
entries(): IterableIterator<[string, string], BuiltinIteratorReturn>;
/**
* Returns a list of keys in the search params
*/
keys(): IterableIterator<string>;
keys(): IterableIterator<string, BuiltinIteratorReturn>;
/**
* Returns a list of values in the search params
*/
values(): IterableIterator<string>;
values(): IterableIterator<string, BuiltinIteratorReturn>;
/**
* iterate over key/value pairs
*/
[Symbol.iterator](): IterableIterator<[string, string]>;
[Symbol.iterator](): IterableIterator<[string, string], BuiltinIteratorReturn>;
}
2 changes: 1 addition & 1 deletion src/lib/es2015.generator.d.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/// <reference lib="es2015.iterable" />

interface Generator<T = unknown, TReturn = any, TNext = unknown> extends Iterator<T, TReturn, TNext> {
interface Generator<T = unknown, TReturn = any, TNext = any> extends Iterator<T, TReturn, TNext> {
// NOTE: 'next' is defined using a tuple to ensure we report the correct assignability errors in all places.
next(...args: [] | [TNext]): IteratorResult<T, TReturn>;
return(value: TReturn): IteratorResult<T, TReturn>;
Expand Down
Loading