diff --git a/compiler/forget/packages/babel-plugin-react-forget/src/ReactiveScopes/PropagateScopeDependencies.ts b/compiler/forget/packages/babel-plugin-react-forget/src/ReactiveScopes/PropagateScopeDependencies.ts index 842fd68101..311ff3829e 100644 --- a/compiler/forget/packages/babel-plugin-react-forget/src/ReactiveScopes/PropagateScopeDependencies.ts +++ b/compiler/forget/packages/babel-plugin-react-forget/src/ReactiveScopes/PropagateScopeDependencies.ts @@ -419,15 +419,12 @@ class Context { * current one as a {@link ReactiveScope.reassignments} */ visitReassignment(place: Place): void { - const declaration = this.#declarations.get(place.identifier.id); if ( this.currentScope.value != null && - place.identifier.scope != null && - declaration !== undefined && - declaration.scope.value !== place.identifier.scope && !Array.from(this.currentScope.value.reassignments).some( - (ident) => ident.id === place.identifier.id - ) + (identifier) => identifier.id === place.identifier.id + ) && + this.#checkValidDependency({ identifier: place.identifier, path: [] }) ) { this.currentScope.value.reassignments.add(place.identifier); } diff --git a/compiler/forget/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/repro-reassign-to-variable-without-mutable-range.expect.md b/compiler/forget/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/repro-reassign-to-variable-without-mutable-range.expect.md new file mode 100644 index 0000000000..a5cc1f8f16 --- /dev/null +++ b/compiler/forget/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/repro-reassign-to-variable-without-mutable-range.expect.md @@ -0,0 +1,81 @@ + +## Input + +```javascript +// @debug +function Component(a, b) { + let x = []; + let y = []; + let z = foo(a); + if (FLAG) { + x = bar(z); + y = baz(b); + } + return [x, y]; +} + +``` + +## Code + +```javascript +import { unstable_useMemoCache as useMemoCache } from "react"; // @debug +function Component(a, b) { + const $ = useMemoCache(11); + let t0; + if ($[0] === Symbol.for("react.memo_cache_sentinel")) { + t0 = []; + $[0] = t0; + } else { + t0 = $[0]; + } + let x = t0; + let t1; + if ($[1] === Symbol.for("react.memo_cache_sentinel")) { + t1 = []; + $[1] = t1; + } else { + t1 = $[1]; + } + let y = t1; + const c_2 = $[2] !== a; + const c_3 = $[3] !== b; + if (c_2 || c_3) { + const z = foo(a); + if (FLAG) { + x = bar(z); + const c_6 = $[6] !== b; + let t2; + if (c_6) { + t2 = baz(b); + $[6] = b; + $[7] = t2; + } else { + t2 = $[7]; + } + y = t2; + } + $[2] = a; + $[3] = b; + $[4] = x; + $[5] = y; + } else { + x = $[4]; + y = $[5]; + } + const c_8 = $[8] !== x; + const c_9 = $[9] !== y; + let t3; + if (c_8 || c_9) { + t3 = [x, y]; + $[8] = x; + $[9] = y; + $[10] = t3; + } else { + t3 = $[10]; + } + return t3; +} + +``` + \ No newline at end of file diff --git a/compiler/forget/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/repro-reassign-to-variable-without-mutable-range.js b/compiler/forget/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/repro-reassign-to-variable-without-mutable-range.js new file mode 100644 index 0000000000..2ffd6efb9f --- /dev/null +++ b/compiler/forget/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/repro-reassign-to-variable-without-mutable-range.js @@ -0,0 +1,11 @@ +// @debug +function Component(a, b) { + let x = []; + let y = []; + let z = foo(a); + if (FLAG) { + x = bar(z); + y = baz(b); + } + return [x, y]; +} diff --git a/compiler/forget/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/ssa-cascading-eliminated-phis.expect.md b/compiler/forget/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/ssa-cascading-eliminated-phis.expect.md index efbe528b59..d6a9f2b5a8 100644 --- a/compiler/forget/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/ssa-cascading-eliminated-phis.expect.md +++ b/compiler/forget/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/ssa-cascading-eliminated-phis.expect.md @@ -25,7 +25,7 @@ function Component(props) { ```javascript import { unstable_useMemoCache as useMemoCache } from "react"; function Component(props) { - const $ = useMemoCache(2); + const $ = useMemoCache(3); let x = 0; const c_0 = $[0] !== props; let values; @@ -45,8 +45,10 @@ function Component(props) { values.push(x); $[0] = props; $[1] = values; + $[2] = x; } else { values = $[1]; + x = $[2]; } return values; }