Skip to content

Commit

Permalink
Add try priors as finally lock label andecedents rather than pre fina…
Browse files Browse the repository at this point in the history
…lly label antecedents (#29790) (#29887)
  • Loading branch information
weswigham committed Feb 13, 2019
1 parent 2312c09 commit 03123fe
Show file tree
Hide file tree
Showing 5 changed files with 138 additions and 2 deletions.
8 changes: 6 additions & 2 deletions src/compiler/binder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1120,11 +1120,15 @@ namespace ts {
// We add the nodes within the `try` block to the `finally`'s antecedents if there's no catch block
// (If there is a `catch` block, it will have all these antecedents instead, and the `finally` will
// have the end of the `try` block and the end of the `catch` block)
let preFinallyPrior = preTryFlow;
if (!node.catchClause) {
if (tryPriors.length) {
const preFinallyFlow = createBranchLabel();
addAntecedent(preFinallyFlow, preTryFlow);
for (const p of tryPriors) {
addAntecedent(preFinallyLabel, p);
addAntecedent(preFinallyFlow, p);
}
preFinallyPrior = finishFlowLabel(preFinallyFlow);
}
}

Expand Down Expand Up @@ -1156,7 +1160,7 @@ namespace ts {
//
// extra edges that we inject allows to control this behavior
// 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.
const preFinallyFlow: PreFinallyFlow = { flags: FlowFlags.PreFinally, antecedent: preTryFlow, lock: {} };
const preFinallyFlow: PreFinallyFlow = { flags: FlowFlags.PreFinally, antecedent: preFinallyPrior, lock: {} };
addAntecedent(preFinallyLabel, preFinallyFlow);

currentFlow = finishFlowLabel(preFinallyLabel);
Expand Down
33 changes: 33 additions & 0 deletions tests/baselines/reference/controlFlowFinallyNoCatchAssignments.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
//// [controlFlowFinallyNoCatchAssignments.ts]
let x: number;
x = Math.random();
let a: number;
try {
if (x) {
a = 1;
} else {
a = 2;
}
} finally {
console.log(x);
}

console.log(a); // <- error here

//// [controlFlowFinallyNoCatchAssignments.js]
"use strict";
var x;
x = Math.random();
var a;
try {
if (x) {
a = 1;
}
else {
a = 2;
}
}
finally {
console.log(x);
}
console.log(a); // <- error here
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
=== tests/cases/compiler/controlFlowFinallyNoCatchAssignments.ts ===
let x: number;
>x : Symbol(x, Decl(controlFlowFinallyNoCatchAssignments.ts, 0, 3))

x = Math.random();
>x : Symbol(x, Decl(controlFlowFinallyNoCatchAssignments.ts, 0, 3))
>Math.random : Symbol(Math.random, Decl(lib.es5.d.ts, --, --))
>Math : Symbol(Math, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --))
>random : Symbol(Math.random, Decl(lib.es5.d.ts, --, --))

let a: number;
>a : Symbol(a, Decl(controlFlowFinallyNoCatchAssignments.ts, 2, 3))

try {
if (x) {
>x : Symbol(x, Decl(controlFlowFinallyNoCatchAssignments.ts, 0, 3))

a = 1;
>a : Symbol(a, Decl(controlFlowFinallyNoCatchAssignments.ts, 2, 3))

} else {
a = 2;
>a : Symbol(a, Decl(controlFlowFinallyNoCatchAssignments.ts, 2, 3))
}
} finally {
console.log(x);
>console.log : Symbol(Console.log, Decl(lib.dom.d.ts, --, --))
>console : Symbol(console, Decl(lib.dom.d.ts, --, --))
>log : Symbol(Console.log, Decl(lib.dom.d.ts, --, --))
>x : Symbol(x, Decl(controlFlowFinallyNoCatchAssignments.ts, 0, 3))
}

console.log(a); // <- error here
>console.log : Symbol(Console.log, Decl(lib.dom.d.ts, --, --))
>console : Symbol(console, Decl(lib.dom.d.ts, --, --))
>log : Symbol(Console.log, Decl(lib.dom.d.ts, --, --))
>a : Symbol(a, Decl(controlFlowFinallyNoCatchAssignments.ts, 2, 3))

Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
=== tests/cases/compiler/controlFlowFinallyNoCatchAssignments.ts ===
let x: number;
>x : number

x = Math.random();
>x = Math.random() : number
>x : number
>Math.random() : number
>Math.random : () => number
>Math : Math
>random : () => number

let a: number;
>a : number

try {
if (x) {
>x : number

a = 1;
>a = 1 : 1
>a : number
>1 : 1

} else {
a = 2;
>a = 2 : 2
>a : number
>2 : 2
}
} finally {
console.log(x);
>console.log(x) : void
>console.log : (message?: any, ...optionalParams: any[]) => void
>console : Console
>log : (message?: any, ...optionalParams: any[]) => void
>x : number
}

console.log(a); // <- error here
>console.log(a) : void
>console.log : (message?: any, ...optionalParams: any[]) => void
>console : Console
>log : (message?: any, ...optionalParams: any[]) => void
>a : number

15 changes: 15 additions & 0 deletions tests/cases/compiler/controlFlowFinallyNoCatchAssignments.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// @strict: true
let x: number;
x = Math.random();
let a: number;
try {
if (x) {
a = 1;
} else {
a = 2;
}
} finally {
console.log(x);
}

console.log(a); // <- error here

0 comments on commit 03123fe

Please sign in to comment.