diff --git a/src/compiler/binder.ts b/src/compiler/binder.ts index e2d31162127c9..2fe7d30ab56af 100644 --- a/src/compiler/binder.ts +++ b/src/compiler/binder.ts @@ -964,7 +964,11 @@ namespace ts { currentFlow = preTryFlow; bind(node.finallyBlock); } - currentFlow = finishFlowLabel(postFinallyLabel); + // if try statement has finally block and flow after finally block is unreachable - keep it + // otherwise use whatever flow was accumulated at postFinallyLabel + if (!node.finallyBlock || !(currentFlow.flags & FlowFlags.Unreachable)) { + currentFlow = finishFlowLabel(postFinallyLabel); + } } function bindSwitchStatement(node: SwitchStatement): void { diff --git a/tests/baselines/reference/unreachableFlowAfterFinally.js b/tests/baselines/reference/unreachableFlowAfterFinally.js new file mode 100644 index 0000000000000..97f04b219b6bb --- /dev/null +++ b/tests/baselines/reference/unreachableFlowAfterFinally.js @@ -0,0 +1,27 @@ +//// [unreachableFlowAfterFinally.ts] + +function f() { + let x = 100; + try { + throw "WAT" + } + catch (e) { + + } + finally { + return x; + } +} + +//// [unreachableFlowAfterFinally.js] +function f() { + var x = 100; + try { + throw "WAT"; + } + catch (e) { + } + finally { + return x; + } +} diff --git a/tests/baselines/reference/unreachableFlowAfterFinally.symbols b/tests/baselines/reference/unreachableFlowAfterFinally.symbols new file mode 100644 index 0000000000000..92a5d7b06a50b --- /dev/null +++ b/tests/baselines/reference/unreachableFlowAfterFinally.symbols @@ -0,0 +1,20 @@ +=== tests/cases/compiler/unreachableFlowAfterFinally.ts === + +function f() { +>f : Symbol(f, Decl(unreachableFlowAfterFinally.ts, 0, 0)) + + let x = 100; +>x : Symbol(x, Decl(unreachableFlowAfterFinally.ts, 2, 7)) + + try { + throw "WAT" + } + catch (e) { +>e : Symbol(e, Decl(unreachableFlowAfterFinally.ts, 6, 11)) + + } + finally { + return x; +>x : Symbol(x, Decl(unreachableFlowAfterFinally.ts, 2, 7)) + } +} diff --git a/tests/baselines/reference/unreachableFlowAfterFinally.types b/tests/baselines/reference/unreachableFlowAfterFinally.types new file mode 100644 index 0000000000000..814bebfc2fd1d --- /dev/null +++ b/tests/baselines/reference/unreachableFlowAfterFinally.types @@ -0,0 +1,22 @@ +=== tests/cases/compiler/unreachableFlowAfterFinally.ts === + +function f() { +>f : () => number + + let x = 100; +>x : number +>100 : 100 + + try { + throw "WAT" +>"WAT" : "WAT" + } + catch (e) { +>e : any + + } + finally { + return x; +>x : number + } +} diff --git a/tests/cases/compiler/unreachableFlowAfterFinally.ts b/tests/cases/compiler/unreachableFlowAfterFinally.ts new file mode 100644 index 0000000000000..4a7c0944949fa --- /dev/null +++ b/tests/cases/compiler/unreachableFlowAfterFinally.ts @@ -0,0 +1,14 @@ +// @noImplicitReturns: true + +function f() { + let x = 100; + try { + throw "WAT" + } + catch (e) { + + } + finally { + return x; + } +} \ No newline at end of file