Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed an issue with ReduceLabel flows spoiling node reachability cache #61265

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 37 additions & 16 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1426,6 +1426,13 @@ const enum IntrinsicTypeKind {
NoInfer,
}

const enum SharedFlowNodeCacheFlags {
None = 0,
Read = 1 << 0,
Write = 1 << 1,
ReadWrite = Read | Write,
}

const intrinsicTypeKinds: ReadonlyMap<string, IntrinsicTypeKind> = new Map(Object.entries({
Uppercase: IntrinsicTypeKind.Uppercase,
Lowercase: IntrinsicTypeKind.Lowercase,
Expand Down Expand Up @@ -28104,7 +28111,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
}

function isReachableFlowNode(flow: FlowNode) {
const result = isReachableFlowNodeWorker(flow, /*noCacheCheck*/ false);
const result = isReachableFlowNodeWorker(flow, SharedFlowNodeCacheFlags.ReadWrite);
lastFlowNode = flow;
lastFlowNodeReachable = result;
return result;
Expand All @@ -28118,19 +28125,26 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
);
}

function isReachableFlowNodeWorker(flow: FlowNode, noCacheCheck: boolean): boolean {
function isReachableFlowNodeWorker(flow: FlowNode, cacheFlags: SharedFlowNodeCacheFlags): boolean {
while (true) {
if (flow === lastFlowNode) {
return lastFlowNodeReachable;
}
const flags = flow.flags;
if (flags & FlowFlags.Shared) {
if (!noCacheCheck) {
if (cacheFlags & SharedFlowNodeCacheFlags.Read) {
const id = getFlowNodeId(flow);
const reachable = flowNodeReachable[id];
return reachable !== undefined ? reachable : (flowNodeReachable[id] = isReachableFlowNodeWorker(flow, /*noCacheCheck*/ true));
let reachable = flowNodeReachable[id];
if (reachable !== undefined) {
return reachable;
}
reachable = isReachableFlowNodeWorker(flow, cacheFlags & ~SharedFlowNodeCacheFlags.Read);
if (cacheFlags & SharedFlowNodeCacheFlags.Write) {
flowNodeReachable[id] = reachable;
}
return reachable;
}
noCacheCheck = false;
cacheFlags |= SharedFlowNodeCacheFlags.Read;
}
if (flags & (FlowFlags.Assignment | FlowFlags.Condition | FlowFlags.ArrayMutation)) {
flow = (flow as FlowAssignment | FlowCondition | FlowArrayMutation).antecedent;
Expand All @@ -28153,7 +28167,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
}
else if (flags & FlowFlags.BranchLabel) {
// A branching point is reachable if any branch is reachable.
return some((flow as FlowLabel).antecedent, f => isReachableFlowNodeWorker(f, /*noCacheCheck*/ false));
return some((flow as FlowLabel).antecedent, f => isReachableFlowNodeWorker(f, cacheFlags));
}
else if (flags & FlowFlags.LoopLabel) {
const antecedents = (flow as FlowLabel).antecedent;
Expand All @@ -28178,7 +28192,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
const target = (flow as FlowReduceLabel).node.target;
const saveAntecedents = target.antecedent;
target.antecedent = (flow as FlowReduceLabel).node.antecedents;
const result = isReachableFlowNodeWorker((flow as FlowReduceLabel).antecedent, /*noCacheCheck*/ false);
const result = isReachableFlowNodeWorker((flow as FlowReduceLabel).antecedent, SharedFlowNodeCacheFlags.None);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

as the comment in this block says:

// Cache is unreliable once we start adjusting labels

We can't let this isReachableFlowNodeWorker call here to populate the cache as that would create entries based on the temporarily antecedent(s). Anything computed below this call can't be preserved, at least not without including the id of this ReduceLabel flow in the cache key or smth.

So this disables cache writes. As later on that would be used on the same shared flow but with "correct" antecedents (and that's the only result the compiler should cache and trust).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm still considering 2 things here:

  • should this code maybe save & restore flowNodeReachable[getFlowNodeId(target)]?
  • should reads be permanently disabled here? this would make the previous consideration obsolete

I have not been able to produce a test case that would prove either to be required so far but I have only spent a little bit of time on it.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cc @ahejlsberg who has been thinking about this sort of thing recently and I believe had ideas on how to simplify the flow node logic here

target.antecedent = saveAntecedents;
return result;
}
Expand All @@ -28190,16 +28204,23 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {

// Return true if the given flow node is preceded by a 'super(...)' call in every possible code path
// leading to the node.
function isPostSuperFlowNode(flow: FlowNode, noCacheCheck: boolean): boolean {
function isPostSuperFlowNode(flow: FlowNode, cacheFlags: SharedFlowNodeCacheFlags): boolean {
while (true) {
const flags = flow.flags;
if (flags & FlowFlags.Shared) {
if (!noCacheCheck) {
if (cacheFlags & SharedFlowNodeCacheFlags.Read) {
const id = getFlowNodeId(flow);
const postSuper = flowNodePostSuper[id];
return postSuper !== undefined ? postSuper : (flowNodePostSuper[id] = isPostSuperFlowNode(flow, /*noCacheCheck*/ true));
let postSuper = flowNodePostSuper[id];
if (postSuper !== undefined) {
return postSuper;
}
postSuper = isPostSuperFlowNode(flow, cacheFlags & ~SharedFlowNodeCacheFlags.Read);
if (cacheFlags & SharedFlowNodeCacheFlags.Write) {
flowNodePostSuper[id] = postSuper;
}
return postSuper;
}
noCacheCheck = false;
cacheFlags |= SharedFlowNodeCacheFlags.Read;
}
if (flags & (FlowFlags.Assignment | FlowFlags.Condition | FlowFlags.ArrayMutation | FlowFlags.SwitchClause)) {
flow = (flow as FlowAssignment | FlowCondition | FlowArrayMutation | FlowSwitchClause).antecedent;
Expand All @@ -28212,7 +28233,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
}
else if (flags & FlowFlags.BranchLabel) {
// A branching point is post-super if every branch is post-super.
return every((flow as FlowLabel).antecedent, f => isPostSuperFlowNode(f, /*noCacheCheck*/ false));
return every((flow as FlowLabel).antecedent, f => isPostSuperFlowNode(f, cacheFlags));
}
else if (flags & FlowFlags.LoopLabel) {
// A loop is post-super if the control flow path that leads to the top is post-super.
Expand All @@ -28222,7 +28243,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
const target = (flow as FlowReduceLabel).node.target;
const saveAntecedents = target.antecedent;
target.antecedent = (flow as FlowReduceLabel).node.antecedents;
const result = isPostSuperFlowNode((flow as FlowReduceLabel).antecedent, /*noCacheCheck*/ false);
const result = isPostSuperFlowNode((flow as FlowReduceLabel).antecedent, SharedFlowNodeCacheFlags.None);
target.antecedent = saveAntecedents;
return result;
}
Expand Down Expand Up @@ -30659,7 +30680,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
// If a containing class does not have extends clause or the class extends null
// skip checking whether super statement is called before "this" accessing.
if (baseTypeNode && !classDeclarationExtendsNull(containingClassDecl)) {
if (canHaveFlowNode(node) && node.flowNode && !isPostSuperFlowNode(node.flowNode, /*noCacheCheck*/ false)) {
if (canHaveFlowNode(node) && node.flowNode && !isPostSuperFlowNode(node.flowNode, SharedFlowNodeCacheFlags.ReadWrite)) {
error(node, diagnosticMessage);
}
}
Expand Down
113 changes: 113 additions & 0 deletions tests/baselines/reference/reachabilityChecks9.symbols
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
//// [tests/cases/compiler/reachabilityChecks9.ts] ////

=== reachabilityChecks9.ts ===
// https://github.com/microsoft/TypeScript/issues/61259

const a = (v: 1 | 2) => {
>a : Symbol(a, Decl(reachabilityChecks9.ts, 2, 5))
>v : Symbol(v, Decl(reachabilityChecks9.ts, 2, 11))

try {
switch (v) {
>v : Symbol(v, Decl(reachabilityChecks9.ts, 2, 11))

case 1:
return v;
>v : Symbol(v, Decl(reachabilityChecks9.ts, 2, 11))

case 2:
return v;
>v : Symbol(v, Decl(reachabilityChecks9.ts, 2, 11))
}
} finally {
console.log("exit");
>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, --, --))
}
};

const b = (v: number) => {
>b : Symbol(b, Decl(reachabilityChecks9.ts, 15, 5))
>v : Symbol(v, Decl(reachabilityChecks9.ts, 15, 11))

try {
switch (v) {
>v : Symbol(v, Decl(reachabilityChecks9.ts, 15, 11))

case 1:
return v;
>v : Symbol(v, Decl(reachabilityChecks9.ts, 15, 11))

default:
return v;
>v : Symbol(v, Decl(reachabilityChecks9.ts, 15, 11))
}
} finally {
console.log("exit");
>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, --, --))
}
};

const c = (v: 1 | 2) => {
>c : Symbol(c, Decl(reachabilityChecks9.ts, 28, 5))
>v : Symbol(v, Decl(reachabilityChecks9.ts, 28, 11))

try {
switch (v) {
>v : Symbol(v, Decl(reachabilityChecks9.ts, 28, 11))

case 1:
return v;
>v : Symbol(v, Decl(reachabilityChecks9.ts, 28, 11))

case 2:
return v;
>v : Symbol(v, Decl(reachabilityChecks9.ts, 28, 11))
}
} finally {
if (Math.random()) {
>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, --, --))

console.log("exit");
>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, --, --))
}
}
};

const d = (v: number) => {
>d : Symbol(d, Decl(reachabilityChecks9.ts, 43, 5))
>v : Symbol(v, Decl(reachabilityChecks9.ts, 43, 11))

try {
switch (v) {
>v : Symbol(v, Decl(reachabilityChecks9.ts, 43, 11))

case 1:
return v;
>v : Symbol(v, Decl(reachabilityChecks9.ts, 43, 11))

default:
return v;
>v : Symbol(v, Decl(reachabilityChecks9.ts, 43, 11))
}
} finally {
if (Math.random()) {
>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, --, --))

console.log("exit");
>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, --, --))
}
}
};

Loading