mirror of
https://github.com/facebook/react.git
synced 2025-11-01 09:12:30 +00:00
[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:
@@ -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;
|
||||
}
|
||||
Reference in New Issue
Block a user