Reviewer · Algorithms
Analysis algorithm correctness
When implementing iterative/recursive analysis algorithms (fixpoint dataflow passes, lattice merges, escape analysis, tree traversals), ensure every case is handled exhaustively, in the right order, and with correct boundary conditions:
When implementing iterative/recursive analysis algorithms (fixpoint dataflow passes, lattice merges, escape analysis, tree traversals), ensure every case is handled exhaustively, in the right order, and with correct boundary conditions:
- Preserve the correct processing order in fixpoint/dataflow loops. For example, phi nodes must be recorded before the block’s instructions are processed, since later instructions depend on values the phis introduce (e.g.
x@2 = phi(x@0, x@1)must exist in context beforedoSomething(x@2)is analyzed). Don’t reorder these steps casually, even for debugging. - When adding a new case to a value/lattice kind (e.g.
ValueKind.ShallowMutable), update all places that join or merge kinds (mergeValueKinds, mutation-effect inference, etc.) so the new kind composes correctly with existing kinds, and add targeted tests that exercise the new interaction (e.g. a conditional assigning a shallow-mutable value in one branch and a fully-mutable value in another) to catch missed merge cases. - When checking scope/escape conditions (e.g. whether a variable is an “outer scope” reference), check against the actual function/closure context rather than assuming based on instruction kind alone—constructs like
LoadContextcan represent reassigned variables in nested closures, not just external captures. - Make recursive checks genuinely recursive: if a node type is reorderable/safe only when its sub-expressions are too (e.g.
TSInstantiationExpression), recurse into those sub-expressions rather than returningtruebased on the outer node type alone. - Watch for incidental complexity blowups, such as allocating a new
Setor similar structure inside a hot loop; cache or hoist such structures to avoid O(n²) behavior, while balancing against premature optimization for rarely-hit error paths.
Example of an incomplete recursive check that was fixed by adding recursion:
case 'TSInstantiationExpression': {
// must recurse into the inner expression, not just return true
return isReorderableExpression(expr.expression);
}
Treat these algorithms as requiring case-by-case reasoning about ordering, recursion depth, and lattice composition, and back every nontrivial branch with a regression test.