From f8cee28f1d7c8da8522974dd3e61b7fc0815c85d Mon Sep 17 00:00:00 2001 From: Joe Savona Date: Thu, 19 Oct 2023 22:08:15 +0100 Subject: [PATCH] Retain minimal variable declarations in DCE Currently DCE can remove variable declarations that are unused, ie where all control-flow paths to usage of the variable are overwritten by a reassignment. We then have to reconstruct the original variable declaration at the appropriate block scope during LeaveSSA, which is complex and can actually be incorrect in some cases. This PR updates to ensure that DCE will not remove the original variable declaration for any variable that is used (even in the case of always being reassigned before use). The main changes are: * DCE retains variable declarations, but if a variable declaration is always shadowed by reassignments then DCE will rewrite StoreLocal -> DeclareLocal so that it can DCE the unused initial value. * BuildHIR now has to change its handling for reassignment destructure instructions with nesting. Nesting uses a temporary which would appear as a declaration of a new variable, which is incompatible with other reassignments. See comments in the file. * LeaveSSA is quite a bit simpler now, since we never need to reconstruct a declaration. --- .../src/HIR/BuildHIR.ts | 66 ++++++++--- .../src/Optimization/DeadCodeElimination.ts | 106 ++++++++++++++---- .../src/SSA/LeaveSSA.ts | 93 ++------------- .../compiler/assignment-variations.expect.md | 4 +- ...control-dependency-do-while-test.expect.md | 3 +- ...g.reactive-control-dependency-if.expect.md | 2 +- ...trol-dependency-switch-case-test.expect.md | 2 +- ...trol-dependency-switch-condition.expect.md | 2 +- .../compiler/capture-param-mutate.expect.md | 2 +- .../capturing-function-within-block.expect.md | 3 +- .../const-propagation-phi-nodes.expect.md | 58 ++++++++++ ...odes.ts => const-propagation-phi-nodes.ts} | 2 +- .../dce-unused-postfix-update.expect.md | 35 ++++++ .../compiler/dce-unused-postfix-update.js | 11 ++ .../dce-unused-prefix-update.expect.md | 35 ++++++ .../compiler/dce-unused-prefix-update.js | 11 ++ .../destructure-direct-reassignment.expect.md | 4 +- .../destructure-in-branch-ssa.expect.md | 82 ++++++++++++++ ...ch-ssa.ts => destructure-in-branch-ssa.ts} | 2 +- ...cturing-assignment-array-default.expect.md | 2 +- .../destructuring-assignment.expect.md | 23 ++-- ...rror.const-propagation-phi-nodes.expect.md | 33 ------ .../error.destructure-in-branch-ssa.expect.md | 41 ------- ...or.while-with-assignment-in-test.expect.md | 25 ----- .../escape-analysis-not-if-test.expect.md | 2 +- .../for-loop-let-undefined-decl.expect.md | 2 +- .../function-declaration-redeclare.expect.md | 3 +- ...oisting-nested-const-declaration.expect.md | 4 +- ...reeze-possibly-mutable-arguments.expect.md | 2 +- .../compiler/infer-phi-primitive.expect.md | 2 +- .../compiler/inverted-if-else.expect.md | 2 +- .../obj-literal-cached-in-if-else.expect.md | 2 +- ...bj-literal-mutated-after-if-else.expect.md | 1 - ...mutated-after-if-else-with-alias.expect.md | 1 - .../obj-mutated-after-if-else.expect.md | 1 - ...-after-nested-if-else-with-alias.expect.md | 3 +- .../phi-type-inference-array-push.expect.md | 4 +- .../compiler/phi-type-inference-array-push.js | 1 - ...hi-type-inference-property-store.expect.md | 1 - .../compiler/primitive-alias-mutate.expect.md | 2 +- ...-in-returned-function-expression.expect.md | 2 +- .../fixtures/compiler/simple-alias.expect.md | 7 +- .../compiler/ssa-multiple-phis.expect.md | 4 +- .../fixtures/compiler/ssa-reassign.expect.md | 4 +- .../ternary-assignment-expression.expect.md | 3 +- ...ry-catch-try-immediately-returns.expect.md | 1 + ...hrows-after-constant-propagation.expect.md | 1 + .../fixtures/compiler/try-catch.expect.md | 2 +- .../compiler/unused-conditional.expect.md | 3 +- .../compiler/unused-logical.expect.md | 3 +- .../compiler/useMemo-switch-return.expect.md | 2 +- .../while-with-assignment-in-test.expect.md | 41 +++++++ ...st.js => while-with-assignment-in-test.js} | 8 +- .../packages/sprout/src/SproutTodoFilter.ts | 2 - 54 files changed, 484 insertions(+), 279 deletions(-) create mode 100644 compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/const-propagation-phi-nodes.expect.md rename compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/{error.const-propagation-phi-nodes.ts => const-propagation-phi-nodes.ts} (85%) create mode 100644 compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/dce-unused-postfix-update.expect.md create mode 100644 compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/dce-unused-postfix-update.js create mode 100644 compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/dce-unused-prefix-update.expect.md create mode 100644 compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/dce-unused-prefix-update.js create mode 100644 compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/destructure-in-branch-ssa.expect.md rename compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/{error.destructure-in-branch-ssa.ts => destructure-in-branch-ssa.ts} (91%) delete mode 100644 compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/error.const-propagation-phi-nodes.expect.md delete mode 100644 compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/error.destructure-in-branch-ssa.expect.md delete mode 100644 compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/error.while-with-assignment-in-test.expect.md create mode 100644 compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/while-with-assignment-in-test.expect.md rename compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/{error.while-with-assignment-in-test.js => while-with-assignment-in-test.js} (59%) diff --git a/compiler/packages/babel-plugin-react-forget/src/HIR/BuildHIR.ts b/compiler/packages/babel-plugin-react-forget/src/HIR/BuildHIR.ts index 4ab36cf6f8..de4ea10e33 100644 --- a/compiler/packages/babel-plugin-react-forget/src/HIR/BuildHIR.ts +++ b/compiler/packages/babel-plugin-react-forget/src/HIR/BuildHIR.ts @@ -3204,11 +3204,19 @@ function lowerAssignment( } } case "ArrayPattern": { - // TODO const lvalue = lvaluePath as NodePath; const elements = lvalue.get("elements"); const items: ArrayPattern["items"] = []; const followups: Array<{ place: Place; path: NodePath }> = []; + // A given destructuring statement must contain all declarations or all + // reassignments. This is enforced by the parser, but we rewrite nested + // destructuring into assignment to a temporary. Therefore, if we see + // any reassignments that are nested destructuring we fall back to + // using temporaries for all variables, and emitting the actual reassignments + // in follow-up statements + const forceTemporaries = + kind === InstructionKind.Reassign && + elements.some((element) => !element.isIdentifier()); for (let i = 0; i < elements.length; i++) { const element = elements[i]; if (element.node == null) { @@ -3228,7 +3236,7 @@ function lowerAssignment( } if (element.isRestElement()) { const argument = element.get("argument"); - if (argument.isIdentifier()) { + if (argument.isIdentifier() && !forceTemporaries) { const identifier = lowerIdentifierForAssignment( builder, element.node.loc ?? GeneratedSource, @@ -3253,7 +3261,7 @@ function lowerAssignment( }); followups.push({ place: temp, path: argument as NodePath }); // TODO remove type cast } - } else if (element.isIdentifier()) { + } else if (element.isIdentifier() && !forceTemporaries) { const identifier = lowerIdentifierForAssignment( builder, element.node.loc ?? GeneratedSource, @@ -3295,6 +3303,20 @@ function lowerAssignment( const propertiesPaths = lvalue.get("properties"); const properties: ObjectPattern["properties"] = []; const followups: Array<{ place: Place; path: NodePath }> = []; + // A given destructuring statement must contain all declarations or all + // reassignments. This is enforced by the parser, but we rewrite nested + // destructuring into assignment to a temporary. Therefore, if we see + // any reassignments that are nested destructuring we fall back to + // using temporaries for all variables, and emitting the actual reassignments + // in follow-up statements + const forceTemporaries = + kind === InstructionKind.Reassign && + propertiesPaths.some( + (property) => + property.isRestElement() || + (property.isObjectProperty() && + !property.get("value").isIdentifier()) + ); for (let i = 0; i < propertiesPaths.length; i++) { const property = propertiesPaths[i]; if (property.isRestElement()) { @@ -3308,19 +3330,31 @@ function lowerAssignment( }); continue; } - const identifier = lowerIdentifierForAssignment( - builder, - property.node.loc ?? GeneratedSource, - kind, - argument - ); - if (identifier === null) { - continue; + if (forceTemporaries) { + const temp = buildTemporaryPlace( + builder, + property.node.loc ?? GeneratedSource + ); + properties.push({ + kind: "Spread", + place: { ...temp }, + }); + followups.push({ place: temp, path: argument as NodePath }); // TODO remove type cast + } else { + const identifier = lowerIdentifierForAssignment( + builder, + property.node.loc ?? GeneratedSource, + kind, + argument + ); + if (identifier === null) { + continue; + } + properties.push({ + kind: "Spread", + place: identifier, + }); } - properties.push({ - kind: "Spread", - place: identifier, - }); } else { // TODO: this should always be true given the if/else if (!property.isObjectProperty()) { @@ -3355,7 +3389,7 @@ function lowerAssignment( }); continue; } - if (element.isIdentifier()) { + if (element.isIdentifier() && !forceTemporaries) { const identifier = lowerIdentifierForAssignment( builder, element.node.loc ?? GeneratedSource, diff --git a/compiler/packages/babel-plugin-react-forget/src/Optimization/DeadCodeElimination.ts b/compiler/packages/babel-plugin-react-forget/src/Optimization/DeadCodeElimination.ts index af6ad7d2c5..e4fb8d44ce 100644 --- a/compiler/packages/babel-plugin-react-forget/src/Optimization/DeadCodeElimination.ts +++ b/compiler/packages/babel-plugin-react-forget/src/Optimization/DeadCodeElimination.ts @@ -10,7 +10,9 @@ import { BlockId, HIRFunction, Identifier, + IdentifierId, Instruction, + InstructionKind, InstructionValue, ObjectPattern, } from "../HIR"; @@ -48,7 +50,7 @@ export function deadCodeElimination(fn: HIRFunction): void { for (let i = block.instructions.length - 1; i >= 0; i--) { const instr = block.instructions[i]!; if ( - !state.used(instr.lvalue.identifier) && + !state.isIdOrNameUsed(instr.lvalue.identifier) && pruneableValue(instr.value, state) && // Can't prune the last value of a value block, that's its value! !(block.kind !== "block" && i === block.instructions.length - 1) @@ -56,10 +58,20 @@ export function deadCodeElimination(fn: HIRFunction): void { continue; } state.reference(instr.lvalue.identifier); + + // For the last value of a value block, if it's not pruneable we can't + // rewrite it. This is necessary to preserve unused value blocks + if (block.kind !== "block" && i === block.instructions.length - 1) { + for (const place of eachInstructionValueOperand(instr.value)) { + state.reference(place.identifier); + } + continue; + } + // Otherwise rewrite instructions to remove unused parts of them visitInstruction(instr, state); } for (const phi of block.phis) { - if (state.used(phi.id)) { + if (state.isIdOrNameUsed(phi.id)) { for (const [_pred, operand] of phi.operands) { state.reference(operand); } @@ -69,25 +81,42 @@ export function deadCodeElimination(fn: HIRFunction): void { } while (state.count > size && hasLoop); for (const [, block] of fn.body.blocks) { for (const phi of block.phis) { - if (!state.used(phi.id)) { + if (!state.isIdOrNameUsed(phi.id)) { block.phis.delete(phi); } } retainWhere(block.instructions, (instr) => - state.used(instr.lvalue.identifier) + state.isIdOrNameUsed(instr.lvalue.identifier) ); } } class State { - identifiers: Set = new Set(); + named: Set = new Set(); + identifiers: Set = new Set(); + // Mark the identifier as being referenced (not dead code) reference(identifier: Identifier): void { - this.identifiers.add(identifier); + this.identifiers.add(identifier.id); + if (identifier.name !== null) { + this.named.add(identifier.name); + } } - used(identifier: Identifier): boolean { - return this.identifiers.has(identifier); + // Check if any version of the given identifier is used somewhere. + // This checks both for usage of this specific identifer id (ssa id) + // and (for named identifiers) for any usages of that identifier name. + isIdOrNameUsed(identifier: Identifier): boolean { + return ( + this.identifiers.has(identifier.id) || + (identifier.name !== null && this.named.has(identifier.name)) + ); + } + + // Like `used()`, but only checks for usages of this specific identifier id + // (ssa id). + isIdUsed(identifier: Identifier): boolean { + return this.identifiers.has(identifier.id); } get count(): number { @@ -110,12 +139,12 @@ function visitInstruction(instr: Instruction, state: State): void { for (let i = originalItems.length - 1; i >= 0; i--) { const item = originalItems[i]; if (item.kind === "Identifier") { - if (state.used(item.identifier)) { + if (state.isIdOrNameUsed(item.identifier)) { nextItems = originalItems.slice(0, i + 1); break; } } else if (item.kind === "Spread") { - if (state.used(item.place.identifier)) { + if (state.isIdOrNameUsed(item.place.identifier)) { nextItems = originalItems.slice(0, i + 1); break; } @@ -135,12 +164,12 @@ function visitInstruction(instr: Instruction, state: State): void { let nextProperties: ObjectPattern["properties"] | null = null; for (const property of instr.value.lvalue.pattern.properties) { if (property.kind === "ObjectProperty") { - if (state.used(property.place.identifier)) { + if (state.isIdOrNameUsed(property.place.identifier)) { nextProperties ??= []; nextProperties.push(property); } } else { - if (state.used(property.place.identifier)) { + if (state.isIdOrNameUsed(property.place.identifier)) { nextProperties = null; break; } @@ -160,6 +189,25 @@ function visitInstruction(instr: Instruction, state: State): void { ); } } + } else if (instr.value.kind === "StoreLocal") { + if ( + instr.value.lvalue.kind !== InstructionKind.Reassign && + !state.isIdUsed(instr.value.lvalue.place.identifier) + ) { + // This is a const/let declaration where the variable is accessed later, + // but where the value is always overwritten before being read. Ie the + // initializer value is never read. We rewrite to a DeclareLocal so + // that the initializer value can be DCE'd + instr.value = { + kind: "DeclareLocal", + lvalue: instr.value.lvalue, + loc: instr.value.loc, + }; + } else { + // Else we mark the initializer as referenced, since the variable itself is + // referenced + state.reference(instr.value.value.identifier); + } } else { for (const operand of eachInstructionValueOperand(instr.value)) { state.reference(operand.identifier); @@ -174,26 +222,40 @@ function visitInstruction(instr: Instruction, state: State): void { function pruneableValue(value: InstructionValue, state: State): boolean { switch (value.kind) { case "DeclareLocal": { - return !state.used(value.lvalue.place.identifier); + // Declarations are pruneable only if the named variable is never read later + return !state.isIdOrNameUsed(value.lvalue.place.identifier); } case "StoreLocal": { - // Stores are pruneable only if the identifier being stored to is never read later - return !state.used(value.lvalue.place.identifier); + if (value.lvalue.kind === InstructionKind.Reassign) { + // Reassignments can be pruned if the specific instance being assigned is never read + return !state.isIdUsed(value.lvalue.place.identifier); + } + // Declarations are pruneable only if the named variable is never read later + return !state.isIdOrNameUsed(value.lvalue.place.identifier); } case "Destructure": { - // Destructure is pruneable only if none of the identifiers are read from later - // TODO: as an optimization, prune unused properties where safe + let isIdOrNameUsed = false; + let isIdUsed = false; for (const place of eachPatternOperand(value.lvalue.pattern)) { - if (state.used(place.identifier)) { - return false; + if (state.isIdUsed(place.identifier)) { + isIdOrNameUsed = true; + isIdUsed = true; + } else if (state.isIdOrNameUsed(place.identifier)) { + isIdOrNameUsed = true; } } - return true; + if (value.lvalue.kind === InstructionKind.Reassign) { + // Reassignments can be pruned if the specific instance being assigned is never read + return !isIdUsed; + } else { + // Otherwise pruneable only if none of the identifiers are read from later + return !isIdOrNameUsed; + } } case "PostfixUpdate": case "PrefixUpdate": { - // Updates are pruneable only if the identifier being stored to is never read later - return !state.used(value.lvalue.identifier); + // Updates are pruneable if the specific instance instance being assigned is never read + return !state.isIdUsed(value.lvalue.identifier); } case "Debugger": { // explicitly retain debugger statements to not break debugging workflows diff --git a/compiler/packages/babel-plugin-react-forget/src/SSA/LeaveSSA.ts b/compiler/packages/babel-plugin-react-forget/src/SSA/LeaveSSA.ts index 85e907e5cd..e557673ea1 100644 --- a/compiler/packages/babel-plugin-react-forget/src/SSA/LeaveSSA.ts +++ b/compiler/packages/babel-plugin-react-forget/src/SSA/LeaveSSA.ts @@ -9,11 +9,8 @@ import { CompilerError } from "../CompilerError"; import { BasicBlock, BlockId, - Effect, - GeneratedSource, HIRFunction, Identifier, - Instruction, InstructionKind, LValue, LValuePattern, @@ -420,88 +417,13 @@ export function leaveSSA(fn: HIRFunction): void { suggestions: null, }); const declaration = declarations.get(phi.id.name); - if (declaration === undefined) { - let initValue: Place; - if (initOperand === null) { - initValue = { - effect: Effect.Read, - kind: "Identifier", - loc: GeneratedSource, - identifier: { - id: fn.env.nextIdentifierId, - name: null, - mutableRange: { - // TODO: this is technically the wrong start range; we do this because the instruction to create the - // undefined and the instruction to store it to the identifier share an InstructionId, which makes - // this value otherwise appear mutable when stored. All that matters is that the range end prior - // to the StoreLocal's instruction id, so we decrement by one. - start: makeInstructionId(block.terminal.id - 1), - end: makeInstructionId(block.terminal.id), - }, - scope: null, - type: { kind: "Primitive" }, - }, - }; - block.instructions.push({ - id: block.terminal.id, - lvalue: { ...initValue, effect: Effect.ConditionallyMutate }, - value: { - kind: "Primitive", - // TODO: consider leaving the variable uninitialized rather than explicitly undefined. - value: undefined, - loc: GeneratedSource, - }, - loc: GeneratedSource, - }); - } else { - initValue = { - kind: "Identifier", - identifier: initOperand, - effect: Effect.Capture, - loc: GeneratedSource, - }; - } - const lvalue: LValue = { - place: { - kind: "Identifier", - identifier: phi.id, - effect: Effect.ConditionallyMutate, - loc: GeneratedSource, - }, - kind: InstructionKind.Let, - }; - const instr: Instruction = { - // NOTE: reuse the terminal id since these lets must be scoped with the terminal anyway. - // the mutable range of this canonical id must by definition span from the binding (before - // the if) to the phi, so it's safe to reuse the terminal's id. - id: block.terminal.id, - lvalue: { - kind: "Identifier", - identifier: { - id: fn.env.nextIdentifierId, - mutableRange: { - start: block.terminal.id, - end: makeInstructionId(block.terminal.id + 1), - }, - name: null, - scope: null, - type: phi.id.type, - }, - effect: Effect.ConditionallyMutate, - loc: GeneratedSource, - }, - value: { - kind: "StoreLocal", - lvalue, - value: initValue, - loc: GeneratedSource, - }, - loc: GeneratedSource, - }; - block.instructions.push(instr); - declarations.set(phi.id.name, { lvalue, place: lvalue.place }); - phi.id.mutableRange.start = terminal.id; - } else if (isPhiMutatedAfterCreation) { + CompilerError.invariant(declaration != null, { + loc: null, + reason: "Expected a declaration for all variables", + description: null, + suggestions: null, + }); + if (isPhiMutatedAfterCreation) { // The declaration is not guaranteed to flow into the phi, for example in the case of a variable // that is reassigned in all control flow paths to a given phi. The original declaration's range // has to be extended in this case (if the phi is later mutated) since we are reusing the original @@ -511,6 +433,7 @@ export function leaveSSA(fn: HIRFunction): void { // not prune. Otherwise, the declaration would have been pruned and we'd synthesize a new one. declaration.place.identifier.mutableRange.end = phi.id.mutableRange.end; } + rewrites.set(phi.id, declaration.place.identifier); } // Similar logic for rewrite phis that occur in loops, except that instead of a new let binding diff --git a/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/assignment-variations.expect.md b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/assignment-variations.expect.md index 5eb49924a9..1191738b28 100644 --- a/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/assignment-variations.expect.md +++ b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/assignment-variations.expect.md @@ -22,7 +22,9 @@ export const FIXTURE_ENTRYPOINT = { ```javascript function f() { - const x = 3 >>> 1; + let x; + + x = 3 >>> 1; return x; } diff --git a/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/bug.reactive-control-dependency-do-while-test.expect.md b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/bug.reactive-control-dependency-do-while-test.expect.md index 0ae7395500..d172bd168b 100644 --- a/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/bug.reactive-control-dependency-do-while-test.expect.md +++ b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/bug.reactive-control-dependency-do-while-test.expect.md @@ -33,10 +33,9 @@ export const FIXTURE_ENTRYPOINT = { import { unstable_useMemoCache as useMemoCache } from "react"; function Component(props) { const $ = useMemoCache(1); - + let x; let i = 0; do { - let x = undefined; if (i > 10) { x = 10; } else { diff --git a/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/bug.reactive-control-dependency-if.expect.md b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/bug.reactive-control-dependency-if.expect.md index c7fb89fe9e..28f34ba129 100644 --- a/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/bug.reactive-control-dependency-if.expect.md +++ b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/bug.reactive-control-dependency-if.expect.md @@ -28,7 +28,7 @@ export const FIXTURE_ENTRYPOINT = { import { unstable_useMemoCache as useMemoCache } from "react"; function Component(props) { const $ = useMemoCache(1); - let x = undefined; + let x; if (props.cond) { x = 1; } else { diff --git a/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/bug.reactive-control-dependency-switch-case-test.expect.md b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/bug.reactive-control-dependency-switch-case-test.expect.md index 306369b10e..3fd0b8c815 100644 --- a/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/bug.reactive-control-dependency-switch-case-test.expect.md +++ b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/bug.reactive-control-dependency-switch-case-test.expect.md @@ -36,7 +36,7 @@ export const FIXTURE_ENTRYPOINT = { import { unstable_useMemoCache as useMemoCache } from "react"; function Component(props) { const $ = useMemoCache(1); - let x = undefined; + let x; bb1: switch (props.cond) { case true: { x = 1; diff --git a/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/bug.reactive-control-dependency-switch-condition.expect.md b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/bug.reactive-control-dependency-switch-condition.expect.md index e17147c356..f1e8cbc2f7 100644 --- a/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/bug.reactive-control-dependency-switch-condition.expect.md +++ b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/bug.reactive-control-dependency-switch-condition.expect.md @@ -38,7 +38,7 @@ const GLOBAL = 42; function Component(t14) { const $ = useMemoCache(1); const { value } = t14; - let x = undefined; + let x; bb1: switch (GLOBAL) { case value: { x = 1; diff --git a/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/capture-param-mutate.expect.md b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/capture-param-mutate.expect.md index 0279315454..b86be7daeb 100644 --- a/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/capture-param-mutate.expect.md +++ b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/capture-param-mutate.expect.md @@ -51,7 +51,7 @@ function getNativeLogFunction(level) { let t0; if ($[0] !== level) { t0 = function () { - let str = undefined; + let str; if (arguments.length === 1 && typeof arguments[0] === "string") { str = arguments[0]; } else { diff --git a/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/capturing-function-within-block.expect.md b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/capturing-function-within-block.expect.md index c615a47036..24b231a1be 100644 --- a/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/capturing-function-within-block.expect.md +++ b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/capturing-function-within-block.expect.md @@ -36,6 +36,7 @@ function component(a) { t0 = $[1]; } const z = t0; + let x; let t1; if ($[2] !== z) { t1 = function () { @@ -46,7 +47,7 @@ function component(a) { } else { t1 = $[3]; } - const x = t1; + x = t1; return x; } diff --git a/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/const-propagation-phi-nodes.expect.md b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/const-propagation-phi-nodes.expect.md new file mode 100644 index 0000000000..7438eb5978 --- /dev/null +++ b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/const-propagation-phi-nodes.expect.md @@ -0,0 +1,58 @@ + +## Input + +```javascript +function useFoo(setOne: boolean) { + let x; + let y; + let z; + if (setOne) { + x = y = z = 1; + } else { + x = 2; + y = 3; + z = 5; + } + return { x, y, z }; +} + +export const FIXTURE_ENTRYPOINT = { + fn: useFoo, + params: [true], +}; + +``` + +## Code + +```javascript +import { unstable_useMemoCache as useMemoCache } from "react"; +function useFoo(setOne) { + const $ = useMemoCache(1); + let x; + let y; + let z; + if (setOne) { + x = y = z = 1; + } else { + x = 2; + y = 3; + z = 5; + } + let t0; + if ($[0] === Symbol.for("react.memo_cache_sentinel")) { + t0 = { x, y, z }; + $[0] = t0; + } else { + t0 = $[0]; + } + return t0; +} + +export const FIXTURE_ENTRYPOINT = { + fn: useFoo, + params: [true], +}; + +``` + \ No newline at end of file diff --git a/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/error.const-propagation-phi-nodes.ts b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/const-propagation-phi-nodes.ts similarity index 85% rename from compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/error.const-propagation-phi-nodes.ts rename to compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/const-propagation-phi-nodes.ts index 42008b0d2e..1a48999fa1 100644 --- a/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/error.const-propagation-phi-nodes.ts +++ b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/const-propagation-phi-nodes.ts @@ -12,7 +12,7 @@ function useFoo(setOne: boolean) { return { x, y, z }; } -export const FIXTURE_ENTRYPONT = { +export const FIXTURE_ENTRYPOINT = { fn: useFoo, params: [true], }; diff --git a/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/dce-unused-postfix-update.expect.md b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/dce-unused-postfix-update.expect.md new file mode 100644 index 0000000000..74c3336dd9 --- /dev/null +++ b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/dce-unused-postfix-update.expect.md @@ -0,0 +1,35 @@ + +## Input + +```javascript +function Component(props) { + let i = 0; + i++; + i = props.i; + return i; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{ i: 42 }], +}; + +``` + +## Code + +```javascript +function Component(props) { + let i; + + i = props.i; + return i; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{ i: 42 }], +}; + +``` + \ No newline at end of file diff --git a/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/dce-unused-postfix-update.js b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/dce-unused-postfix-update.js new file mode 100644 index 0000000000..43b90f718d --- /dev/null +++ b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/dce-unused-postfix-update.js @@ -0,0 +1,11 @@ +function Component(props) { + let i = 0; + i++; + i = props.i; + return i; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{ i: 42 }], +}; diff --git a/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/dce-unused-prefix-update.expect.md b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/dce-unused-prefix-update.expect.md new file mode 100644 index 0000000000..0be35c86fc --- /dev/null +++ b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/dce-unused-prefix-update.expect.md @@ -0,0 +1,35 @@ + +## Input + +```javascript +function Component(props) { + let i = 0; + --i; + i = props.i; + return i; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{ i: 42 }], +}; + +``` + +## Code + +```javascript +function Component(props) { + let i; + + i = props.i; + return i; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{ i: 42 }], +}; + +``` + \ No newline at end of file diff --git a/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/dce-unused-prefix-update.js b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/dce-unused-prefix-update.js new file mode 100644 index 0000000000..a76db00e53 --- /dev/null +++ b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/dce-unused-prefix-update.js @@ -0,0 +1,11 @@ +function Component(props) { + let i = 0; + --i; + i = props.i; + return i; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{ i: 42 }], +}; diff --git a/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/destructure-direct-reassignment.expect.md b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/destructure-direct-reassignment.expect.md index d55724a4b4..dfb1ae8565 100644 --- a/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/destructure-direct-reassignment.expect.md +++ b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/destructure-direct-reassignment.expect.md @@ -22,7 +22,9 @@ export const FIXTURE_ENTRYPOINT = { ```javascript function foo(props) { - let { x, y } = { x: props.a, y: props.b }; + let x; + let y; + ({ x, y } = { x: props.a, y: props.b }); console.log(x); x = props.c; return x + y; diff --git a/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/destructure-in-branch-ssa.expect.md b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/destructure-in-branch-ssa.expect.md new file mode 100644 index 0000000000..6a6e961491 --- /dev/null +++ b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/destructure-in-branch-ssa.expect.md @@ -0,0 +1,82 @@ + +## Input + +```javascript +function useFoo(props: { + x?: string; + y?: string; + z?: string; + doDestructure: boolean; +}) { + let x = null; + let y = null; + let z = null; + const myList = []; + if (props.doDestructure) { + ({ x, y, z } = props); + + myList.push(z); + } + return { + x, + y, + myList, + }; +} + +export const FIXTURE_ENTRYPOINT = { + fn: useFoo, + params: [{ x: "hello", y: "world", doDestructure: true }], +}; + +``` + +## Code + +```javascript +import { unstable_useMemoCache as useMemoCache } from "react"; +function useFoo(props) { + const $ = useMemoCache(9); + + let x = null; + let y = null; + let z; + let myList; + if ($[0] !== props) { + myList = []; + if (props.doDestructure) { + ({ x, y, z } = props); + + myList.push(z); + } + $[0] = props; + $[1] = myList; + $[2] = x; + $[3] = y; + $[4] = z; + } else { + myList = $[1]; + x = $[2]; + y = $[3]; + z = $[4]; + } + let t0; + if ($[5] !== x || $[6] !== y || $[7] !== myList) { + t0 = { x, y, myList }; + $[5] = x; + $[6] = y; + $[7] = myList; + $[8] = t0; + } else { + t0 = $[8]; + } + return t0; +} + +export const FIXTURE_ENTRYPOINT = { + fn: useFoo, + params: [{ x: "hello", y: "world", doDestructure: true }], +}; + +``` + \ No newline at end of file diff --git a/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/error.destructure-in-branch-ssa.ts b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/destructure-in-branch-ssa.ts similarity index 91% rename from compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/error.destructure-in-branch-ssa.ts rename to compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/destructure-in-branch-ssa.ts index a11e146491..1a6cb7b937 100644 --- a/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/error.destructure-in-branch-ssa.ts +++ b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/destructure-in-branch-ssa.ts @@ -20,7 +20,7 @@ function useFoo(props: { }; } -export const FIXTURE_ENTRYPONT = { +export const FIXTURE_ENTRYPOINT = { fn: useFoo, params: [{ x: "hello", y: "world", doDestructure: true }], }; diff --git a/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/destructuring-assignment-array-default.expect.md b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/destructuring-assignment-array-default.expect.md index 4c7a5908a6..17d3a0b9d1 100644 --- a/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/destructuring-assignment-array-default.expect.md +++ b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/destructuring-assignment-array-default.expect.md @@ -26,7 +26,7 @@ export const FIXTURE_ENTRYPOINT = { import { unstable_useMemoCache as useMemoCache } from "react"; function Component(props) { const $ = useMemoCache(2); - let x = undefined; + let x; if (props.cond) { const [t0] = props.y; let t1; diff --git a/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/destructuring-assignment.expect.md b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/destructuring-assignment.expect.md index f2c09bb122..7b93e9b33a 100644 --- a/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/destructuring-assignment.expect.md +++ b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/destructuring-assignment.expect.md @@ -35,15 +35,20 @@ export const FIXTURE_ENTRYPOINT = { import { unstable_useMemoCache as useMemoCache } from "react"; function foo(a, b, c) { const $ = useMemoCache(5); - - const [d, t46] = a; - const [t48] = t46; - const { e: t50 } = t48; - const { f: g } = t50; - const { l: t55, o } = b; - const { m: t58 } = t55; - const [t60] = t58; - const [n] = t60; + let d; + let g; + let n; + let o; + const [t49, t50] = a; + d = t49; + const [t54] = t50; + const { e: t56 } = t54; + ({ f: g } = t56); + const { l: t61, o: t62 } = b; + const { m: t64 } = t61; + const [t66] = t64; + [n] = t66; + o = t62; let t0; if ($[0] !== d || $[1] !== g || $[2] !== n || $[3] !== o) { t0 = { d, g, n, o }; diff --git a/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/error.const-propagation-phi-nodes.expect.md b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/error.const-propagation-phi-nodes.expect.md deleted file mode 100644 index a7218ad162..0000000000 --- a/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/error.const-propagation-phi-nodes.expect.md +++ /dev/null @@ -1,33 +0,0 @@ - -## Input - -```javascript -function useFoo(setOne: boolean) { - let x; - let y; - let z; - if (setOne) { - x = y = z = 1; - } else { - x = 2; - y = 3; - z = 5; - } - return { x, y, z }; -} - -export const FIXTURE_ENTRYPONT = { - fn: useFoo, - params: [true], -}; - -``` - - -## Error - -``` -[ReactForget] Invariant: Const declaration cannot be referenced as an expression (6:6) -``` - - \ No newline at end of file diff --git a/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/error.destructure-in-branch-ssa.expect.md b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/error.destructure-in-branch-ssa.expect.md deleted file mode 100644 index 1eb1abb0d1..0000000000 --- a/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/error.destructure-in-branch-ssa.expect.md +++ /dev/null @@ -1,41 +0,0 @@ - -## Input - -```javascript -function useFoo(props: { - x?: string; - y?: string; - z?: string; - doDestructure: boolean; -}) { - let x = null; - let y = null; - let z = null; - const myList = []; - if (props.doDestructure) { - ({ x, y, z } = props); - - myList.push(z); - } - return { - x, - y, - myList, - }; -} - -export const FIXTURE_ENTRYPONT = { - fn: useFoo, - params: [{ x: "hello", y: "world", doDestructure: true }], -}; - -``` - - -## Error - -``` -[ReactForget] Invariant: Expected consistent kind for destructuring. Other places were 'Reassign' but 'store z$44' is const (12:12) -``` - - \ No newline at end of file diff --git a/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/error.while-with-assignment-in-test.expect.md b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/error.while-with-assignment-in-test.expect.md deleted file mode 100644 index f3812861f6..0000000000 --- a/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/error.while-with-assignment-in-test.expect.md +++ /dev/null @@ -1,25 +0,0 @@ - -## Input - -```javascript -function f(reader) { - const queue = [1, 2, 3]; - let value = 0; - let sum = 0; - // BUG: we need to codegen the complex test expression - while ((value = queue.pop()) != null) { - sum += value; - } - return sum; -} - -``` - - -## Error - -``` -[ReactForget] Invariant: TODO: Handle reassignment in a value block where the original declaration was removed by dead code elimination (DCE) (6:6) -``` - - \ No newline at end of file diff --git a/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/escape-analysis-not-if-test.expect.md b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/escape-analysis-not-if-test.expect.md index ab2c0bc83e..3bbdffcf71 100644 --- a/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/escape-analysis-not-if-test.expect.md +++ b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/escape-analysis-not-if-test.expect.md @@ -26,7 +26,7 @@ export const FIXTURE_ENTRYPOINT = { ```javascript function Component(props) { const x = [props.a]; - let y = undefined; + let y; if (x) { y = props.b; } else { diff --git a/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/for-loop-let-undefined-decl.expect.md b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/for-loop-let-undefined-decl.expect.md index 1e722713ad..0b86daefa9 100644 --- a/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/for-loop-let-undefined-decl.expect.md +++ b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/for-loop-let-undefined-decl.expect.md @@ -26,7 +26,7 @@ export const FIXTURE_ENTRYPOINT = { ```javascript function useFoo() { for (let i = 0; i <= 5; i++) { - let color = undefined; + let color; if (isSelected) { color = isCurrent ? "#FFCC22" : "#FF5050"; } else { diff --git a/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/function-declaration-redeclare.expect.md b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/function-declaration-redeclare.expect.md index 7524e6f200..3dd98530c3 100644 --- a/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/function-declaration-redeclare.expect.md +++ b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/function-declaration-redeclare.expect.md @@ -24,6 +24,7 @@ export const FIXTURE_ENTRYPOINT = { import { unstable_useMemoCache as useMemoCache } from "react"; function component() { const $ = useMemoCache(1); + let x; let t0; if ($[0] === Symbol.for("react.memo_cache_sentinel")) { t0 = function x() {}; @@ -31,7 +32,7 @@ function component() { } else { t0 = $[0]; } - const x = t0; + x = t0; return x; } diff --git a/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/hoisting-nested-const-declaration.expect.md b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/hoisting-nested-const-declaration.expect.md index b2916d6869..98eddcc809 100644 --- a/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/hoisting-nested-const-declaration.expect.md +++ b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/hoisting-nested-const-declaration.expect.md @@ -35,7 +35,9 @@ function hoisting() { let t0; if ($[0] === Symbol.for("react.memo_cache_sentinel")) { const qux = () => { - const result = foo(); + let result; + + result = foo(); return result; }; diff --git a/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/hooks-freeze-possibly-mutable-arguments.expect.md b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/hooks-freeze-possibly-mutable-arguments.expect.md index 58f4d87fc9..94e32c01fa 100644 --- a/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/hooks-freeze-possibly-mutable-arguments.expect.md +++ b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/hooks-freeze-possibly-mutable-arguments.expect.md @@ -30,7 +30,7 @@ function Component(props) { const $ = useMemoCache(1); const cond = props.cond; const x = props.x; - let a = undefined; + let a; if (cond) { a = x; } else { diff --git a/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/infer-phi-primitive.expect.md b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/infer-phi-primitive.expect.md index a96d63d5e5..ef71894440 100644 --- a/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/infer-phi-primitive.expect.md +++ b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/infer-phi-primitive.expect.md @@ -26,7 +26,7 @@ export const FIXTURE_ENTRYPOINT = { ```javascript function foo(a, b) { - let x = undefined; + let x; if (a) { x = 1; } else { diff --git a/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/inverted-if-else.expect.md b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/inverted-if-else.expect.md index b6838569ac..3cea08a9c4 100644 --- a/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/inverted-if-else.expect.md +++ b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/inverted-if-else.expect.md @@ -26,7 +26,7 @@ export const FIXTURE_ENTRYPOINT = { ```javascript function foo(a, b, c) { - let x = undefined; + let x; bb1: { if (a) { x = b; diff --git a/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/obj-literal-cached-in-if-else.expect.md b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/obj-literal-cached-in-if-else.expect.md index 24cd76e11b..9b3444a00a 100644 --- a/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/obj-literal-cached-in-if-else.expect.md +++ b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/obj-literal-cached-in-if-else.expect.md @@ -21,7 +21,7 @@ function foo(a, b, c, d) { import { unstable_useMemoCache as useMemoCache } from "react"; function foo(a, b, c, d) { const $ = useMemoCache(4); - let x = undefined; + let x; if (someVal) { let t0; if ($[0] !== b) { diff --git a/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/obj-literal-mutated-after-if-else.expect.md b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/obj-literal-mutated-after-if-else.expect.md index 8271b1d353..52799e9c30 100644 --- a/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/obj-literal-mutated-after-if-else.expect.md +++ b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/obj-literal-mutated-after-if-else.expect.md @@ -24,7 +24,6 @@ function foo(a, b, c, d) { const $ = useMemoCache(3); let x; if ($[0] !== b || $[1] !== c) { - x = undefined; if (someVal) { x = { b }; } else { diff --git a/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/obj-mutated-after-if-else-with-alias.expect.md b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/obj-mutated-after-if-else-with-alias.expect.md index b1e28b6697..0ed16663da 100644 --- a/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/obj-mutated-after-if-else-with-alias.expect.md +++ b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/obj-mutated-after-if-else-with-alias.expect.md @@ -27,7 +27,6 @@ function foo(a, b, c, d) { someObj(); let x; if ($[0] !== a) { - x = undefined; if (a) { const y = someObj(); const z = y; diff --git a/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/obj-mutated-after-if-else.expect.md b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/obj-mutated-after-if-else.expect.md index 9a8604ede7..0b62b8351e 100644 --- a/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/obj-mutated-after-if-else.expect.md +++ b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/obj-mutated-after-if-else.expect.md @@ -25,7 +25,6 @@ function foo(a, b, c, d) { someObj(); let x; if ($[0] !== a) { - x = undefined; if (a) { x = someObj(); } else { diff --git a/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/obj-mutated-after-nested-if-else-with-alias.expect.md b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/obj-mutated-after-nested-if-else-with-alias.expect.md index 6d7ab19057..7e84471978 100644 --- a/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/obj-mutated-after-nested-if-else-with-alias.expect.md +++ b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/obj-mutated-after-nested-if-else-with-alias.expect.md @@ -33,9 +33,8 @@ function foo(a, b, c, d) { someObj(); let x; if ($[0] !== a || $[1] !== b) { - x = undefined; if (a) { - let z = undefined; + let z; if (b) { const w = someObj(); z = w; diff --git a/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/phi-type-inference-array-push.expect.md b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/phi-type-inference-array-push.expect.md index 801109a132..417ba26094 100644 --- a/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/phi-type-inference-array-push.expect.md +++ b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/phi-type-inference-array-push.expect.md @@ -2,7 +2,6 @@ ## Input ```javascript -// @debug function Component(props) { const x = {}; let y; @@ -30,14 +29,13 @@ export const FIXTURE_ENTRYPOINT = { ## Code ```javascript -import { unstable_useMemoCache as useMemoCache } from "react"; // @debug +import { unstable_useMemoCache as useMemoCache } from "react"; function Component(props) { const $ = useMemoCache(6); let x; let y; if ($[0] !== props) { x = {}; - y = undefined; if (props.cond) { y = [props.value]; } else { diff --git a/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/phi-type-inference-array-push.js b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/phi-type-inference-array-push.js index 1c973f556f..052e1a274a 100644 --- a/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/phi-type-inference-array-push.js +++ b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/phi-type-inference-array-push.js @@ -1,4 +1,3 @@ -// @debug function Component(props) { const x = {}; let y; diff --git a/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/phi-type-inference-property-store.expect.md b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/phi-type-inference-property-store.expect.md index 4ddb01328f..65dc68f39e 100644 --- a/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/phi-type-inference-property-store.expect.md +++ b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/phi-type-inference-property-store.expect.md @@ -43,7 +43,6 @@ function Component(props) { const x = t0; let y; if ($[1] !== props || $[2] !== x) { - y = undefined; if (props.cond) { y = {}; } else { diff --git a/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/primitive-alias-mutate.expect.md b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/primitive-alias-mutate.expect.md index 01e67a8fd4..70c332c90e 100644 --- a/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/primitive-alias-mutate.expect.md +++ b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/primitive-alias-mutate.expect.md @@ -20,7 +20,7 @@ function component(a) { ```javascript function component(a) { - let x = undefined; + let x; if (a) { x = "bar"; } else { diff --git a/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/reassigned-phi-in-returned-function-expression.expect.md b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/reassigned-phi-in-returned-function-expression.expect.md index 22ea5bbce3..66ec676c60 100644 --- a/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/reassigned-phi-in-returned-function-expression.expect.md +++ b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/reassigned-phi-in-returned-function-expression.expect.md @@ -25,7 +25,7 @@ function Component(props) { let t0; if ($[0] !== props.str) { t0 = () => { - let str = undefined; + let str; if (arguments.length) { str = arguments[0]; } else { diff --git a/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/simple-alias.expect.md b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/simple-alias.expect.md index 932bb2436c..b7e8ddde4f 100644 --- a/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/simple-alias.expect.md +++ b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/simple-alias.expect.md @@ -22,18 +22,21 @@ function foo() { import { unstable_useMemoCache as useMemoCache } from "react"; function mutate() {} function foo() { - const $ = useMemoCache(1); + const $ = useMemoCache(2); + let a; let c; if ($[0] === Symbol.for("react.memo_cache_sentinel")) { let b = {}; c = {}; - const a = b; + a = b; b = c; c = a; mutate(a, b); $[0] = c; + $[1] = a; } else { c = $[0]; + a = $[1]; } return c; } diff --git a/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/ssa-multiple-phis.expect.md b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/ssa-multiple-phis.expect.md index 9f21b06cd5..42013535ca 100644 --- a/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/ssa-multiple-phis.expect.md +++ b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/ssa-multiple-phis.expect.md @@ -34,7 +34,9 @@ export const FIXTURE_ENTRYPOINT = { ```javascript function foo(a, b, c, d) { - const x = a; + let x; + + x = a; return x; } diff --git a/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/ssa-reassign.expect.md b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/ssa-reassign.expect.md index 2264b00548..fc403ed7de 100644 --- a/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/ssa-reassign.expect.md +++ b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/ssa-reassign.expect.md @@ -22,7 +22,9 @@ export const FIXTURE_ENTRYPOINT = { ```javascript function foo(a, b, c) { - const x = c; + let x; + + x = c; return x; } diff --git a/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/ternary-assignment-expression.expect.md b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/ternary-assignment-expression.expect.md index b99e49c872..0b158d84cc 100644 --- a/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/ternary-assignment-expression.expect.md +++ b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/ternary-assignment-expression.expect.md @@ -20,8 +20,7 @@ export const FIXTURE_ENTRYPOINT = { ```javascript function ternary(props) { - let x = undefined; - + let x; const y = props.a ? (x = 1) : (x = 2); return x + y; } diff --git a/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/try-catch-try-immediately-returns.expect.md b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/try-catch-try-immediately-returns.expect.md index acb5734487..d683100ce9 100644 --- a/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/try-catch-try-immediately-returns.expect.md +++ b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/try-catch-try-immediately-returns.expect.md @@ -26,6 +26,7 @@ export const FIXTURE_ENTRYPOINT = { ```javascript function Component(props) { + let x; return 42; } diff --git a/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/try-catch-try-immediately-throws-after-constant-propagation.expect.md b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/try-catch-try-immediately-throws-after-constant-propagation.expect.md index 9003edefd8..c5f3c38a19 100644 --- a/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/try-catch-try-immediately-throws-after-constant-propagation.expect.md +++ b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/try-catch-try-immediately-throws-after-constant-propagation.expect.md @@ -26,6 +26,7 @@ export const FIXTURE_ENTRYPOINT = { ```javascript function Component(props) { + let x; return 42; } diff --git a/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/try-catch.expect.md b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/try-catch.expect.md index 8be44e9a7c..27674dc596 100644 --- a/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/try-catch.expect.md +++ b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/try-catch.expect.md @@ -29,7 +29,7 @@ const { throwErrorWithMessage } = require("shared-runtime"); function Component(props) { const $ = useMemoCache(1); - let x = undefined; + let x; try { let t0; if ($[0] === Symbol.for("react.memo_cache_sentinel")) { diff --git a/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/unused-conditional.expect.md b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/unused-conditional.expect.md index 5d74bf4402..3d38dbfd2f 100644 --- a/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/unused-conditional.expect.md +++ b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/unused-conditional.expect.md @@ -20,8 +20,7 @@ export const FIXTURE_ENTRYPOINT = { ```javascript function Component(props) { - let x = undefined; - + let x; ((x = 1), 1) && (x = 2); return x; } diff --git a/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/unused-logical.expect.md b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/unused-logical.expect.md index 6483c731ab..28c77cea11 100644 --- a/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/unused-logical.expect.md +++ b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/unused-logical.expect.md @@ -20,8 +20,7 @@ export const FIXTURE_ENTRYPOINT = { ```javascript function Component(props) { - let x = undefined; - + let x; props.cond ? (x = 1) : (x = 2); return x; } diff --git a/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/useMemo-switch-return.expect.md b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/useMemo-switch-return.expect.md index 764d57a776..b18bf38d03 100644 --- a/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/useMemo-switch-return.expect.md +++ b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/useMemo-switch-return.expect.md @@ -36,7 +36,7 @@ export const FIXTURE_ENTRYPOINT = { function Component(props) { let t49; bb11: { - let y = undefined; + let y; bb2: switch (props.switch) { case "foo": { t49 = "foo"; diff --git a/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/while-with-assignment-in-test.expect.md b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/while-with-assignment-in-test.expect.md new file mode 100644 index 0000000000..b062e08480 --- /dev/null +++ b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/while-with-assignment-in-test.expect.md @@ -0,0 +1,41 @@ + +## Input + +```javascript +function Component() { + const queue = [1, 2, 3]; + let value = 0; + let sum = 0; + while ((value = queue.pop()) != null) { + sum += value; + } + return sum; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [], +}; + +``` + +## Code + +```javascript +function Component() { + const queue = [1, 2, 3]; + let value; + let sum = 0; + while ((value = queue.pop()) != null) { + sum = sum + value; + } + return sum; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [], +}; + +``` + \ No newline at end of file diff --git a/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/error.while-with-assignment-in-test.js b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/while-with-assignment-in-test.js similarity index 59% rename from compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/error.while-with-assignment-in-test.js rename to compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/while-with-assignment-in-test.js index 0ad8369a39..295bf27d3f 100644 --- a/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/error.while-with-assignment-in-test.js +++ b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/while-with-assignment-in-test.js @@ -1,10 +1,14 @@ -function f(reader) { +function Component() { const queue = [1, 2, 3]; let value = 0; let sum = 0; - // BUG: we need to codegen the complex test expression while ((value = queue.pop()) != null) { sum += value; } return sum; } + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [], +}; diff --git a/compiler/packages/sprout/src/SproutTodoFilter.ts b/compiler/packages/sprout/src/SproutTodoFilter.ts index 88f7a408b2..94b04a9680 100644 --- a/compiler/packages/sprout/src/SproutTodoFilter.ts +++ b/compiler/packages/sprout/src/SproutTodoFilter.ts @@ -416,8 +416,6 @@ const skipFilter = new Set([ "readonly-object-method-calls", "readonly-object-method-calls-mutable-lambda", - "bug.reactive-control-dependency-do-while-test", - // TODO: 🌲 "forest-basic",