Skip to content

Commit 1cd8ee4

Browse files
authored
fix(42088): fix crash on unreachability condition in for statement (#42110)
1 parent eca8957 commit 1cd8ee4

File tree

6 files changed

+81
-1
lines changed

6 files changed

+81
-1
lines changed

src/compiler/checker.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21966,8 +21966,12 @@ namespace ts {
2196621966
return some((<FlowLabel>flow).antecedents, f => isReachableFlowNodeWorker(f, /*noCacheCheck*/ false));
2196721967
}
2196821968
else if (flags & FlowFlags.LoopLabel) {
21969+
const antecedents = (<FlowLabel>flow).antecedents;
21970+
if (antecedents === undefined || antecedents.length === 0) {
21971+
return false;
21972+
}
2196921973
// A loop is reachable if the control flow path that leads to the top is reachable.
21970-
flow = (<FlowLabel>flow).antecedents![0];
21974+
flow = antecedents[0];
2197121975
}
2197221976
else if (flags & FlowFlags.SwitchClause) {
2197321977
// The control flow path representing an unmatched value in a switch statement with
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
tests/cases/compiler/reachabilityChecks8.ts(4,24): error TS7027: Unreachable code detected.
2+
tests/cases/compiler/reachabilityChecks8.ts(5,24): error TS7027: Unreachable code detected.
3+
4+
5+
==== tests/cases/compiler/reachabilityChecks8.ts (2 errors) ====
6+
try {
7+
for (
8+
(function () { throw "1"; })();
9+
(function () { throw "2"; })();
10+
~~~~~~~~~~
11+
!!! error TS7027: Unreachable code detected.
12+
(function () { throw "3"; })()
13+
~~~~~~~~~~
14+
!!! error TS7027: Unreachable code detected.
15+
) {}
16+
} catch (e) {}
17+
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
//// [reachabilityChecks8.ts]
2+
try {
3+
for (
4+
(function () { throw "1"; })();
5+
(function () { throw "2"; })();
6+
(function () { throw "3"; })()
7+
) {}
8+
} catch (e) {}
9+
10+
11+
//// [reachabilityChecks8.js]
12+
try {
13+
for ((function () { throw "1"; })(); (function () { throw "2"; })(); (function () { throw "3"; })()) { }
14+
}
15+
catch (e) { }
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
=== tests/cases/compiler/reachabilityChecks8.ts ===
2+
try {
3+
for (
4+
(function () { throw "1"; })();
5+
(function () { throw "2"; })();
6+
(function () { throw "3"; })()
7+
) {}
8+
} catch (e) {}
9+
>e : Symbol(e, Decl(reachabilityChecks8.ts, 6, 9))
10+
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
=== tests/cases/compiler/reachabilityChecks8.ts ===
2+
try {
3+
for (
4+
(function () { throw "1"; })();
5+
>(function () { throw "1"; })() : never
6+
>(function () { throw "1"; }) : () => never
7+
>function () { throw "1"; } : () => never
8+
>"1" : "1"
9+
10+
(function () { throw "2"; })();
11+
>(function () { throw "2"; })() : never
12+
>(function () { throw "2"; }) : () => never
13+
>function () { throw "2"; } : () => never
14+
>"2" : "2"
15+
16+
(function () { throw "3"; })()
17+
>(function () { throw "3"; })() : never
18+
>(function () { throw "3"; }) : () => never
19+
>function () { throw "3"; } : () => never
20+
>"3" : "3"
21+
22+
) {}
23+
} catch (e) {}
24+
>e : any
25+
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// @allowUnreachableCode: false
2+
3+
try {
4+
for (
5+
(function () { throw "1"; })();
6+
(function () { throw "2"; })();
7+
(function () { throw "3"; })()
8+
) {}
9+
} catch (e) {}

0 commit comments

Comments
 (0)