fix(compiler): preserve runNumber across StoreGlobal mutations (#34899) #35469
+94
−7
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Summary
This PR fixes a React Compiler bug where a local variable capturing a global value was incorrectly optimized away when the global was later mutated using a compound assignment (e.g. +=) inside a useEffect.
In this scenario, the compiler treated the captured local variable as a constant LoadGlobal and replaced it with direct references to the global variable. This caused cleanup functions to observe the mutated value instead of the originally captured one, resulting in incorrect behavior in StrictMode.
This change preserves correct JavaScript closure semantics by ensuring captured values remain stable across effect and cleanup boundaries.
Closes #34899
What was happening
Given code like:
The compiler optimized away runNumber and replaced it with i, causing the cleanup to log 1 instead of the expected 0.
Root cause
The constant propagation pass did not handle StoreGlobal instructions generated by compound assignments (+=, -=, etc.). When a global was mutated, constants derived from that global were not invalidated, allowing the optimizer to incorrectly eliminate captured local variables.
Fix
This PR adds explicit handling for StoreGlobal instructions in the constant propagation pass:
This prevents captured locals from being optimized away after a global mutation.
Changes made
How did I test this change?
+=inuseEffect#34899Expected behavior after fix
Files changed