diff --git a/compiler/forget/packages/babel-plugin-react-forget/src/Inference/AnalyseFunctions.ts b/compiler/forget/packages/babel-plugin-react-forget/src/Inference/AnalyseFunctions.ts index bbfdb796cb..c40fc5fee2 100644 --- a/compiler/forget/packages/babel-plugin-react-forget/src/Inference/AnalyseFunctions.ts +++ b/compiler/forget/packages/babel-plugin-react-forget/src/Inference/AnalyseFunctions.ts @@ -17,7 +17,7 @@ import { Place, ReactiveScopeDependency, } from "../HIR"; -import { constantPropagation } from "../Optimization"; +import { constantPropagation, deadCodeElimination } from "../Optimization"; import { inferReactiveScopeVariables } from "../ReactiveScopes"; import { eliminateRedundantPhi, enterSSA, leaveSSA } from "../SSA"; import { inferTypes } from "../TypeInference"; @@ -113,6 +113,7 @@ function lower(func: HIRFunction): void { analyseFunctions(func); inferReferenceEffects(func, { isFunctionExpression: true }); + deadCodeElimination(func); inferMutableRanges(func); leaveSSA(func); inferReactiveScopeVariables(func); @@ -133,6 +134,7 @@ function infer( ) { mutations.set(operand.identifier.name, operand.effect); } + operand.identifier.mutableRange.end = operand.identifier.mutableRange.start; } for (const dep of value.dependencies) { diff --git a/compiler/forget/packages/babel-plugin-react-forget/src/ReactiveScopes/CodegenReactiveFunction.ts b/compiler/forget/packages/babel-plugin-react-forget/src/ReactiveScopes/CodegenReactiveFunction.ts index a9a495d780..257565715a 100644 --- a/compiler/forget/packages/babel-plugin-react-forget/src/ReactiveScopes/CodegenReactiveFunction.ts +++ b/compiler/forget/packages/babel-plugin-react-forget/src/ReactiveScopes/CodegenReactiveFunction.ts @@ -31,7 +31,6 @@ import { } from "../HIR/HIR"; import { printPlace } from "../HIR/PrintHIR"; import { eachPatternOperand } from "../HIR/visitors"; -import { deadCodeElimination } from "../Optimization"; import { Err, Ok, Result } from "../Utils/Result"; import { assertExhaustive } from "../Utils/utils"; import { buildReactiveFunction } from "./BuildReactiveFunction"; @@ -962,7 +961,6 @@ function codegenInstructionValue( case "FunctionExpression": { if (cx.env.enableOptimizeFunctionExpressions) { const loweredFunc = instrValue.loweredFunc; - deadCodeElimination(loweredFunc); const reactiveFunction = buildReactiveFunction(loweredFunc); pruneUnusedLabels(reactiveFunction); pruneUnusedLValues(reactiveFunction); diff --git a/compiler/forget/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/reassigned-phi-in-returned-function-expression.expect.md b/compiler/forget/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/reassigned-phi-in-returned-function-expression.expect.md new file mode 100644 index 0000000000..13e811b62f --- /dev/null +++ b/compiler/forget/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/reassigned-phi-in-returned-function-expression.expect.md @@ -0,0 +1,48 @@ + +## Input + +```javascript +// @enableOptimizeFunctionExpressions +function Component(props) { + return () => { + let str; + if (arguments.length) { + str = arguments[0]; + } else { + str = props.str; + } + global.log(str); + }; +} + +``` + +## Code + +```javascript +import { unstable_useMemoCache as useMemoCache } from "react"; // @enableOptimizeFunctionExpressions +function Component(props) { + const $ = useMemoCache(2); + const c_0 = $[0] !== props.str; + let t0; + if (c_0) { + t0 = () => { + let str = undefined; + if (arguments.length) { + str = arguments[0]; + } else { + str = props.str; + } + + global.log(str); + }; + $[0] = props.str; + $[1] = t0; + } else { + t0 = $[1]; + } + return t0; +} + +``` + \ No newline at end of file diff --git a/compiler/forget/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/reassigned-phi-in-returned-function-expression.js b/compiler/forget/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/reassigned-phi-in-returned-function-expression.js new file mode 100644 index 0000000000..dcf5ecc79c --- /dev/null +++ b/compiler/forget/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/reassigned-phi-in-returned-function-expression.js @@ -0,0 +1,12 @@ +// @enableOptimizeFunctionExpressions +function Component(props) { + return () => { + let str; + if (arguments.length) { + str = arguments[0]; + } else { + str = props.str; + } + global.log(str); + }; +}