fix(42088): fix crash on unreachability condition in for statement (#42110)

This commit is contained in:
Oleksandr T
2021-01-11 01:42:39 +02:00
committed by GitHub
parent eca8957430
commit 1cd8ee4b8d
6 changed files with 81 additions and 1 deletions

View File

@@ -21966,8 +21966,12 @@ namespace ts {
return some((<FlowLabel>flow).antecedents, f => isReachableFlowNodeWorker(f, /*noCacheCheck*/ false));
}
else if (flags & FlowFlags.LoopLabel) {
const antecedents = (<FlowLabel>flow).antecedents;
if (antecedents === undefined || antecedents.length === 0) {
return false;
}
// A loop is reachable if the control flow path that leads to the top is reachable.
flow = (<FlowLabel>flow).antecedents![0];
flow = antecedents[0];
}
else if (flags & FlowFlags.SwitchClause) {
// The control flow path representing an unmatched value in a switch statement with

View File

@@ -0,0 +1,17 @@
tests/cases/compiler/reachabilityChecks8.ts(4,24): error TS7027: Unreachable code detected.
tests/cases/compiler/reachabilityChecks8.ts(5,24): error TS7027: Unreachable code detected.
==== tests/cases/compiler/reachabilityChecks8.ts (2 errors) ====
try {
for (
(function () { throw "1"; })();
(function () { throw "2"; })();
~~~~~~~~~~
!!! error TS7027: Unreachable code detected.
(function () { throw "3"; })()
~~~~~~~~~~
!!! error TS7027: Unreachable code detected.
) {}
} catch (e) {}

View File

@@ -0,0 +1,15 @@
//// [reachabilityChecks8.ts]
try {
for (
(function () { throw "1"; })();
(function () { throw "2"; })();
(function () { throw "3"; })()
) {}
} catch (e) {}
//// [reachabilityChecks8.js]
try {
for ((function () { throw "1"; })(); (function () { throw "2"; })(); (function () { throw "3"; })()) { }
}
catch (e) { }

View File

@@ -0,0 +1,10 @@
=== tests/cases/compiler/reachabilityChecks8.ts ===
try {
for (
(function () { throw "1"; })();
(function () { throw "2"; })();
(function () { throw "3"; })()
) {}
} catch (e) {}
>e : Symbol(e, Decl(reachabilityChecks8.ts, 6, 9))

View File

@@ -0,0 +1,25 @@
=== tests/cases/compiler/reachabilityChecks8.ts ===
try {
for (
(function () { throw "1"; })();
>(function () { throw "1"; })() : never
>(function () { throw "1"; }) : () => never
>function () { throw "1"; } : () => never
>"1" : "1"
(function () { throw "2"; })();
>(function () { throw "2"; })() : never
>(function () { throw "2"; }) : () => never
>function () { throw "2"; } : () => never
>"2" : "2"
(function () { throw "3"; })()
>(function () { throw "3"; })() : never
>(function () { throw "3"; }) : () => never
>function () { throw "3"; } : () => never
>"3" : "3"
) {}
} catch (e) {}
>e : any

View File

@@ -0,0 +1,9 @@
// @allowUnreachableCode: false
try {
for (
(function () { throw "1"; })();
(function () { throw "2"; })();
(function () { throw "3"; })()
) {}
} catch (e) {}