Skip to content

Commit

Permalink
Merge pull request #27271 from Microsoft/fix24570-3
Browse files Browse the repository at this point in the history
Fix iterated type in for-await-of
  • Loading branch information
rbuckton authored Sep 21, 2018
2 parents 336be23 + 112fe6e commit 5fb3976
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 17 deletions.
16 changes: 11 additions & 5 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25313,14 +25313,18 @@ namespace ts {

if (allowSyncIterables) {
if (typeAsIterable.iteratedTypeOfIterable) {
return typeAsIterable.iteratedTypeOfIterable;
return allowAsyncIterables
? typeAsIterable.iteratedTypeOfAsyncIterable = getAwaitedType(typeAsIterable.iteratedTypeOfIterable)
: typeAsIterable.iteratedTypeOfIterable;
}

// As an optimization, if the type is an instantiation of the global `Iterable<T>` or
// `IterableIterator<T>` then just grab its type argument.
if (isReferenceToType(type, getGlobalIterableType(/*reportErrors*/ false)) ||
isReferenceToType(type, getGlobalIterableIteratorType(/*reportErrors*/ false))) {
return typeAsIterable.iteratedTypeOfIterable = (<GenericType>type).typeArguments![0];
return allowAsyncIterables
? typeAsIterable.iteratedTypeOfAsyncIterable = getAwaitedType((<GenericType>type).typeArguments![0])
: typeAsIterable.iteratedTypeOfIterable = (<GenericType>type).typeArguments![0];
}
}

Expand Down Expand Up @@ -25351,9 +25355,11 @@ namespace ts {
: createIterableType(iteratedType), errorNode);
}

return asyncMethodType
? typeAsIterable.iteratedTypeOfAsyncIterable = iteratedType
: typeAsIterable.iteratedTypeOfIterable = iteratedType;
if (iteratedType) {
return allowAsyncIterables
? typeAsIterable.iteratedTypeOfAsyncIterable = asyncMethodType ? iteratedType : getAwaitedType(iteratedType)
: typeAsIterable.iteratedTypeOfIterable = iteratedType;
}
}
}

Expand Down
45 changes: 33 additions & 12 deletions tests/baselines/reference/types.forAwait.esnext.1.symbols
Original file line number Diff line number Diff line change
Expand Up @@ -7,49 +7,70 @@ declare const iterable: Iterable<number>;
>iterable : Symbol(iterable, Decl(types.forAwait.esnext.1.ts, 1, 13))
>Iterable : Symbol(Iterable, Decl(lib.es2015.iterable.d.ts, --, --))

declare const iterableOfPromise: Iterable<Promise<number>>;
>iterableOfPromise : Symbol(iterableOfPromise, Decl(types.forAwait.esnext.1.ts, 2, 13))
>Iterable : Symbol(Iterable, Decl(lib.es2015.iterable.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, --, --))

async function f1() {
>f1 : Symbol(f1, Decl(types.forAwait.esnext.1.ts, 1, 41))
>f1 : Symbol(f1, Decl(types.forAwait.esnext.1.ts, 2, 59))

let y: number;
>y : Symbol(y, Decl(types.forAwait.esnext.1.ts, 3, 7))
>y : Symbol(y, Decl(types.forAwait.esnext.1.ts, 4, 7))

for await (const x of asyncIterable) {
>x : Symbol(x, Decl(types.forAwait.esnext.1.ts, 4, 20))
>x : Symbol(x, Decl(types.forAwait.esnext.1.ts, 5, 20))
>asyncIterable : Symbol(asyncIterable, Decl(types.forAwait.esnext.1.ts, 0, 13))
}
for await (const x of iterable) {
>x : Symbol(x, Decl(types.forAwait.esnext.1.ts, 6, 20))
>x : Symbol(x, Decl(types.forAwait.esnext.1.ts, 7, 20))
>iterable : Symbol(iterable, Decl(types.forAwait.esnext.1.ts, 1, 13))
}
for await (const x of iterableOfPromise) {
>x : Symbol(x, Decl(types.forAwait.esnext.1.ts, 9, 20))
>iterableOfPromise : Symbol(iterableOfPromise, Decl(types.forAwait.esnext.1.ts, 2, 13))
}
for await (y of asyncIterable) {
>y : Symbol(y, Decl(types.forAwait.esnext.1.ts, 3, 7))
>y : Symbol(y, Decl(types.forAwait.esnext.1.ts, 4, 7))
>asyncIterable : Symbol(asyncIterable, Decl(types.forAwait.esnext.1.ts, 0, 13))
}
for await (y of iterable) {
>y : Symbol(y, Decl(types.forAwait.esnext.1.ts, 3, 7))
>y : Symbol(y, Decl(types.forAwait.esnext.1.ts, 4, 7))
>iterable : Symbol(iterable, Decl(types.forAwait.esnext.1.ts, 1, 13))
}
for await (y of iterableOfPromise) {
>y : Symbol(y, Decl(types.forAwait.esnext.1.ts, 4, 7))
>iterableOfPromise : Symbol(iterableOfPromise, Decl(types.forAwait.esnext.1.ts, 2, 13))
}
}
async function * f2() {
>f2 : Symbol(f2, Decl(types.forAwait.esnext.1.ts, 12, 1))
>f2 : Symbol(f2, Decl(types.forAwait.esnext.1.ts, 17, 1))

let y: number;
>y : Symbol(y, Decl(types.forAwait.esnext.1.ts, 14, 7))
>y : Symbol(y, Decl(types.forAwait.esnext.1.ts, 19, 7))

for await (const x of asyncIterable) {
>x : Symbol(x, Decl(types.forAwait.esnext.1.ts, 15, 20))
>x : Symbol(x, Decl(types.forAwait.esnext.1.ts, 20, 20))
>asyncIterable : Symbol(asyncIterable, Decl(types.forAwait.esnext.1.ts, 0, 13))
}
for await (const x of iterable) {
>x : Symbol(x, Decl(types.forAwait.esnext.1.ts, 17, 20))
>x : Symbol(x, Decl(types.forAwait.esnext.1.ts, 22, 20))
>iterable : Symbol(iterable, Decl(types.forAwait.esnext.1.ts, 1, 13))
}
for await (const x of iterableOfPromise) {
>x : Symbol(x, Decl(types.forAwait.esnext.1.ts, 24, 20))
>iterableOfPromise : Symbol(iterableOfPromise, Decl(types.forAwait.esnext.1.ts, 2, 13))
}
for await (y of asyncIterable) {
>y : Symbol(y, Decl(types.forAwait.esnext.1.ts, 14, 7))
>y : Symbol(y, Decl(types.forAwait.esnext.1.ts, 19, 7))
>asyncIterable : Symbol(asyncIterable, Decl(types.forAwait.esnext.1.ts, 0, 13))
}
for await (y of iterable) {
>y : Symbol(y, Decl(types.forAwait.esnext.1.ts, 14, 7))
>y : Symbol(y, Decl(types.forAwait.esnext.1.ts, 19, 7))
>iterable : Symbol(iterable, Decl(types.forAwait.esnext.1.ts, 1, 13))
}
for await (y of iterableOfPromise) {
>y : Symbol(y, Decl(types.forAwait.esnext.1.ts, 19, 7))
>iterableOfPromise : Symbol(iterableOfPromise, Decl(types.forAwait.esnext.1.ts, 2, 13))
}
}
19 changes: 19 additions & 0 deletions tests/baselines/reference/types.forAwait.esnext.1.types
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ declare const asyncIterable: AsyncIterable<number>;
declare const iterable: Iterable<number>;
>iterable : Iterable<number>

declare const iterableOfPromise: Iterable<Promise<number>>;
>iterableOfPromise : Iterable<Promise<number>>

async function f1() {
>f1 : () => Promise<void>

Expand All @@ -18,6 +21,10 @@ async function f1() {
for await (const x of iterable) {
>x : number
>iterable : Iterable<number>
}
for await (const x of iterableOfPromise) {
>x : number
>iterableOfPromise : Iterable<Promise<number>>
}
for await (y of asyncIterable) {
>y : number
Expand All @@ -27,6 +34,10 @@ async function f1() {
>y : number
>iterable : Iterable<number>
}
for await (y of iterableOfPromise) {
>y : number
>iterableOfPromise : Iterable<Promise<number>>
}
}
async function * f2() {
>f2 : () => AsyncIterableIterator<any>
Expand All @@ -41,6 +52,10 @@ async function * f2() {
for await (const x of iterable) {
>x : number
>iterable : Iterable<number>
}
for await (const x of iterableOfPromise) {
>x : number
>iterableOfPromise : Iterable<Promise<number>>
}
for await (y of asyncIterable) {
>y : number
Expand All @@ -50,4 +65,8 @@ async function * f2() {
>y : number
>iterable : Iterable<number>
}
for await (y of iterableOfPromise) {
>y : number
>iterableOfPromise : Iterable<Promise<number>>
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,34 @@
// @noEmit: true
declare const asyncIterable: AsyncIterable<number>;
declare const iterable: Iterable<number>;
declare const iterableOfPromise: Iterable<Promise<number>>;
async function f1() {
let y: number;
for await (const x of asyncIterable) {
}
for await (const x of iterable) {
}
for await (const x of iterableOfPromise) {
}
for await (y of asyncIterable) {
}
for await (y of iterable) {
}
for await (y of iterableOfPromise) {
}
}
async function * f2() {
let y: number;
for await (const x of asyncIterable) {
}
for await (const x of iterable) {
}
for await (const x of iterableOfPromise) {
}
for await (y of asyncIterable) {
}
for await (y of iterable) {
}
for await (y of iterableOfPromise) {
}
}

0 comments on commit 5fb3976

Please sign in to comment.