Skip to content

Commit 5105a31

Browse files
committed
properly set reachable state after protected try\finally block
1 parent 02e3abe commit 5105a31

11 files changed

Lines changed: 192 additions & 1 deletion

src/compiler/binder.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -685,7 +685,7 @@ namespace ts {
685685
// post catch/finally state is reachable if
686686
// - post try state is reachable - control flow can fall out of try block
687687
// - post catch state is reachable - control flow can fall out of catch block
688-
currentReachabilityState = or(postTryState, postCatchState);
688+
currentReachabilityState = n.catchClause ? or(postTryState, postCatchState) : postTryState;
689689
}
690690

691691
function bindSwitchStatement(n: SwitchStatement): void {
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
//// [noImplicitReturnsWithProtectedBlocks1.ts]
2+
declare function log(s: string): void;
3+
declare function get(): number;
4+
5+
function main1() : number {
6+
try {
7+
return get();
8+
}
9+
finally {
10+
log("in finally");
11+
}
12+
}
13+
14+
//// [noImplicitReturnsWithProtectedBlocks1.js]
15+
function main1() {
16+
try {
17+
return get();
18+
}
19+
finally {
20+
log("in finally");
21+
}
22+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
=== tests/cases/compiler/noImplicitReturnsWithProtectedBlocks1.ts ===
2+
declare function log(s: string): void;
3+
>log : Symbol(log, Decl(noImplicitReturnsWithProtectedBlocks1.ts, 0, 0))
4+
>s : Symbol(s, Decl(noImplicitReturnsWithProtectedBlocks1.ts, 0, 21))
5+
6+
declare function get(): number;
7+
>get : Symbol(get, Decl(noImplicitReturnsWithProtectedBlocks1.ts, 0, 38))
8+
9+
function main1() : number {
10+
>main1 : Symbol(main1, Decl(noImplicitReturnsWithProtectedBlocks1.ts, 1, 31))
11+
12+
try {
13+
return get();
14+
>get : Symbol(get, Decl(noImplicitReturnsWithProtectedBlocks1.ts, 0, 38))
15+
}
16+
finally {
17+
log("in finally");
18+
>log : Symbol(log, Decl(noImplicitReturnsWithProtectedBlocks1.ts, 0, 0))
19+
}
20+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
=== tests/cases/compiler/noImplicitReturnsWithProtectedBlocks1.ts ===
2+
declare function log(s: string): void;
3+
>log : (s: string) => void
4+
>s : string
5+
6+
declare function get(): number;
7+
>get : () => number
8+
9+
function main1() : number {
10+
>main1 : () => number
11+
12+
try {
13+
return get();
14+
>get() : number
15+
>get : () => number
16+
}
17+
finally {
18+
log("in finally");
19+
>log("in finally") : void
20+
>log : (s: string) => void
21+
>"in finally" : string
22+
}
23+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
tests/cases/compiler/noImplicitReturnsWithProtectedBlocks2.ts(4,20): error TS7030: Not all code paths return a value.
2+
3+
4+
==== tests/cases/compiler/noImplicitReturnsWithProtectedBlocks2.ts (1 errors) ====
5+
declare function log(s: string): void;
6+
declare function get(): number;
7+
8+
function main1() : number {
9+
~~~~~~
10+
!!! error TS7030: Not all code paths return a value.
11+
try {
12+
return get();
13+
}
14+
catch(e) {
15+
log("in catch");
16+
}
17+
finally {
18+
log("in finally");
19+
}
20+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
//// [noImplicitReturnsWithProtectedBlocks2.ts]
2+
declare function log(s: string): void;
3+
declare function get(): number;
4+
5+
function main1() : number {
6+
try {
7+
return get();
8+
}
9+
catch(e) {
10+
log("in catch");
11+
}
12+
finally {
13+
log("in finally");
14+
}
15+
}
16+
17+
//// [noImplicitReturnsWithProtectedBlocks2.js]
18+
function main1() {
19+
try {
20+
return get();
21+
}
22+
catch (e) {
23+
log("in catch");
24+
}
25+
finally {
26+
log("in finally");
27+
}
28+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
tests/cases/compiler/noImplicitReturnsWithProtectedBlocks3.ts(4,20): error TS7030: Not all code paths return a value.
2+
3+
4+
==== tests/cases/compiler/noImplicitReturnsWithProtectedBlocks3.ts (1 errors) ====
5+
declare function log(s: string): void;
6+
declare function get(): number;
7+
8+
function main1() : number {
9+
~~~~~~
10+
!!! error TS7030: Not all code paths return a value.
11+
try {
12+
return get();
13+
}
14+
catch(e) {
15+
log("in catch");
16+
}
17+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
//// [noImplicitReturnsWithProtectedBlocks3.ts]
2+
declare function log(s: string): void;
3+
declare function get(): number;
4+
5+
function main1() : number {
6+
try {
7+
return get();
8+
}
9+
catch(e) {
10+
log("in catch");
11+
}
12+
}
13+
14+
//// [noImplicitReturnsWithProtectedBlocks3.js]
15+
function main1() {
16+
try {
17+
return get();
18+
}
19+
catch (e) {
20+
log("in catch");
21+
}
22+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// @noImplicitReturns: true
2+
declare function log(s: string): void;
3+
declare function get(): number;
4+
5+
function main1() : number {
6+
try {
7+
return get();
8+
}
9+
finally {
10+
log("in finally");
11+
}
12+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// @noImplicitReturns: true
2+
declare function log(s: string): void;
3+
declare function get(): number;
4+
5+
function main1() : number {
6+
try {
7+
return get();
8+
}
9+
catch(e) {
10+
log("in catch");
11+
}
12+
finally {
13+
log("in finally");
14+
}
15+
}

0 commit comments

Comments
 (0)