Skip to content

Commit 8cbf694

Browse files
committed
Cache last isReachableFlowNode result + switch statement CFA fix
1 parent 282a7af commit 8cbf694

File tree

1 file changed

+12
-4
lines changed

1 file changed

+12
-4
lines changed

src/compiler/checker.ts

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -820,6 +820,8 @@ namespace ts {
820820
let flowLoopCount = 0;
821821
let sharedFlowCount = 0;
822822
let flowAnalysisDisabled = false;
823+
let lastFlowNode: FlowNode | undefined;
824+
let lastFlowNodeReachable: boolean;
823825

824826
const emptyStringType = getLiteralType("");
825827
const zeroType = getLiteralType(0);
@@ -16995,11 +16997,17 @@ namespace ts {
1699516997
}
1699616998

1699716999
function isReachableFlowNode(flow: FlowNode) {
16998-
return isReachableFlowNodeWorker(flow, /*skipCacheCheck*/ false);
17000+
const result = isReachableFlowNodeWorker(flow, /*skipCacheCheck*/ false);
17001+
lastFlowNode = flow;
17002+
lastFlowNodeReachable = result;
17003+
return result;
1699917004
}
1700017005

1700117006
function isReachableFlowNodeWorker(flow: FlowNode, noCacheCheck: boolean): boolean {
1700217007
while (true) {
17008+
if (flow === lastFlowNode) {
17009+
return lastFlowNodeReachable;
17010+
}
1700317011
const flags = flow.flags;
1700417012
if (flags & FlowFlags.Shared) {
1700517013
if (!noCacheCheck) {
@@ -17329,9 +17337,6 @@ namespace ts {
1732917337
}
1733017338

1733117339
function getTypeAtSwitchClause(flow: FlowSwitchClause): FlowType {
17332-
if (flow.clauseStart === flow.clauseEnd && isExhaustiveSwitchStatement(flow.switchStatement)) {
17333-
return neverType;
17334-
}
1733517340
const expr = flow.switchStatement.expression;
1733617341
const flowType = getTypeAtFlowNode(flow.antecedent);
1733717342
let type = getTypeFromFlowType(flowType);
@@ -17350,6 +17355,9 @@ namespace ts {
1735017355
else if (containsMatchingReferenceDiscriminant(reference, expr)) {
1735117356
type = declaredType;
1735217357
}
17358+
else if (flow.clauseStart === flow.clauseEnd && isExhaustiveSwitchStatement(flow.switchStatement)) {
17359+
return unreachableNeverType;
17360+
}
1735317361
return createFlowType(type, isIncomplete(flowType));
1735417362
}
1735517363

0 commit comments

Comments
 (0)