diff --git a/compiler/packages/babel-plugin-react-compiler/src/ReactiveScopes/InferReactiveScopeVariables.ts b/compiler/packages/babel-plugin-react-compiler/src/ReactiveScopes/InferReactiveScopeVariables.ts index 2c9e67646b155..3e344d1bfa34c 100644 --- a/compiler/packages/babel-plugin-react-compiler/src/ReactiveScopes/InferReactiveScopeVariables.ts +++ b/compiler/packages/babel-plugin-react-compiler/src/ReactiveScopes/InferReactiveScopeVariables.ts @@ -114,9 +114,13 @@ export function inferReactiveScopeVariables(fn: HIRFunction): void { }; scopes.set(groupIdentifier, scope); } else { - scope.range.start = makeInstructionId( - Math.min(scope.range.start, identifier.mutableRange.start) - ); + if (scope.range.start === 0) { + scope.range.start = identifier.mutableRange.start; + } else if (identifier.mutableRange.start !== 0) { + scope.range.start = makeInstructionId( + Math.min(scope.range.start, identifier.mutableRange.start) + ); + } scope.range.end = makeInstructionId( Math.max(scope.range.end, identifier.mutableRange.end) ); diff --git a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/primitive-reassigned-loop-force-scopes-enabled.expect.md b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/primitive-reassigned-loop-force-scopes-enabled.expect.md new file mode 100644 index 0000000000000..a40c7452bb973 --- /dev/null +++ b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/primitive-reassigned-loop-force-scopes-enabled.expect.md @@ -0,0 +1,68 @@ + +## Input + +```javascript +// @enableForest +function Component({ base, start, increment, test }) { + let value = base; + for (let i = start; i < test; i += increment) { + value += i; + } + return
{value}
; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{ base: 0, start: 0, test: 10, increment: 1 }], + sequentialRenders: [ + { base: 0, start: 1, test: 10, increment: 1 }, + { base: 0, start: 0, test: 10, increment: 2 }, + { base: 2, start: 0, test: 10, increment: 2 }, + { base: 0, start: 0, test: 11, increment: 2 }, + ], +}; + +``` + +## Code + +```javascript +import { c as _c } from "react/compiler-runtime"; // @enableForest +function Component(t0) { + const $ = _c(5); + const { base, start, increment, test } = t0; + let value; + if ($[0] !== base || $[1] !== start || $[2] !== test || $[3] !== increment) { + value = base; + for (let i = start; i < test; i = i + increment, i) { + value = value + i; + } + $[0] = base; + $[1] = start; + $[2] = test; + $[3] = increment; + $[4] = value; + } else { + value = $[4]; + } + return
{value}
; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{ base: 0, start: 0, test: 10, increment: 1 }], + sequentialRenders: [ + { base: 0, start: 1, test: 10, increment: 1 }, + { base: 0, start: 0, test: 10, increment: 2 }, + { base: 2, start: 0, test: 10, increment: 2 }, + { base: 0, start: 0, test: 11, increment: 2 }, + ], +}; + +``` + +### Eval output +(kind: ok)
45
+
20
+
22
+
30
\ No newline at end of file diff --git a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/primitive-reassigned-loop-force-scopes-enabled.js b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/primitive-reassigned-loop-force-scopes-enabled.js new file mode 100644 index 0000000000000..2cbf8ee58a8dd --- /dev/null +++ b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/primitive-reassigned-loop-force-scopes-enabled.js @@ -0,0 +1,19 @@ +// @enableForest +function Component({ base, start, increment, test }) { + let value = base; + for (let i = start; i < test; i += increment) { + value += i; + } + return
{value}
; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{ base: 0, start: 0, test: 10, increment: 1 }], + sequentialRenders: [ + { base: 0, start: 1, test: 10, increment: 1 }, + { base: 0, start: 0, test: 10, increment: 2 }, + { base: 2, start: 0, test: 10, increment: 2 }, + { base: 0, start: 0, test: 11, increment: 2 }, + ], +};