diff --git a/tsc/internal/checker/checker.go b/tsc/internal/checker/checker.go index 14f8356152d83..5a08fc3d0f9a6 100644 --- a/tsc/internal/checker/checker.go +++ b/tsc/internal/checker/checker.go @@ -6497,18 +6497,37 @@ func (c *Checker) getIterationTypesOfIterable(t *Type, use IterationUse, errorNo } noCache = true } - result := c.getIterationTypesOfIterableWorker(t, use, errorNode) + // Keep the original use site while probing union constituents without iterable diagnostics. + getAsyncFromSyncIterationTypes := func(iterationTypes IterationTypes) IterationTypes { + if iterationTypes == (IterationTypes{c.anyType, c.anyType, c.anyType}) { + return iterationTypes + } + if errorNode != nil { + c.getGlobalAwaitedSymbol() + } + diagnostic := core.IfElse(use&IterationUseYieldStarFlag != 0, + diagnostics.Type_of_iterated_elements_of_a_yield_Asterisk_operand_must_either_be_a_valid_promise_or_must_not_contain_a_callable_then_member, + diagnostics.Type_of_await_operand_must_either_be_a_valid_promise_or_must_not_contain_a_callable_then_member) + yieldType := c.getAwaitedTypeEx(iterationTypes.yieldType, errorNode, diagnostic) + returnType := c.getAwaitedTypeEx(iterationTypes.returnType, errorNode, diagnostic) + // A cached fallback to any would hide the error at subsequent uses of this iterable. + if yieldType == nil || returnType == nil { + noCache = true + } + return IterationTypes{core.OrElse(yieldType, c.anyType), core.OrElse(returnType, c.anyType), iterationTypes.nextType} + } + result := c.getIterationTypesOfIterableWorker(t, use, errorNode, getAsyncFromSyncIterationTypes) if !noCache { c.iterationTypesCache[key] = result } return result } -func (c *Checker) getIterationTypesOfIterableWorker(t *Type, use IterationUse, errorNode *ast.Node) IterationTypes { +func (c *Checker) getIterationTypesOfIterableWorker(t *Type, use IterationUse, errorNode *ast.Node, getAsyncFromSyncIterationTypes func(IterationTypes) IterationTypes) IterationTypes { if t.flags&TypeFlagsUnion != 0 { allIterationTypes := make([]IterationTypes, 0, len(t.Types())) for _, constituent := range t.Types() { - iterationTypes := c.getIterationTypesOfIterableWorker(constituent, use, nil) + iterationTypes := c.getIterationTypesOfIterableWorker(constituent, use, nil, getAsyncFromSyncIterationTypes) if !iterationTypes.hasTypes() { if errorNode != nil { c.addDeferredDiagnostic(func() { @@ -6526,7 +6545,7 @@ func (c *Checker) getIterationTypesOfIterableWorker(t *Type, use IterationUse, e iterationTypes := c.getIterationTypesOfIterableFast(t, c.asyncIterationTypesResolver) if iterationTypes.hasTypes() { if use&IterationUseForOfFlag != 0 { - return c.getAsyncFromSyncIterationTypes(iterationTypes, errorNode) + return getAsyncFromSyncIterationTypes(iterationTypes) } return iterationTypes } @@ -6544,7 +6563,7 @@ func (c *Checker) getIterationTypesOfIterableWorker(t *Type, use IterationUse, e iterationTypes := c.getIterationTypesOfIterableFast(t, c.syncIterationTypesResolver) if iterationTypes.hasTypes() { if use&IterationUseAllowsAsyncIterablesFlag != 0 { - return c.getAsyncFromSyncIterationTypes(iterationTypes, errorNode) + return getAsyncFromSyncIterationTypes(iterationTypes) } return iterationTypes } @@ -6556,7 +6575,7 @@ func (c *Checker) getIterationTypesOfIterableWorker(t *Type, use IterationUse, e } } if use&IterationUseAllowsAsyncIterablesFlag != 0 { - return c.getAsyncFromSyncIterationTypes(iterationTypes, errorNode) + return getAsyncFromSyncIterationTypes(iterationTypes) } return iterationTypes } @@ -6653,22 +6672,6 @@ func (c *Checker) getIterationTypeUnion(iterationTypes []IterationTypes, f func( return c.getUnionType(types) } -func (c *Checker) getAsyncFromSyncIterationTypes(iterationTypes IterationTypes, errorNode *ast.Node) IterationTypes { - if !iterationTypes.hasTypes() || - iterationTypes.yieldType == c.anyType && iterationTypes.returnType == c.anyType && iterationTypes.nextType == c.anyType { - return iterationTypes - } - // if we're requesting diagnostics, report errors for a missing `Awaited`. - if errorNode != nil { - c.getGlobalAwaitedSymbol() - } - return IterationTypes{ - yieldType: core.OrElse(c.getAwaitedTypeEx(iterationTypes.yieldType, errorNode, nil), c.anyType), - returnType: core.OrElse(c.getAwaitedTypeEx(iterationTypes.returnType, errorNode, nil), c.anyType), - nextType: iterationTypes.nextType, - } -} - // Gets the *yield*, *return*, and *next* types of an `Iterable`-like or `AsyncIterable`-like // type from its members. // @@ -21018,7 +21021,9 @@ func (c *Checker) checkAndAggregateYieldOperandTypes(fn *ast.Node, checkMode Che if yieldExpr.Expression() != nil && c.isConstContext(yieldExpr.Expression()) { yieldExprType = c.getRegularTypeOfLiteralType(yieldExprType) } - yieldTypes = core.AppendIfUnique(yieldTypes, c.getYieldedTypeOfYieldExpression(yieldExpr, yieldExprType, c.anyType, isAsync)) + if yieldType := c.getYieldedTypeOfYieldExpression(yieldExpr, yieldExprType, c.anyType, isAsync); yieldType != nil { + yieldTypes = core.AppendIfUnique(yieldTypes, yieldType) + } var nextType *Type if yieldExpr.AsYieldExpression().AsteriskToken != nil { iterationTypes := c.getIterationTypesOfIterable(yieldExprType, core.IfElse(isAsync, IterationUseAsyncYieldStar, IterationUseYieldStar), yieldExpr.Expression()) diff --git a/tsc/testdata/baselines/reference/compiler/yieldStarSyncIterableInvalidThenable.errors.txt b/tsc/testdata/baselines/reference/compiler/yieldStarSyncIterableInvalidThenable.errors.txt new file mode 100644 index 0000000000000..04a4fa196e7e5 --- /dev/null +++ b/tsc/testdata/baselines/reference/compiler/yieldStarSyncIterableInvalidThenable.errors.txt @@ -0,0 +1,49 @@ +yieldStarSyncIterableInvalidThenable.ts(7,12): error TS1322: Type of iterated elements of a 'yield*' operand must either be a valid promise or must not contain a callable 'then' member. +yieldStarSyncIterableInvalidThenable.ts(8,11): error TS1321: Type of 'yield' operand in an async generator must either be a valid promise or must not contain a callable 'then' member. +yieldStarSyncIterableInvalidThenable.ts(9,31): error TS1320: Type of 'await' operand must either be a valid promise or must not contain a callable 'then' member. +yieldStarSyncIterableInvalidThenable.ts(11,12): error TS1322: Type of iterated elements of a 'yield*' operand must either be a valid promise or must not contain a callable 'then' member. +yieldStarSyncIterableInvalidThenable.ts(12,12): error TS1322: Type of iterated elements of a 'yield*' operand must either be a valid promise or must not contain a callable 'then' member. +yieldStarSyncIterableInvalidThenable.ts(13,12): error TS1322: Type of iterated elements of a 'yield*' operand must either be a valid promise or must not contain a callable 'then' member. +yieldStarSyncIterableInvalidThenable.ts(14,31): error TS1320: Type of 'await' operand must either be a valid promise or must not contain a callable 'then' member. +yieldStarSyncIterableInvalidThenable.ts(15,31): error TS1320: Type of 'await' operand must either be a valid promise or must not contain a callable 'then' member. + + +==== yieldStarSyncIterableInvalidThenable.ts (8 errors) ==== + declare const thenable: { then(onfulfilled: number): void }; + declare const iterable: Iterable; + declare const union: number[] | Iterable; + declare const asyncIterable: AsyncIterable; + + async function* invalid() { + yield* [thenable]; + ~~~~~~~~~~ +!!! error TS1322: Type of iterated elements of a 'yield*' operand must either be a valid promise or must not contain a callable 'then' member. + yield thenable; + ~~~~~~~~ +!!! error TS1321: Type of 'yield' operand in an async generator must either be a valid promise or must not contain a callable 'then' member. + for await (const value of [thenable]) {} + ~~~~~~~~~~ +!!! error TS1320: Type of 'await' operand must either be a valid promise or must not contain a callable 'then' member. + + yield* iterable; + ~~~~~~~~ +!!! error TS1322: Type of iterated elements of a 'yield*' operand must either be a valid promise or must not contain a callable 'then' member. + yield* iterable; + ~~~~~~~~ +!!! error TS1322: Type of iterated elements of a 'yield*' operand must either be a valid promise or must not contain a callable 'then' member. + yield* union; + ~~~~~ +!!! error TS1322: Type of iterated elements of a 'yield*' operand must either be a valid promise or must not contain a callable 'then' member. + for await (const value of union) {} + ~~~~~ +!!! error TS1320: Type of 'await' operand must either be a valid promise or must not contain a callable 'then' member. + for await (const value of asyncIterable) {} + ~~~~~~~~~~~~~ +!!! error TS1320: Type of 'await' operand must either be a valid promise or must not contain a callable 'then' member. + } + + async function* valid() { + yield* [1]; + yield* [Promise.resolve(1)]; + } + \ No newline at end of file diff --git a/tsc/testdata/baselines/reference/compiler/yieldStarSyncIterableInvalidThenable.js b/tsc/testdata/baselines/reference/compiler/yieldStarSyncIterableInvalidThenable.js new file mode 100644 index 0000000000000..856df10948e6c --- /dev/null +++ b/tsc/testdata/baselines/reference/compiler/yieldStarSyncIterableInvalidThenable.js @@ -0,0 +1,42 @@ +//// [tests/cases/compiler/yieldStarSyncIterableInvalidThenable.ts] //// + +//// [yieldStarSyncIterableInvalidThenable.ts] +declare const thenable: { then(onfulfilled: number): void }; +declare const iterable: Iterable; +declare const union: number[] | Iterable; +declare const asyncIterable: AsyncIterable; + +async function* invalid() { + yield* [thenable]; + yield thenable; + for await (const value of [thenable]) {} + + yield* iterable; + yield* iterable; + yield* union; + for await (const value of union) {} + for await (const value of asyncIterable) {} +} + +async function* valid() { + yield* [1]; + yield* [Promise.resolve(1)]; +} + + +//// [yieldStarSyncIterableInvalidThenable.js] +"use strict"; +async function* invalid() { + yield* [thenable]; + yield thenable; + for await (const value of [thenable]) { } + yield* iterable; + yield* iterable; + yield* union; + for await (const value of union) { } + for await (const value of asyncIterable) { } +} +async function* valid() { + yield* [1]; + yield* [Promise.resolve(1)]; +} diff --git a/tsc/testdata/baselines/reference/compiler/yieldStarSyncIterableInvalidThenable.symbols b/tsc/testdata/baselines/reference/compiler/yieldStarSyncIterableInvalidThenable.symbols new file mode 100644 index 0000000000000..b6588123fa494 --- /dev/null +++ b/tsc/testdata/baselines/reference/compiler/yieldStarSyncIterableInvalidThenable.symbols @@ -0,0 +1,64 @@ +//// [tests/cases/compiler/yieldStarSyncIterableInvalidThenable.ts] //// + +=== yieldStarSyncIterableInvalidThenable.ts === +declare const thenable: { then(onfulfilled: number): void }; +>thenable : Symbol(thenable, Decl(yieldStarSyncIterableInvalidThenable.ts, 0, 13)) +>then : Symbol(then, Decl(yieldStarSyncIterableInvalidThenable.ts, 0, 25)) +>onfulfilled : Symbol(onfulfilled, Decl(yieldStarSyncIterableInvalidThenable.ts, 0, 31)) + +declare const iterable: Iterable; +>iterable : Symbol(iterable, Decl(yieldStarSyncIterableInvalidThenable.ts, 1, 13)) +>Iterable : Symbol(Iterable, Decl(lib.es2015.iterable.d.ts, --, --)) +>thenable : Symbol(thenable, Decl(yieldStarSyncIterableInvalidThenable.ts, 0, 13)) + +declare const union: number[] | Iterable; +>union : Symbol(union, Decl(yieldStarSyncIterableInvalidThenable.ts, 2, 13)) +>Iterable : Symbol(Iterable, Decl(lib.es2015.iterable.d.ts, --, --)) +>thenable : Symbol(thenable, Decl(yieldStarSyncIterableInvalidThenable.ts, 0, 13)) + +declare const asyncIterable: AsyncIterable; +>asyncIterable : Symbol(asyncIterable, Decl(yieldStarSyncIterableInvalidThenable.ts, 3, 13)) +>AsyncIterable : Symbol(AsyncIterable, Decl(lib.es2018.asynciterable.d.ts, --, --)) +>thenable : Symbol(thenable, Decl(yieldStarSyncIterableInvalidThenable.ts, 0, 13)) + +async function* invalid() { +>invalid : Symbol(invalid, Decl(yieldStarSyncIterableInvalidThenable.ts, 3, 60)) + + yield* [thenable]; +>thenable : Symbol(thenable, Decl(yieldStarSyncIterableInvalidThenable.ts, 0, 13)) + + yield thenable; +>thenable : Symbol(thenable, Decl(yieldStarSyncIterableInvalidThenable.ts, 0, 13)) + + for await (const value of [thenable]) {} +>value : Symbol(value, Decl(yieldStarSyncIterableInvalidThenable.ts, 8, 20)) +>thenable : Symbol(thenable, Decl(yieldStarSyncIterableInvalidThenable.ts, 0, 13)) + + yield* iterable; +>iterable : Symbol(iterable, Decl(yieldStarSyncIterableInvalidThenable.ts, 1, 13)) + + yield* iterable; +>iterable : Symbol(iterable, Decl(yieldStarSyncIterableInvalidThenable.ts, 1, 13)) + + yield* union; +>union : Symbol(union, Decl(yieldStarSyncIterableInvalidThenable.ts, 2, 13)) + + for await (const value of union) {} +>value : Symbol(value, Decl(yieldStarSyncIterableInvalidThenable.ts, 13, 20)) +>union : Symbol(union, Decl(yieldStarSyncIterableInvalidThenable.ts, 2, 13)) + + for await (const value of asyncIterable) {} +>value : Symbol(value, Decl(yieldStarSyncIterableInvalidThenable.ts, 14, 20)) +>asyncIterable : Symbol(asyncIterable, Decl(yieldStarSyncIterableInvalidThenable.ts, 3, 13)) +} + +async function* valid() { +>valid : Symbol(valid, Decl(yieldStarSyncIterableInvalidThenable.ts, 15, 1)) + + yield* [1]; + yield* [Promise.resolve(1)]; +>Promise.resolve : Symbol(PromiseConstructor.resolve, Decl(lib.es2015.promise.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --)) +>Promise : Symbol(Promise, Decl(lib.es5.d.ts, --, --), Decl(lib.es2015.iterable.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --), Decl(lib.es2015.symbol.wellknown.d.ts, --, --), Decl(lib.es2018.promise.d.ts, --, --)) +>resolve : Symbol(PromiseConstructor.resolve, Decl(lib.es2015.promise.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --)) +} + diff --git a/tsc/testdata/baselines/reference/compiler/yieldStarSyncIterableInvalidThenable.types b/tsc/testdata/baselines/reference/compiler/yieldStarSyncIterableInvalidThenable.types new file mode 100644 index 0000000000000..011e6c06e5634 --- /dev/null +++ b/tsc/testdata/baselines/reference/compiler/yieldStarSyncIterableInvalidThenable.types @@ -0,0 +1,76 @@ +//// [tests/cases/compiler/yieldStarSyncIterableInvalidThenable.ts] //// + +=== yieldStarSyncIterableInvalidThenable.ts === +declare const thenable: { then(onfulfilled: number): void }; +>thenable : { then(onfulfilled: number): void; } +>then : (onfulfilled: number) => void +>onfulfilled : number + +declare const iterable: Iterable; +>iterable : Iterable +>thenable : { then(onfulfilled: number): void; } + +declare const union: number[] | Iterable; +>union : number[] | Iterable<{ then(onfulfilled: number): void; }> +>thenable : { then(onfulfilled: number): void; } + +declare const asyncIterable: AsyncIterable; +>asyncIterable : AsyncIterable<{ then(onfulfilled: number): void; }> +>thenable : { then(onfulfilled: number): void; } + +async function* invalid() { +>invalid : () => AsyncGenerator + + yield* [thenable]; +>yield* [thenable] : undefined +>[thenable] : { then(onfulfilled: number): void; }[] +>thenable : { then(onfulfilled: number): void; } + + yield thenable; +>yield thenable : any +>thenable : { then(onfulfilled: number): void; } + + for await (const value of [thenable]) {} +>value : any +>[thenable] : { then(onfulfilled: number): void; }[] +>thenable : { then(onfulfilled: number): void; } + + yield* iterable; +>yield* iterable : any +>iterable : Iterable + + yield* iterable; +>yield* iterable : any +>iterable : Iterable + + yield* union; +>yield* union : any +>union : number[] | Iterable<{ then(onfulfilled: number): void; }> + + for await (const value of union) {} +>value : any +>union : number[] | Iterable<{ then(onfulfilled: number): void; }> + + for await (const value of asyncIterable) {} +>value : any +>asyncIterable : AsyncIterable<{ then(onfulfilled: number): void; }> +} + +async function* valid() { +>valid : () => AsyncGenerator + + yield* [1]; +>yield* [1] : undefined +>[1] : number[] +>1 : 1 + + yield* [Promise.resolve(1)]; +>yield* [Promise.resolve(1)] : undefined +>[Promise.resolve(1)] : Promise[] +>Promise.resolve(1) : Promise +>Promise.resolve : { (): Promise; (value: T): Promise>; (value: T | PromiseLike): Promise>; } +>Promise : PromiseConstructor +>resolve : { (): Promise; (value: T): Promise>; (value: T | PromiseLike): Promise>; } +>1 : 1 +} + diff --git a/tsc/testdata/tests/cases/compiler/yieldStarSyncIterableInvalidThenable.ts b/tsc/testdata/tests/cases/compiler/yieldStarSyncIterableInvalidThenable.ts new file mode 100644 index 0000000000000..beca7aabc78e8 --- /dev/null +++ b/tsc/testdata/tests/cases/compiler/yieldStarSyncIterableInvalidThenable.ts @@ -0,0 +1,24 @@ +// @target: esnext +// @strict: true + +declare const thenable: { then(onfulfilled: number): void }; +declare const iterable: Iterable; +declare const union: number[] | Iterable; +declare const asyncIterable: AsyncIterable; + +async function* invalid() { + yield* [thenable]; + yield thenable; + for await (const value of [thenable]) {} + + yield* iterable; + yield* iterable; + yield* union; + for await (const value of union) {} + for await (const value of asyncIterable) {} +} + +async function* valid() { + yield* [1]; + yield* [Promise.resolve(1)]; +}