From 986c95bc09189f81f5a6a324f36195fe9bafcf47 Mon Sep 17 00:00:00 2001 From: Sathya Gunasekaran Date: Wed, 11 Jan 2023 15:17:13 +0000 Subject: [PATCH] [hir] Add test case for incorrect memoisation of mutated objects In this case, x should not be cached within the if or else blocks as it is mutated later. --- .../hir/obj-mutated-after-if-else.expect.md | 65 +++++++++++++++++++ .../fixtures/hir/obj-mutated-after-if-else.js | 11 ++++ 2 files changed, 76 insertions(+) create mode 100644 compiler/forget/src/__tests__/fixtures/hir/obj-mutated-after-if-else.expect.md create mode 100644 compiler/forget/src/__tests__/fixtures/hir/obj-mutated-after-if-else.js diff --git a/compiler/forget/src/__tests__/fixtures/hir/obj-mutated-after-if-else.expect.md b/compiler/forget/src/__tests__/fixtures/hir/obj-mutated-after-if-else.expect.md new file mode 100644 index 0000000000..a963142635 --- /dev/null +++ b/compiler/forget/src/__tests__/fixtures/hir/obj-mutated-after-if-else.expect.md @@ -0,0 +1,65 @@ + +## Input + +```javascript +function foo(a, b, c, d) { + let x = someObj(); + if (a) { + x = someObj(); + } else { + x = someObj(); + } + + x.f = 1; + return x; +} + +``` + +## Code + +```javascript +function foo(a, b, c, d) { + const $ = React.useMemoCache(); + const x = someObj(); + const c_0 = $[0] !== a; + let x$0; + if (c_0) { + x$0 = undefined; + + if (a) { + let x$1; + + if ($[2] === Symbol.for("react.memo_cache_sentinel")) { + x$1 = someObj(); + $[2] = x$1; + } else { + x$1 = $[2]; + } + + x$0 = x$1; + } else { + let x$2; + + if ($[3] === Symbol.for("react.memo_cache_sentinel")) { + x$2 = someObj(); + $[3] = x$2; + } else { + x$2 = $[3]; + } + + x$0 = x$2; + } + + x$0.f = 1; + $[0] = a; + $[1] = x$0; + } else { + x$0 = $[1]; + } + + return x$0; +} + +``` + \ No newline at end of file diff --git a/compiler/forget/src/__tests__/fixtures/hir/obj-mutated-after-if-else.js b/compiler/forget/src/__tests__/fixtures/hir/obj-mutated-after-if-else.js new file mode 100644 index 0000000000..ed43dec122 --- /dev/null +++ b/compiler/forget/src/__tests__/fixtures/hir/obj-mutated-after-if-else.js @@ -0,0 +1,11 @@ +function foo(a, b, c, d) { + let x = someObj(); + if (a) { + x = someObj(); + } else { + x = someObj(); + } + + x.f = 1; + return x; +}