Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[compiler] Count loop reassignments as the enclosing scope reassigning the variable#36732

Merged
poteto merged 2 commits into
react:mainfrom
poteto:lauren/fix-34971-loop-counter
Jun 17, 2026
Merged

[compiler] Count loop reassignments as the enclosing scope reassigning the variable#36732
poteto merged 2 commits into
react:mainfrom
poteto:lauren/fix-34971-loop-counter

Conversation

@poteto

@poteto poteto commented Jun 10, 2026

Copy link
Copy Markdown
Collaborator

A counter initialized before a memo scope and incremented inside it (a++ or a = a + 1 in a for body) was emitted as a scope dependency, compared at its pre-loop value (constant every render) while the cache stored its post-loop value. The memo could never hit, so the scope recomputed on every render.

Root cause: the phi-union rule in InferReactiveScopeVariables.findDisjointMutableValues only unioned a phi into the scope when the phi value was mutated after creation, which range-extension only does for object mutation. Primitive reassignments around a loop back-edge never extend ranges, so the counter's SSA versions stayed outside the scope and downstream dependency propagation classified the pre-loop read as a dep.

The fix unions a phi with its operands and declaration when any operand is defined at or after the phi's block, i.e. the value is reassigned around a loop back-edge. This matches the shape the compiler already produced for non-primitive loop reassignment (x = [...x, i]). Implemented identically in the TypeScript compiler and the Rust port.

Both a++ and a = a + 1 variants are pinned by fixtures; the first commit documents the previously-wrong codegen, the second fixes it (counter becomes a scope output, dep on count only).

Corpus delta beyond the new fixtures is 4 fixtures, all strict improvements with byte-identical eval output: for-in-statement-break, for-in-statement-continue, for-in-statement-type-inference (loops previously re-ran every render, now memoized), and sequence-expression (two memo blocks collapse into one).

Known limitation, unchanged from before: conditional reassignment in a loop (for (...) { if (c) a++; }) routes through a join phi that this rule cannot see; that shape behaves as it did before this change.

Verification: TS snap 1806/1806, Rust snap 1806/1806, cargo workspace green, scoped TS-vs-Rust HIR parity harness green.

Closes #34971

@meta-cla meta-cla Bot added the CLA Signed label Jun 10, 2026
@poteto poteto marked this pull request as ready for review June 10, 2026 06:13
function sequence(props) {
const $ = _c(2);
let t0;
const $ = _c(1);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

makes sense that we should produce a single memo for x, merging the reassignment

@mofeiZ mofeiZ left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Ran this on 100k+ files internally to validate there are no unintended side effects. Looks great, let's land this

poteto added 2 commits June 17, 2026 00:36
…f output

A counter declared before a reactive scope and incremented inside the
scope's loop is currently treated as a scope dependency: the memo block
compares the counter at its pre-loop value (constant every render) but
stores its post-loop value, so the comparison never matches and the
scope re-executes on every render. The counter is reassigned by the
scope, so it should instead be one of the scope's outputs. Repro for
react#34971; the expect file documents today's incorrect
output.
…g the variable

A variable declared before a reactive scope and reassigned inside the
scope's loop (a counter incremented via `a++` or `a = a + 1`) was
emitted as a scope dependency: the memo block compared the variable at
its pre-loop value but cached its post-loop value, so the comparison
could never match and the scope re-executed on every render. The root
cause is in InferReactiveScopeVariables: the phi-union rule only fired
for phis whose value is mutated after creation, which never holds for
primitive reassignments, so the counter's SSA versions were never
unioned into a scope. Fix by also unioning a phi with its operands and
declaration when an operand is defined at or after the phi's block,
i.e. when the value is reassigned around a loop back-edge; the variable
then becomes a value produced by the scope rather than a dependency.
The same rule is mirrored in the Rust port's
infer_reactive_scope_variables pass. Fixes react#34971.

Updated for-in/sequence-expression fixtures pick up the same shape:
values reassigned in loops become memoized scope outputs instead of
recomputing on every render; eval outputs are unchanged.
@poteto poteto force-pushed the lauren/fix-34971-loop-counter branch from 0144c65 to f54f582 Compare June 17, 2026 07:45
@poteto poteto requested a review from mofeiZ June 17, 2026 07:49
@poteto

poteto commented Jun 17, 2026

Copy link
Copy Markdown
Collaborator Author

sorry, needed to rebase

@poteto poteto merged commit 0ca9d20 into react:main Jun 17, 2026
18 of 19 checks passed
@poteto poteto deleted the lauren/fix-34971-loop-counter branch June 17, 2026 22:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Compiler Bug]: Memoization is not correctly applied around for loops

2 participants