Skip to content

Commit 03123fe

Browse files
authored
Add try priors as finally lock label andecedents rather than pre finally label antecedents (#29790) (#29887)
1 parent 2312c09 commit 03123fe

File tree

5 files changed

+138
-2
lines changed

5 files changed

+138
-2
lines changed

src/compiler/binder.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1120,11 +1120,15 @@ namespace ts {
11201120
// We add the nodes within the `try` block to the `finally`'s antecedents if there's no catch block
11211121
// (If there is a `catch` block, it will have all these antecedents instead, and the `finally` will
11221122
// have the end of the `try` block and the end of the `catch` block)
1123+
let preFinallyPrior = preTryFlow;
11231124
if (!node.catchClause) {
11241125
if (tryPriors.length) {
1126+
const preFinallyFlow = createBranchLabel();
1127+
addAntecedent(preFinallyFlow, preTryFlow);
11251128
for (const p of tryPriors) {
1126-
addAntecedent(preFinallyLabel, p);
1129+
addAntecedent(preFinallyFlow, p);
11271130
}
1131+
preFinallyPrior = finishFlowLabel(preFinallyFlow);
11281132
}
11291133
}
11301134

@@ -1156,7 +1160,7 @@ namespace ts {
11561160
//
11571161
// extra edges that we inject allows to control this behavior
11581162
// if when walking the flow we step on post-finally edge - we can mark matching pre-finally edge as locked so it will be skipped.
1159-
const preFinallyFlow: PreFinallyFlow = { flags: FlowFlags.PreFinally, antecedent: preTryFlow, lock: {} };
1163+
const preFinallyFlow: PreFinallyFlow = { flags: FlowFlags.PreFinally, antecedent: preFinallyPrior, lock: {} };
11601164
addAntecedent(preFinallyLabel, preFinallyFlow);
11611165

11621166
currentFlow = finishFlowLabel(preFinallyLabel);
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
//// [controlFlowFinallyNoCatchAssignments.ts]
2+
let x: number;
3+
x = Math.random();
4+
let a: number;
5+
try {
6+
if (x) {
7+
a = 1;
8+
} else {
9+
a = 2;
10+
}
11+
} finally {
12+
console.log(x);
13+
}
14+
15+
console.log(a); // <- error here
16+
17+
//// [controlFlowFinallyNoCatchAssignments.js]
18+
"use strict";
19+
var x;
20+
x = Math.random();
21+
var a;
22+
try {
23+
if (x) {
24+
a = 1;
25+
}
26+
else {
27+
a = 2;
28+
}
29+
}
30+
finally {
31+
console.log(x);
32+
}
33+
console.log(a); // <- error here
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
=== tests/cases/compiler/controlFlowFinallyNoCatchAssignments.ts ===
2+
let x: number;
3+
>x : Symbol(x, Decl(controlFlowFinallyNoCatchAssignments.ts, 0, 3))
4+
5+
x = Math.random();
6+
>x : Symbol(x, Decl(controlFlowFinallyNoCatchAssignments.ts, 0, 3))
7+
>Math.random : Symbol(Math.random, Decl(lib.es5.d.ts, --, --))
8+
>Math : Symbol(Math, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --))
9+
>random : Symbol(Math.random, Decl(lib.es5.d.ts, --, --))
10+
11+
let a: number;
12+
>a : Symbol(a, Decl(controlFlowFinallyNoCatchAssignments.ts, 2, 3))
13+
14+
try {
15+
if (x) {
16+
>x : Symbol(x, Decl(controlFlowFinallyNoCatchAssignments.ts, 0, 3))
17+
18+
a = 1;
19+
>a : Symbol(a, Decl(controlFlowFinallyNoCatchAssignments.ts, 2, 3))
20+
21+
} else {
22+
a = 2;
23+
>a : Symbol(a, Decl(controlFlowFinallyNoCatchAssignments.ts, 2, 3))
24+
}
25+
} finally {
26+
console.log(x);
27+
>console.log : Symbol(Console.log, Decl(lib.dom.d.ts, --, --))
28+
>console : Symbol(console, Decl(lib.dom.d.ts, --, --))
29+
>log : Symbol(Console.log, Decl(lib.dom.d.ts, --, --))
30+
>x : Symbol(x, Decl(controlFlowFinallyNoCatchAssignments.ts, 0, 3))
31+
}
32+
33+
console.log(a); // <- error here
34+
>console.log : Symbol(Console.log, Decl(lib.dom.d.ts, --, --))
35+
>console : Symbol(console, Decl(lib.dom.d.ts, --, --))
36+
>log : Symbol(Console.log, Decl(lib.dom.d.ts, --, --))
37+
>a : Symbol(a, Decl(controlFlowFinallyNoCatchAssignments.ts, 2, 3))
38+
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
=== tests/cases/compiler/controlFlowFinallyNoCatchAssignments.ts ===
2+
let x: number;
3+
>x : number
4+
5+
x = Math.random();
6+
>x = Math.random() : number
7+
>x : number
8+
>Math.random() : number
9+
>Math.random : () => number
10+
>Math : Math
11+
>random : () => number
12+
13+
let a: number;
14+
>a : number
15+
16+
try {
17+
if (x) {
18+
>x : number
19+
20+
a = 1;
21+
>a = 1 : 1
22+
>a : number
23+
>1 : 1
24+
25+
} else {
26+
a = 2;
27+
>a = 2 : 2
28+
>a : number
29+
>2 : 2
30+
}
31+
} finally {
32+
console.log(x);
33+
>console.log(x) : void
34+
>console.log : (message?: any, ...optionalParams: any[]) => void
35+
>console : Console
36+
>log : (message?: any, ...optionalParams: any[]) => void
37+
>x : number
38+
}
39+
40+
console.log(a); // <- error here
41+
>console.log(a) : void
42+
>console.log : (message?: any, ...optionalParams: any[]) => void
43+
>console : Console
44+
>log : (message?: any, ...optionalParams: any[]) => void
45+
>a : number
46+
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// @strict: true
2+
let x: number;
3+
x = Math.random();
4+
let a: number;
5+
try {
6+
if (x) {
7+
a = 1;
8+
} else {
9+
a = 2;
10+
}
11+
} finally {
12+
console.log(x);
13+
}
14+
15+
console.log(a); // <- error here

0 commit comments

Comments
 (0)