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

Handle silentNeverType in iteration-related functions #61317

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
7 changes: 7 additions & 0 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2175,6 +2175,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
};

var anyIterationTypes = createIterationTypes(anyType, anyType, anyType);
var silentNeverIterationTypes = createIterationTypes(silentNeverType, silentNeverType, silentNeverType);

var asyncIterationTypesResolver: IterationTypesResolver = {
iterableCacheKey: "iterationTypesOfAsyncIterable",
Expand Down Expand Up @@ -38571,6 +38572,9 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
}

function getYieldedTypeOfYieldExpression(node: YieldExpression, expressionType: Type, sentType: Type, isAsync: boolean): Type | undefined {
if (expressionType === silentNeverType) {
return silentNeverType;
}
const errorNode = node.expression || node;
// A `yield*` expression effectively yields everything that its operand yields
const yieldedType = node.asteriskToken ? checkIteratedTypeOrElementType(isAsync ? IterationUse.AsyncYieldStar : IterationUse.YieldStar, expressionType, sentType, errorNode) : expressionType;
Expand Down Expand Up @@ -45072,6 +45076,9 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
* the `[Symbol.asyncIterator]()` method first, and then the `[Symbol.iterator]()` method.
*/
function getIterationTypesOfIterable(type: Type, use: IterationUse, errorNode: Node | undefined) {
if (type === silentNeverType) {
return silentNeverIterationTypes;
}
if (isTypeAny(type)) {
return anyIterationTypes;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
genericCallAtYieldExpressionInGenericCall1.ts(26,25): error TS2488: Type '() => T' must have a '[Symbol.iterator]()' method that returns an iterator.


==== genericCallAtYieldExpressionInGenericCall1.ts (1 errors) ====
declare const inner: {
<A>(value: A): {
(): A;
[Symbol.iterator](): {
next(...args: ReadonlyArray<any>): IteratorResult<any, A>;
};
};
};

declare function outer<A>(body: (value: A) => Generator<any, any, any>): void;

outer(function* <T>(value: T) {
const result = yield* inner(value); // ok
});

outer(function* <T>(value: T) {
const x = inner(value);
const result = yield* x; // ok
});

declare const inner2: {
<A>(value: A): () => A;
};

outer(function* <T>(value: T) {
const result = yield* inner2(value); // error
~~~~~~~~~~~~~
!!! error TS2488: Type '() => T' must have a '[Symbol.iterator]()' method that returns an iterator.
});

Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
//// [tests/cases/compiler/genericCallAtYieldExpressionInGenericCall1.ts] ////

=== genericCallAtYieldExpressionInGenericCall1.ts ===
declare const inner: {
>inner : Symbol(inner, Decl(genericCallAtYieldExpressionInGenericCall1.ts, 0, 13))

<A>(value: A): {
>A : Symbol(A, Decl(genericCallAtYieldExpressionInGenericCall1.ts, 1, 3))
>value : Symbol(value, Decl(genericCallAtYieldExpressionInGenericCall1.ts, 1, 6))
>A : Symbol(A, Decl(genericCallAtYieldExpressionInGenericCall1.ts, 1, 3))

(): A;
>A : Symbol(A, Decl(genericCallAtYieldExpressionInGenericCall1.ts, 1, 3))

[Symbol.iterator](): {
>[Symbol.iterator] : Symbol([Symbol.iterator], Decl(genericCallAtYieldExpressionInGenericCall1.ts, 2, 10))
>Symbol.iterator : Symbol(SymbolConstructor.iterator, Decl(lib.es2015.iterable.d.ts, --, --))
>Symbol : Symbol(Symbol, Decl(lib.es5.d.ts, --, --), Decl(lib.es2015.symbol.d.ts, --, --), Decl(lib.es2015.symbol.wellknown.d.ts, --, --), Decl(lib.es2019.symbol.d.ts, --, --))
>iterator : Symbol(SymbolConstructor.iterator, Decl(lib.es2015.iterable.d.ts, --, --))

next(...args: ReadonlyArray<any>): IteratorResult<any, A>;
>next : Symbol(next, Decl(genericCallAtYieldExpressionInGenericCall1.ts, 3, 26))
>args : Symbol(args, Decl(genericCallAtYieldExpressionInGenericCall1.ts, 4, 11))
>ReadonlyArray : Symbol(ReadonlyArray, Decl(lib.es5.d.ts, --, --), Decl(lib.es2015.core.d.ts, --, --), Decl(lib.es2015.iterable.d.ts, --, --), Decl(lib.es2015.symbol.wellknown.d.ts, --, --), Decl(lib.es2016.array.include.d.ts, --, --) ... and 3 more)
>IteratorResult : Symbol(IteratorResult, Decl(lib.es2015.iterable.d.ts, --, --))
>A : Symbol(A, Decl(genericCallAtYieldExpressionInGenericCall1.ts, 1, 3))

};
};
};

declare function outer<A>(body: (value: A) => Generator<any, any, any>): void;
>outer : Symbol(outer, Decl(genericCallAtYieldExpressionInGenericCall1.ts, 7, 2))
>A : Symbol(A, Decl(genericCallAtYieldExpressionInGenericCall1.ts, 9, 23))
>body : Symbol(body, Decl(genericCallAtYieldExpressionInGenericCall1.ts, 9, 26))
>value : Symbol(value, Decl(genericCallAtYieldExpressionInGenericCall1.ts, 9, 33))
>A : Symbol(A, Decl(genericCallAtYieldExpressionInGenericCall1.ts, 9, 23))
>Generator : Symbol(Generator, Decl(lib.es2015.generator.d.ts, --, --))

outer(function* <T>(value: T) {
>outer : Symbol(outer, Decl(genericCallAtYieldExpressionInGenericCall1.ts, 7, 2))
>T : Symbol(T, Decl(genericCallAtYieldExpressionInGenericCall1.ts, 11, 17))
>value : Symbol(value, Decl(genericCallAtYieldExpressionInGenericCall1.ts, 11, 20))
>T : Symbol(T, Decl(genericCallAtYieldExpressionInGenericCall1.ts, 11, 17))

const result = yield* inner(value); // ok
>result : Symbol(result, Decl(genericCallAtYieldExpressionInGenericCall1.ts, 12, 7))
>inner : Symbol(inner, Decl(genericCallAtYieldExpressionInGenericCall1.ts, 0, 13))
>value : Symbol(value, Decl(genericCallAtYieldExpressionInGenericCall1.ts, 11, 20))

});

outer(function* <T>(value: T) {
>outer : Symbol(outer, Decl(genericCallAtYieldExpressionInGenericCall1.ts, 7, 2))
>T : Symbol(T, Decl(genericCallAtYieldExpressionInGenericCall1.ts, 15, 17))
>value : Symbol(value, Decl(genericCallAtYieldExpressionInGenericCall1.ts, 15, 20))
>T : Symbol(T, Decl(genericCallAtYieldExpressionInGenericCall1.ts, 15, 17))

const x = inner(value);
>x : Symbol(x, Decl(genericCallAtYieldExpressionInGenericCall1.ts, 16, 7))
>inner : Symbol(inner, Decl(genericCallAtYieldExpressionInGenericCall1.ts, 0, 13))
>value : Symbol(value, Decl(genericCallAtYieldExpressionInGenericCall1.ts, 15, 20))

const result = yield* x; // ok
>result : Symbol(result, Decl(genericCallAtYieldExpressionInGenericCall1.ts, 17, 7))
>x : Symbol(x, Decl(genericCallAtYieldExpressionInGenericCall1.ts, 16, 7))

});

declare const inner2: {
>inner2 : Symbol(inner2, Decl(genericCallAtYieldExpressionInGenericCall1.ts, 20, 13))

<A>(value: A): () => A;
>A : Symbol(A, Decl(genericCallAtYieldExpressionInGenericCall1.ts, 21, 3))
>value : Symbol(value, Decl(genericCallAtYieldExpressionInGenericCall1.ts, 21, 6))
>A : Symbol(A, Decl(genericCallAtYieldExpressionInGenericCall1.ts, 21, 3))
>A : Symbol(A, Decl(genericCallAtYieldExpressionInGenericCall1.ts, 21, 3))

};

outer(function* <T>(value: T) {
>outer : Symbol(outer, Decl(genericCallAtYieldExpressionInGenericCall1.ts, 7, 2))
>T : Symbol(T, Decl(genericCallAtYieldExpressionInGenericCall1.ts, 24, 17))
>value : Symbol(value, Decl(genericCallAtYieldExpressionInGenericCall1.ts, 24, 20))
>T : Symbol(T, Decl(genericCallAtYieldExpressionInGenericCall1.ts, 24, 17))

const result = yield* inner2(value); // error
>result : Symbol(result, Decl(genericCallAtYieldExpressionInGenericCall1.ts, 25, 7))
>inner2 : Symbol(inner2, Decl(genericCallAtYieldExpressionInGenericCall1.ts, 20, 13))
>value : Symbol(value, Decl(genericCallAtYieldExpressionInGenericCall1.ts, 24, 20))

});

Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
//// [tests/cases/compiler/genericCallAtYieldExpressionInGenericCall1.ts] ////

=== Performance Stats ===
Type Count: 1,000
Instantiation count: 2,500

=== genericCallAtYieldExpressionInGenericCall1.ts ===
declare const inner: {
>inner : <A>(value: A) => { (): A; [Symbol.iterator](): { next(...args: ReadonlyArray<any>): IteratorResult<any, A>; }; }
> : ^ ^^ ^^ ^^^^^

<A>(value: A): {
>value : A
> : ^

(): A;
[Symbol.iterator](): {
>[Symbol.iterator] : () => { next(...args: ReadonlyArray<any>): IteratorResult<any, A>; }
> : ^^^^^^
>Symbol.iterator : unique symbol
> : ^^^^^^^^^^^^^
>Symbol : SymbolConstructor
> : ^^^^^^^^^^^^^^^^^
>iterator : unique symbol
> : ^^^^^^^^^^^^^

next(...args: ReadonlyArray<any>): IteratorResult<any, A>;
>next : (...args: ReadonlyArray<any>) => IteratorResult<any, A>
> : ^^^^ ^^ ^^^^^
>args : readonly any[]
> : ^^^^^^^^^^^^^^

};
};
};

declare function outer<A>(body: (value: A) => Generator<any, any, any>): void;
>outer : <A>(body: (value: A) => Generator<any, any, any>) => void
> : ^ ^^ ^^ ^^^^^
>body : (value: A) => Generator<any, any, any>
> : ^ ^^ ^^^^^
>value : A
> : ^

outer(function* <T>(value: T) {
>outer(function* <T>(value: T) { const result = yield* inner(value); // ok}) : void
> : ^^^^
>outer : <A>(body: (value: A) => Generator<any, any, any>) => void
> : ^ ^^ ^^ ^^^^^
>function* <T>(value: T) { const result = yield* inner(value); // ok} : <T>(value: T) => Generator<never, void, never>
> : ^ ^^ ^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
>value : T
> : ^

const result = yield* inner(value); // ok
>result : T
> : ^
>yield* inner(value) : T
> : ^
>inner(value) : { (): T; [Symbol.iterator](): { next(...args: ReadonlyArray<any>): IteratorResult<any, T>; }; }
> : ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
>inner : <A>(value: A) => { (): A; [Symbol.iterator](): { next(...args: ReadonlyArray<any>): IteratorResult<any, A>; }; }
> : ^ ^^ ^^ ^^^^^
>value : T
> : ^

});

outer(function* <T>(value: T) {
>outer(function* <T>(value: T) { const x = inner(value); const result = yield* x; // ok}) : void
> : ^^^^
>outer : <A>(body: (value: A) => Generator<any, any, any>) => void
> : ^ ^^ ^^ ^^^^^
>function* <T>(value: T) { const x = inner(value); const result = yield* x; // ok} : <T>(value: T) => Generator<any, void, any>
> : ^ ^^ ^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
>value : T
> : ^

const x = inner(value);
>x : { (): T; [Symbol.iterator](): { next(...args: ReadonlyArray<any>): IteratorResult<any, T>; }; }
> : ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
>inner(value) : { (): T; [Symbol.iterator](): { next(...args: ReadonlyArray<any>): IteratorResult<any, T>; }; }
> : ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
>inner : <A>(value: A) => { (): A; [Symbol.iterator](): { next(...args: ReadonlyArray<any>): IteratorResult<any, A>; }; }
> : ^ ^^ ^^ ^^^^^
>value : T
> : ^

const result = yield* x; // ok
>result : T
> : ^
>yield* x : T
> : ^
>x : { (): T; [Symbol.iterator](): { next(...args: ReadonlyArray<any>): IteratorResult<any, T>; }; }
> : ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

});

declare const inner2: {
>inner2 : <A>(value: A) => () => A
> : ^ ^^ ^^ ^^^^^

<A>(value: A): () => A;
>value : A
> : ^

};

outer(function* <T>(value: T) {
>outer(function* <T>(value: T) { const result = yield* inner2(value); // error}) : void
> : ^^^^
>outer : <A>(body: (value: A) => Generator<any, any, any>) => void
> : ^ ^^ ^^ ^^^^^
>function* <T>(value: T) { const result = yield* inner2(value); // error} : <T>(value: T) => Generator<never, void, never>
> : ^ ^^ ^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
>value : T
> : ^

const result = yield* inner2(value); // error
>result : any
> : ^^^
>yield* inner2(value) : any
> : ^^^
>inner2(value) : () => T
> : ^^^^^^^
>inner2 : <A>(value: A) => () => A
> : ^ ^^ ^^ ^^^^^
>value : T
> : ^

});

Loading