[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.
This commit is contained in:
Sathya Gunasekaran
2023-01-11 15:17:13 +00:00
parent ed63dba739
commit 986c95bc09
2 changed files with 76 additions and 0 deletions
@@ -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;
}
```
@@ -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;
}