Skip to content

Commit 952e9ca

Browse files
committed
Remove debug assertions due to invalid syntax in generators transform
1 parent 6da73b0 commit 952e9ca

File tree

4 files changed

+131
-33
lines changed

4 files changed

+131
-33
lines changed

src/compiler/transformers/generators.ts

Lines changed: 43 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1639,8 +1639,13 @@ namespace ts {
16391639

16401640
function transformAndEmitContinueStatement(node: ContinueStatement): void {
16411641
const label = findContinueTarget(node.label ? unescapeLeadingUnderscores(node.label.escapedText) : undefined);
1642-
Debug.assert(label > 0, "Expected continue statment to point to a valid Label.");
1643-
emitBreak(label, /*location*/ node);
1642+
if (label > 0) {
1643+
emitBreak(label, /*location*/ node);
1644+
}
1645+
else {
1646+
// invalid continue without a containing loop. Leave the node as is, per #17875.
1647+
emitStatement(node);
1648+
}
16441649
}
16451650

16461651
function visitContinueStatement(node: ContinueStatement): Statement {
@@ -1656,8 +1661,13 @@ namespace ts {
16561661

16571662
function transformAndEmitBreakStatement(node: BreakStatement): void {
16581663
const label = findBreakTarget(node.label ? unescapeLeadingUnderscores(node.label.escapedText) : undefined);
1659-
Debug.assert(label > 0, "Expected break statment to point to a valid Label.");
1660-
emitBreak(label, /*location*/ node);
1664+
if (label > 0) {
1665+
emitBreak(label, /*location*/ node);
1666+
}
1667+
else {
1668+
// invalid break without a containing loop, switch, or labeled statement. Leave the node as is, per #17875.
1669+
emitStatement(node);
1670+
}
16611671
}
16621672

16631673
function visitBreakStatement(node: BreakStatement): Statement {
@@ -2351,27 +2361,27 @@ namespace ts {
23512361
* @param labelText An optional name of a containing labeled statement.
23522362
*/
23532363
function findBreakTarget(labelText?: string): Label {
2354-
Debug.assert(blocks !== undefined);
2355-
if (labelText) {
2356-
for (let i = blockStack.length - 1; i >= 0; i--) {
2357-
const block = blockStack[i];
2358-
if (supportsLabeledBreakOrContinue(block) && block.labelText === labelText) {
2359-
return block.breakLabel;
2360-
}
2361-
else if (supportsUnlabeledBreak(block) && hasImmediateContainingLabeledBlock(labelText, i - 1)) {
2362-
return block.breakLabel;
2364+
if (blockStack) {
2365+
if (labelText) {
2366+
for (let i = blockStack.length - 1; i >= 0; i--) {
2367+
const block = blockStack[i];
2368+
if (supportsLabeledBreakOrContinue(block) && block.labelText === labelText) {
2369+
return block.breakLabel;
2370+
}
2371+
else if (supportsUnlabeledBreak(block) && hasImmediateContainingLabeledBlock(labelText, i - 1)) {
2372+
return block.breakLabel;
2373+
}
23632374
}
23642375
}
2365-
}
2366-
else {
2367-
for (let i = blockStack.length - 1; i >= 0; i--) {
2368-
const block = blockStack[i];
2369-
if (supportsUnlabeledBreak(block)) {
2370-
return block.breakLabel;
2376+
else {
2377+
for (let i = blockStack.length - 1; i >= 0; i--) {
2378+
const block = blockStack[i];
2379+
if (supportsUnlabeledBreak(block)) {
2380+
return block.breakLabel;
2381+
}
23712382
}
23722383
}
23732384
}
2374-
23752385
return 0;
23762386
}
23772387

@@ -2381,24 +2391,24 @@ namespace ts {
23812391
* @param labelText An optional name of a containing labeled statement.
23822392
*/
23832393
function findContinueTarget(labelText?: string): Label {
2384-
Debug.assert(blocks !== undefined);
2385-
if (labelText) {
2386-
for (let i = blockStack.length - 1; i >= 0; i--) {
2387-
const block = blockStack[i];
2388-
if (supportsUnlabeledContinue(block) && hasImmediateContainingLabeledBlock(labelText, i - 1)) {
2389-
return block.continueLabel;
2394+
if (blockStack) {
2395+
if (labelText) {
2396+
for (let i = blockStack.length - 1; i >= 0; i--) {
2397+
const block = blockStack[i];
2398+
if (supportsUnlabeledContinue(block) && hasImmediateContainingLabeledBlock(labelText, i - 1)) {
2399+
return block.continueLabel;
2400+
}
23902401
}
23912402
}
2392-
}
2393-
else {
2394-
for (let i = blockStack.length - 1; i >= 0; i--) {
2395-
const block = blockStack[i];
2396-
if (supportsUnlabeledContinue(block)) {
2397-
return block.continueLabel;
2403+
else {
2404+
for (let i = blockStack.length - 1; i >= 0; i--) {
2405+
const block = blockStack[i];
2406+
if (supportsUnlabeledContinue(block)) {
2407+
return block.continueLabel;
2408+
}
23982409
}
23992410
}
24002411
}
2401-
24022412
return 0;
24032413
}
24042414

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
tests/cases/compiler/invalidContinueInDownlevelAsync.ts(3,9): error TS1107: Jump target cannot cross function boundary.
2+
tests/cases/compiler/invalidContinueInDownlevelAsync.ts(6,9): error TS7027: Unreachable code detected.
3+
4+
5+
==== tests/cases/compiler/invalidContinueInDownlevelAsync.ts (2 errors) ====
6+
async function func() {
7+
if (true) {
8+
continue;
9+
~~~~~~~~~
10+
!!! error TS1107: Jump target cannot cross function boundary.
11+
}
12+
else {
13+
await 1;
14+
~~~~~
15+
!!! error TS7027: Unreachable code detected.
16+
}
17+
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
//// [invalidContinueInDownlevelAsync.ts]
2+
async function func() {
3+
if (true) {
4+
continue;
5+
}
6+
else {
7+
await 1;
8+
}
9+
}
10+
11+
//// [invalidContinueInDownlevelAsync.js]
12+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
13+
return new (P || (P = Promise))(function (resolve, reject) {
14+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
15+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
16+
function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
17+
step((generator = generator.apply(thisArg, _arguments || [])).next());
18+
});
19+
};
20+
var __generator = (this && this.__generator) || function (thisArg, body) {
21+
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
22+
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
23+
function verb(n) { return function (v) { return step([n, v]); }; }
24+
function step(op) {
25+
if (f) throw new TypeError("Generator is already executing.");
26+
while (_) try {
27+
if (f = 1, y && (t = y[op[0] & 2 ? "return" : op[0] ? "throw" : "next"]) && !(t = t.call(y, op[1])).done) return t;
28+
if (y = 0, t) op = [0, t.value];
29+
switch (op[0]) {
30+
case 0: case 1: t = op; break;
31+
case 4: _.label++; return { value: op[1], done: false };
32+
case 5: _.label++; y = op[1]; op = [0]; continue;
33+
case 7: op = _.ops.pop(); _.trys.pop(); continue;
34+
default:
35+
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
36+
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
37+
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
38+
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
39+
if (t[2]) _.ops.pop();
40+
_.trys.pop(); continue;
41+
}
42+
op = body.call(thisArg, _);
43+
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
44+
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
45+
}
46+
};
47+
function func() {
48+
return __awaiter(this, void 0, void 0, function () {
49+
return __generator(this, function (_a) {
50+
switch (_a.label) {
51+
case 0:
52+
if (!true) return [3 /*break*/, 1];
53+
continue;
54+
return [3 /*break*/, 3];
55+
case 1: return [4 /*yield*/, 1];
56+
case 2:
57+
_a.sent();
58+
_a.label = 3;
59+
case 3: return [2 /*return*/];
60+
}
61+
});
62+
});
63+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
async function func() {
2+
if (true) {
3+
continue;
4+
}
5+
else {
6+
await 1;
7+
}
8+
}

0 commit comments

Comments
 (0)