Skip to content

Commit 4800464

Browse files
authored
do not reset current flow after processing finally block if it was unreachable (#11310)
* do not reset current flow after processing finally block if it was unreachable * fix tests
1 parent 9afb07d commit 4800464

File tree

5 files changed

+88
-1
lines changed

5 files changed

+88
-1
lines changed

src/compiler/binder.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -964,7 +964,11 @@ namespace ts {
964964
currentFlow = preTryFlow;
965965
bind(node.finallyBlock);
966966
}
967-
currentFlow = finishFlowLabel(postFinallyLabel);
967+
// if try statement has finally block and flow after finally block is unreachable - keep it
968+
// otherwise use whatever flow was accumulated at postFinallyLabel
969+
if (!node.finallyBlock || !(currentFlow.flags & FlowFlags.Unreachable)) {
970+
currentFlow = finishFlowLabel(postFinallyLabel);
971+
}
968972
}
969973

970974
function bindSwitchStatement(node: SwitchStatement): void {
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
//// [unreachableFlowAfterFinally.ts]
2+
3+
function f() {
4+
let x = 100;
5+
try {
6+
throw "WAT"
7+
}
8+
catch (e) {
9+
10+
}
11+
finally {
12+
return x;
13+
}
14+
}
15+
16+
//// [unreachableFlowAfterFinally.js]
17+
function f() {
18+
var x = 100;
19+
try {
20+
throw "WAT";
21+
}
22+
catch (e) {
23+
}
24+
finally {
25+
return x;
26+
}
27+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
=== tests/cases/compiler/unreachableFlowAfterFinally.ts ===
2+
3+
function f() {
4+
>f : Symbol(f, Decl(unreachableFlowAfterFinally.ts, 0, 0))
5+
6+
let x = 100;
7+
>x : Symbol(x, Decl(unreachableFlowAfterFinally.ts, 2, 7))
8+
9+
try {
10+
throw "WAT"
11+
}
12+
catch (e) {
13+
>e : Symbol(e, Decl(unreachableFlowAfterFinally.ts, 6, 11))
14+
15+
}
16+
finally {
17+
return x;
18+
>x : Symbol(x, Decl(unreachableFlowAfterFinally.ts, 2, 7))
19+
}
20+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
=== tests/cases/compiler/unreachableFlowAfterFinally.ts ===
2+
3+
function f() {
4+
>f : () => number
5+
6+
let x = 100;
7+
>x : number
8+
>100 : 100
9+
10+
try {
11+
throw "WAT"
12+
>"WAT" : "WAT"
13+
}
14+
catch (e) {
15+
>e : any
16+
17+
}
18+
finally {
19+
return x;
20+
>x : number
21+
}
22+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// @noImplicitReturns: true
2+
3+
function f() {
4+
let x = 100;
5+
try {
6+
throw "WAT"
7+
}
8+
catch (e) {
9+
10+
}
11+
finally {
12+
return x;
13+
}
14+
}

0 commit comments

Comments
 (0)