Skip to content

Commit a747a4d

Browse files
committed
Fix crash with nested generators
1 parent c302893 commit a747a4d

File tree

5 files changed

+224
-0
lines changed

5 files changed

+224
-0
lines changed

src/compiler/transformers/generators.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -552,6 +552,7 @@ namespace ts {
552552
const savedBlocks = blocks;
553553
const savedBlockOffsets = blockOffsets;
554554
const savedBlockActions = blockActions;
555+
const savedBlockStack = blockStack;
555556
const savedLabelOffsets = labelOffsets;
556557
const savedLabelExpressions = labelExpressions;
557558
const savedNextLabelId = nextLabelId;
@@ -566,6 +567,7 @@ namespace ts {
566567
blocks = undefined;
567568
blockOffsets = undefined;
568569
blockActions = undefined;
570+
blockStack = undefined;
569571
labelOffsets = undefined;
570572
labelExpressions = undefined;
571573
nextLabelId = 1;
@@ -591,6 +593,7 @@ namespace ts {
591593
blocks = savedBlocks;
592594
blockOffsets = savedBlockOffsets;
593595
blockActions = savedBlockActions;
596+
blockStack = savedBlockStack;
594597
labelOffsets = savedLabelOffsets;
595598
labelExpressions = savedLabelExpressions;
596599
nextLabelId = savedNextLabelId;
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
//// [tests/cases/compiler/transformNestedGeneratorsWithTry.ts] ////
2+
3+
//// [main.ts]
4+
import * as Bluebird from 'bluebird';
5+
async function a(): Bluebird<void> {
6+
try {
7+
const b = async function b(): Bluebird<void> {
8+
try {
9+
await Bluebird.resolve(); // -- remove this and it compiles
10+
} catch (error) { }
11+
};
12+
13+
await b(); // -- or remove this and it compiles
14+
} catch (error) { }
15+
}
16+
17+
//// [bluebird.d.ts]
18+
declare module "bluebird" {
19+
type Bluebird<T> = Promise<T>;
20+
const Bluebird: typeof Promise;
21+
export = Bluebird;
22+
}
23+
24+
//// [main.js]
25+
"use strict";
26+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
27+
return new (P || (P = Promise))(function (resolve, reject) {
28+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
29+
function rejected(value) { try { step(generator.throw(value)); } catch (e) { reject(e); } }
30+
function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
31+
step((generator = generator.apply(thisArg, _arguments)).next());
32+
});
33+
};
34+
var __generator = (this && this.__generator) || function (thisArg, body) {
35+
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t;
36+
return { next: verb(0), "throw": verb(1), "return": verb(2) };
37+
function verb(n) { return function (v) { return step([n, v]); }; }
38+
function step(op) {
39+
if (f) throw new TypeError("Generator is already executing.");
40+
while (_) try {
41+
if (f = 1, y && (t = y[op[0] & 2 ? "return" : op[0] ? "throw" : "next"]) && !(t = t.call(y, op[1])).done) return t;
42+
if (y = 0, t) op = [0, t.value];
43+
switch (op[0]) {
44+
case 0: case 1: t = op; break;
45+
case 4: _.label++; return { value: op[1], done: false };
46+
case 5: _.label++; y = op[1]; op = [0]; continue;
47+
case 7: op = _.ops.pop(); _.trys.pop(); continue;
48+
default:
49+
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
50+
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
51+
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
52+
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
53+
if (t[2]) _.ops.pop();
54+
_.trys.pop(); continue;
55+
}
56+
op = body.call(thisArg, _);
57+
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
58+
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
59+
}
60+
};
61+
var Bluebird = require("bluebird");
62+
function a() {
63+
return __awaiter(this, void 0, Bluebird, function () {
64+
var b, error_1;
65+
return __generator(this, function (_a) {
66+
switch (_a.label) {
67+
case 0:
68+
_a.trys.push([0, 2, , 3]);
69+
b = function b() {
70+
return __awaiter(this, void 0, Bluebird, function () {
71+
var error_2;
72+
return __generator(this, function (_a) {
73+
switch (_a.label) {
74+
case 0:
75+
_a.trys.push([0, 2, , 3]);
76+
return [4 /*yield*/, Bluebird.resolve()];
77+
case 1:
78+
_a.sent(); // -- remove this and it compiles
79+
return [3 /*break*/, 3];
80+
case 2:
81+
error_2 = _a.sent();
82+
return [3 /*break*/, 3];
83+
case 3: return [2 /*return*/];
84+
}
85+
});
86+
});
87+
};
88+
return [4 /*yield*/, b()];
89+
case 1:
90+
_a.sent(); // -- or remove this and it compiles
91+
return [3 /*break*/, 3];
92+
case 2:
93+
error_1 = _a.sent();
94+
return [3 /*break*/, 3];
95+
case 3: return [2 /*return*/];
96+
}
97+
});
98+
});
99+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
=== tests/cases/compiler/main.ts ===
2+
import * as Bluebird from 'bluebird';
3+
>Bluebird : Symbol(Bluebird, Decl(main.ts, 0, 6))
4+
5+
async function a(): Bluebird<void> {
6+
>a : Symbol(a, Decl(main.ts, 0, 37))
7+
>Bluebird : Symbol(Bluebird, Decl(main.ts, 0, 6))
8+
9+
try {
10+
const b = async function b(): Bluebird<void> {
11+
>b : Symbol(b, Decl(main.ts, 3, 9))
12+
>b : Symbol(b, Decl(main.ts, 3, 13))
13+
>Bluebird : Symbol(Bluebird, Decl(main.ts, 0, 6))
14+
15+
try {
16+
await Bluebird.resolve(); // -- remove this and it compiles
17+
>Bluebird.resolve : Symbol(PromiseConstructor.resolve, Decl(lib.es2015.promise.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --))
18+
>Bluebird : Symbol(Bluebird, Decl(main.ts, 0, 6))
19+
>resolve : Symbol(PromiseConstructor.resolve, Decl(lib.es2015.promise.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --))
20+
21+
} catch (error) { }
22+
>error : Symbol(error, Decl(main.ts, 6, 15))
23+
24+
};
25+
26+
await b(); // -- or remove this and it compiles
27+
>b : Symbol(b, Decl(main.ts, 3, 9))
28+
29+
} catch (error) { }
30+
>error : Symbol(error, Decl(main.ts, 10, 11))
31+
}
32+
33+
=== tests/cases/compiler/bluebird.d.ts ===
34+
declare module "bluebird" {
35+
type Bluebird<T> = Promise<T>;
36+
>Bluebird : Symbol(Bluebird, Decl(bluebird.d.ts, 0, 27), Decl(bluebird.d.ts, 2, 9))
37+
>T : Symbol(T, Decl(bluebird.d.ts, 1, 18))
38+
>Promise : Symbol(Promise, Decl(lib.es2015.symbol.wellknown.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --), Decl(lib.es2015.iterable.d.ts, --, --))
39+
>T : Symbol(T, Decl(bluebird.d.ts, 1, 18))
40+
41+
const Bluebird: typeof Promise;
42+
>Bluebird : Symbol(Bluebird, Decl(bluebird.d.ts, 0, 27), Decl(bluebird.d.ts, 2, 9))
43+
>Promise : Symbol(Promise, Decl(lib.es2015.symbol.wellknown.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --), Decl(lib.es2015.iterable.d.ts, --, --))
44+
45+
export = Bluebird;
46+
>Bluebird : Symbol(Bluebird, Decl(bluebird.d.ts, 0, 27), Decl(bluebird.d.ts, 2, 9))
47+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
=== tests/cases/compiler/main.ts ===
2+
import * as Bluebird from 'bluebird';
3+
>Bluebird : PromiseConstructor
4+
5+
async function a(): Bluebird<void> {
6+
>a : () => Promise<void>
7+
>Bluebird : Promise<T>
8+
9+
try {
10+
const b = async function b(): Bluebird<void> {
11+
>b : () => Promise<void>
12+
>async function b(): Bluebird<void> { try { await Bluebird.resolve(); // -- remove this and it compiles } catch (error) { } } : () => Promise<void>
13+
>b : () => Promise<void>
14+
>Bluebird : Promise<T>
15+
16+
try {
17+
await Bluebird.resolve(); // -- remove this and it compiles
18+
>await Bluebird.resolve() : void
19+
>Bluebird.resolve() : Promise<void>
20+
>Bluebird.resolve : { <T>(value: T | PromiseLike<T>): Promise<T>; (): Promise<void>; }
21+
>Bluebird : PromiseConstructor
22+
>resolve : { <T>(value: T | PromiseLike<T>): Promise<T>; (): Promise<void>; }
23+
24+
} catch (error) { }
25+
>error : any
26+
27+
};
28+
29+
await b(); // -- or remove this and it compiles
30+
>await b() : void
31+
>b() : Promise<void>
32+
>b : () => Promise<void>
33+
34+
} catch (error) { }
35+
>error : any
36+
}
37+
38+
=== tests/cases/compiler/bluebird.d.ts ===
39+
declare module "bluebird" {
40+
type Bluebird<T> = Promise<T>;
41+
>Bluebird : Promise<T>
42+
>T : T
43+
>Promise : Promise<T>
44+
>T : T
45+
46+
const Bluebird: typeof Promise;
47+
>Bluebird : PromiseConstructor
48+
>Promise : PromiseConstructor
49+
50+
export = Bluebird;
51+
>Bluebird : Promise<T>
52+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// @target: ES5
2+
// @lib: es5,es6
3+
// @filename: main.ts
4+
// https://github.com/Microsoft/TypeScript/issues/11177
5+
import * as Bluebird from 'bluebird';
6+
async function a(): Bluebird<void> {
7+
try {
8+
const b = async function b(): Bluebird<void> {
9+
try {
10+
await Bluebird.resolve(); // -- remove this and it compiles
11+
} catch (error) { }
12+
};
13+
14+
await b(); // -- or remove this and it compiles
15+
} catch (error) { }
16+
}
17+
18+
// @filename: bluebird.d.ts
19+
declare module "bluebird" {
20+
type Bluebird<T> = Promise<T>;
21+
const Bluebird: typeof Promise;
22+
export = Bluebird;
23+
}

0 commit comments

Comments
 (0)