diff --git a/compiler/forget/src/SSA/EnterSSA.ts b/compiler/forget/src/SSA/EnterSSA.ts index d34c9e773b..8bd7b60667 100644 --- a/compiler/forget/src/SSA/EnterSSA.ts +++ b/compiler/forget/src/SSA/EnterSSA.ts @@ -36,6 +36,7 @@ class SSABuilder { #blocks: Map; #env: Environment; #unknown: Set = new Set(); + #context: Set = new Set(); constructor(env: Environment, blocks: Map) { this.#blocks = blocks; @@ -67,6 +68,12 @@ class SSABuilder { }; } + defineContext(oldPlace: Place): Place { + const newPlace = this.definePlace(oldPlace); + this.#context.add(oldPlace.identifier); + return newPlace; + } + definePlace(oldPlace: Place): Place { const oldId = oldPlace.identifier; if (this.#unknown.has(oldId)) { @@ -78,6 +85,11 @@ class SSABuilder { ); } + // Do not redefine context references. + if (this.#context.has(oldId)) { + return this.getPlace(oldPlace); + } + const newId = this.makeId(oldId); this.state().defs.set(oldId, newId); return { @@ -206,7 +218,7 @@ export default function enterSSA(func: HIRFunction): void { builder.startBlock(block); if (func.body.entry === blockId) { - func.context = func.context.map((p) => builder.definePlace(p)); + func.context = func.context.map((p) => builder.defineContext(p)); func.params = func.params.map((p) => builder.definePlace(p)); } diff --git a/compiler/forget/src/__tests__/fixtures/hir/_bug.capturing-func-simple-alias.expect.md b/compiler/forget/src/__tests__/fixtures/hir/capturing-func-simple-alias.expect.md similarity index 63% rename from compiler/forget/src/__tests__/fixtures/hir/_bug.capturing-func-simple-alias.expect.md rename to compiler/forget/src/__tests__/fixtures/hir/capturing-func-simple-alias.expect.md index 84e2a114fc..e090d8cbc1 100644 --- a/compiler/forget/src/__tests__/fixtures/hir/_bug.capturing-func-simple-alias.expect.md +++ b/compiler/forget/src/__tests__/fixtures/hir/capturing-func-simple-alias.expect.md @@ -18,29 +18,20 @@ function component(a) { ```javascript function component(a) { - const $ = React.unstable_useMemoCache(4); + const $ = React.unstable_useMemoCache(2); const c_0 = $[0] !== a; - let t0; - if (c_0) { - t0 = { a: a }; - $[0] = a; - $[1] = t0; - } else { - t0 = $[1]; - } - const x = t0; - const c_2 = $[2] !== x; let y; - if (c_2) { + if (c_0) { + const x = { a: a }; y = {}; (function () { y = x; })(); mutate(y); - $[2] = x; - $[3] = y; + $[0] = a; + $[1] = y; } else { - y = $[3]; + y = $[1]; } return y; } diff --git a/compiler/forget/src/__tests__/fixtures/hir/_bug.capturing-func-simple-alias.js b/compiler/forget/src/__tests__/fixtures/hir/capturing-func-simple-alias.js similarity index 100% rename from compiler/forget/src/__tests__/fixtures/hir/_bug.capturing-func-simple-alias.js rename to compiler/forget/src/__tests__/fixtures/hir/capturing-func-simple-alias.js