diff --git a/compiler/forget/packages/babel-plugin-react-forget/src/Optimization/ConstantPropagation.ts b/compiler/forget/packages/babel-plugin-react-forget/src/Optimization/ConstantPropagation.ts index 8b7691ee1d..cba7623a45 100644 --- a/compiler/forget/packages/babel-plugin-react-forget/src/Optimization/ConstantPropagation.ts +++ b/compiler/forget/packages/babel-plugin-react-forget/src/Optimization/ConstantPropagation.ts @@ -206,6 +206,19 @@ function evaluatePhi(phi: Phi, constants: Constants): Constant | null { } break; } + case "LoadGlobal": { + CompilerError.invariant(value.kind === "LoadGlobal", { + reason: "value kind expected to be LoadGlobal", + loc: null, + suggestions: null, + }); + + // different global values, can't constant propogate + if (operandValue.name !== value.name) { + return null; + } + break; + } default: return null; } diff --git a/compiler/forget/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/constant-propagate-global-phis-constant.expect.md b/compiler/forget/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/constant-propagate-global-phis-constant.expect.md new file mode 100644 index 0000000000..5f8201c8f2 --- /dev/null +++ b/compiler/forget/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/constant-propagate-global-phis-constant.expect.md @@ -0,0 +1,33 @@ + +## Input + +```javascript +function Test() { + const { tab } = useFoo(); + const currentTab = tab === WAT ? WAT : WAT; + + return ; +} + +``` + +## Code + +```javascript +import { unstable_useMemoCache as useMemoCache } from "react"; +function Test() { + const $ = useMemoCache(1); + const { tab } = useFoo(); + tab === WAT ? WAT : WAT; + let t0; + if ($[0] === Symbol.for("react.memo_cache_sentinel")) { + t0 = ; + $[0] = t0; + } else { + t0 = $[0]; + } + return t0; +} + +``` + \ No newline at end of file diff --git a/compiler/forget/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/constant-propagate-global-phis-constant.js b/compiler/forget/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/constant-propagate-global-phis-constant.js new file mode 100644 index 0000000000..a9b0384026 --- /dev/null +++ b/compiler/forget/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/constant-propagate-global-phis-constant.js @@ -0,0 +1,6 @@ +function Test() { + const { tab } = useFoo(); + const currentTab = tab === WAT ? WAT : WAT; + + return ; +}