diff --git a/compiler/forget/src/Inference/AnalyseFunctions.ts b/compiler/forget/src/Inference/AnalyseFunctions.ts index c8843b9f9a..7257b4389a 100644 --- a/compiler/forget/src/Inference/AnalyseFunctions.ts +++ b/compiler/forget/src/Inference/AnalyseFunctions.ts @@ -62,6 +62,12 @@ export default function analyseFunctions(func: HIRFunction): void { ); break; } + case "ComputedLoad": { + // The path is set to an empty string as the path doesn't really + // matter for a computed load. + state.declareProperty(instr.lvalue, instr.value.object, ""); + break; + } case "LoadLocal": { if (instr.lvalue.identifier.name === null) { state.declareTemporary(instr.lvalue, instr.value.place); diff --git a/compiler/forget/src/__tests__/fixtures/compiler/capturing-function-alias-computed-load-2.expect.md b/compiler/forget/src/__tests__/fixtures/compiler/capturing-function-alias-computed-load-2.expect.md new file mode 100644 index 0000000000..60be692813 --- /dev/null +++ b/compiler/forget/src/__tests__/fixtures/compiler/capturing-function-alias-computed-load-2.expect.md @@ -0,0 +1,39 @@ + +## Input + +```javascript +function bar(a) { + let x = [a]; + let y = {}; + (function () { + y = x[0][1]; + })(); + + return y; +} + +``` + +## Code + +```javascript +function bar(a) { + const $ = React.unstable_useMemoCache(2); + const c_0 = $[0] !== a; + let y; + if (c_0) { + const x = [a]; + y = {}; + (function () { + y = x[0][1]; + })(); + $[0] = a; + $[1] = y; + } else { + y = $[1]; + } + return y; +} + +``` + \ No newline at end of file diff --git a/compiler/forget/src/__tests__/fixtures/compiler/capturing-function-alias-computed-load-2.js b/compiler/forget/src/__tests__/fixtures/compiler/capturing-function-alias-computed-load-2.js new file mode 100644 index 0000000000..e2de4e1be4 --- /dev/null +++ b/compiler/forget/src/__tests__/fixtures/compiler/capturing-function-alias-computed-load-2.js @@ -0,0 +1,9 @@ +function bar(a) { + let x = [a]; + let y = {}; + (function () { + y = x[0][1]; + })(); + + return y; +} diff --git a/compiler/forget/src/__tests__/fixtures/compiler/capturing-function-alias-computed-load-3.expect.md b/compiler/forget/src/__tests__/fixtures/compiler/capturing-function-alias-computed-load-3.expect.md new file mode 100644 index 0000000000..95dc2829d5 --- /dev/null +++ b/compiler/forget/src/__tests__/fixtures/compiler/capturing-function-alias-computed-load-3.expect.md @@ -0,0 +1,45 @@ + +## Input + +```javascript +function bar(a, b) { + let x = [a, b]; + let y = {}; + let t = {}; + (function () { + y = x[0][1]; + t = x[1][0]; + })(); + + return y; +} + +``` + +## Code + +```javascript +function bar(a, b) { + const $ = React.unstable_useMemoCache(3); + const c_0 = $[0] !== a; + const c_1 = $[1] !== b; + let y; + if (c_0 || c_1) { + const x = [a, b]; + y = {}; + const t = {}; + (function () { + y = x[0][1]; + t = x[1][0]; + })(); + $[0] = a; + $[1] = b; + $[2] = y; + } else { + y = $[2]; + } + return y; +} + +``` + \ No newline at end of file diff --git a/compiler/forget/src/__tests__/fixtures/compiler/capturing-function-alias-computed-load-3.js b/compiler/forget/src/__tests__/fixtures/compiler/capturing-function-alias-computed-load-3.js new file mode 100644 index 0000000000..c9ecd39960 --- /dev/null +++ b/compiler/forget/src/__tests__/fixtures/compiler/capturing-function-alias-computed-load-3.js @@ -0,0 +1,11 @@ +function bar(a, b) { + let x = [a, b]; + let y = {}; + let t = {}; + (function () { + y = x[0][1]; + t = x[1][0]; + })(); + + return y; +} diff --git a/compiler/forget/src/__tests__/fixtures/compiler/capturing-function-alias-computed-load-4.expect.md b/compiler/forget/src/__tests__/fixtures/compiler/capturing-function-alias-computed-load-4.expect.md new file mode 100644 index 0000000000..bb860b98ca --- /dev/null +++ b/compiler/forget/src/__tests__/fixtures/compiler/capturing-function-alias-computed-load-4.expect.md @@ -0,0 +1,39 @@ + +## Input + +```javascript +function bar(a) { + let x = [a]; + let y = {}; + (function () { + y = x[0].a[1]; + })(); + + return y; +} + +``` + +## Code + +```javascript +function bar(a) { + const $ = React.unstable_useMemoCache(2); + const c_0 = $[0] !== a; + let y; + if (c_0) { + const x = [a]; + y = {}; + (function () { + y = x[0].a[1]; + })(); + $[0] = a; + $[1] = y; + } else { + y = $[1]; + } + return y; +} + +``` + \ No newline at end of file diff --git a/compiler/forget/src/__tests__/fixtures/compiler/capturing-function-alias-computed-load-4.js b/compiler/forget/src/__tests__/fixtures/compiler/capturing-function-alias-computed-load-4.js new file mode 100644 index 0000000000..ef52aa19e8 --- /dev/null +++ b/compiler/forget/src/__tests__/fixtures/compiler/capturing-function-alias-computed-load-4.js @@ -0,0 +1,9 @@ +function bar(a) { + let x = [a]; + let y = {}; + (function () { + y = x[0].a[1]; + })(); + + return y; +} diff --git a/compiler/forget/src/__tests__/fixtures/compiler/capturing-function-alias-computed-load.expect.md b/compiler/forget/src/__tests__/fixtures/compiler/capturing-function-alias-computed-load.expect.md new file mode 100644 index 0000000000..1c8780a0de --- /dev/null +++ b/compiler/forget/src/__tests__/fixtures/compiler/capturing-function-alias-computed-load.expect.md @@ -0,0 +1,39 @@ + +## Input + +```javascript +function bar(a) { + let x = [a]; + let y = {}; + (function () { + y = x[0]; + })(); + + return y; +} + +``` + +## Code + +```javascript +function bar(a) { + const $ = React.unstable_useMemoCache(2); + const c_0 = $[0] !== a; + let y; + if (c_0) { + const x = [a]; + y = {}; + (function () { + y = x[0]; + })(); + $[0] = a; + $[1] = y; + } else { + y = $[1]; + } + return y; +} + +``` + \ No newline at end of file diff --git a/compiler/forget/src/__tests__/fixtures/compiler/capturing-function-alias-computed-load.js b/compiler/forget/src/__tests__/fixtures/compiler/capturing-function-alias-computed-load.js new file mode 100644 index 0000000000..1811e87081 --- /dev/null +++ b/compiler/forget/src/__tests__/fixtures/compiler/capturing-function-alias-computed-load.js @@ -0,0 +1,9 @@ +function bar(a) { + let x = [a]; + let y = {}; + (function () { + y = x[0]; + })(); + + return y; +}