Skip to content

Commit e70f4ac

Browse files
authored
Merge pull request #11291 from Microsoft/fix11177
Fix crash with nested generators
2 parents 4b8e8b7 + f293bf5 commit e70f4ac

File tree

5 files changed

+228
-0
lines changed

5 files changed

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