Skip to content

Commit aade007

Browse files
committed
[compiler] Fix <ValidateMemoization>
By accident we were only ever checking the compiled output, but the intention was in general to be able to compare memoization with/without forget.
1 parent afe22b9 commit aade007

21 files changed

+141
-74
lines changed

compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/todo-control-flow-sensitive-mutation.expect.md

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,9 @@ function Component({a, b, c}: {a: number; b: number; c: number}) {
2323

2424
return (
2525
<>
26-
<ValidateMemoization inputs={[a, b, c]} output={x} alwaysCheck={true} />;
26+
<ValidateMemoization inputs={[a, b, c]} output={x} />;
2727
{/* TODO: should only depend on c */}
28-
<ValidateMemoization
29-
inputs={[a, b, c]}
30-
output={x[0]}
31-
alwaysCheck={true}
32-
/>
33-
;
28+
<ValidateMemoization inputs={[a, b, c]} output={x[0]} />;
3429
</>
3530
);
3631
}
@@ -98,7 +93,7 @@ function Component(t0) {
9893
}
9994
let t3;
10095
if ($[9] !== t2 || $[10] !== x) {
101-
t3 = <ValidateMemoization inputs={t2} output={x} alwaysCheck={true} />;
96+
t3 = <ValidateMemoization inputs={t2} output={x} />;
10297
$[9] = t2;
10398
$[10] = x;
10499
$[11] = t3;
@@ -117,7 +112,7 @@ function Component(t0) {
117112
}
118113
let t5;
119114
if ($[16] !== t4 || $[17] !== x[0]) {
120-
t5 = <ValidateMemoization inputs={t4} output={x[0]} alwaysCheck={true} />;
115+
t5 = <ValidateMemoization inputs={t4} output={x[0]} />;
121116
$[16] = t4;
122117
$[17] = x[0];
123118
$[18] = t5;

compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/todo-control-flow-sensitive-mutation.tsx

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,9 @@ function Component({a, b, c}: {a: number; b: number; c: number}) {
1919

2020
return (
2121
<>
22-
<ValidateMemoization inputs={[a, b, c]} output={x} alwaysCheck={true} />;
22+
<ValidateMemoization inputs={[a, b, c]} output={x} />;
2323
{/* TODO: should only depend on c */}
24-
<ValidateMemoization
25-
inputs={[a, b, c]}
26-
output={x[0]}
27-
alwaysCheck={true}
28-
/>
29-
;
24+
<ValidateMemoization inputs={[a, b, c]} output={x[0]} />;
3025
</>
3126
);
3227
}

compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/todo-transitivity-createfrom-capture-lambda.expect.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ function Component({a, b}) {
2222
typedMutate(z, b);
2323

2424
// TODO: this *should* only depend on `a`
25-
return <ValidateMemoization inputs={[a, b]} output={x} alwaysCheck={true} />;
25+
return <ValidateMemoization inputs={[a, b]} output={x} />;
2626
}
2727

2828
export const FIXTURE_ENTRYPOINT = {
@@ -86,7 +86,7 @@ function Component(t0) {
8686
}
8787
let t3;
8888
if ($[7] !== t2 || $[8] !== x) {
89-
t3 = <ValidateMemoization inputs={t2} output={x} alwaysCheck={true} />;
89+
t3 = <ValidateMemoization inputs={t2} output={x} />;
9090
$[7] = t2;
9191
$[8] = x;
9292
$[9] = t3;

compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/todo-transitivity-createfrom-capture-lambda.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ function Component({a, b}) {
1818
typedMutate(z, b);
1919

2020
// TODO: this *should* only depend on `a`
21-
return <ValidateMemoization inputs={[a, b]} output={x} alwaysCheck={true} />;
21+
return <ValidateMemoization inputs={[a, b]} output={x} />;
2222
}
2323

2424
export const FIXTURE_ENTRYPOINT = {

compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/transitivity-add-captured-array-to-itself.expect.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ function Component({a, b}) {
2020

2121
return (
2222
<>
23-
<ValidateMemoization inputs={[a]} output={o} alwaysCheck={true} />;
24-
<ValidateMemoization inputs={[a, b]} output={x} alwaysCheck={true} />;
23+
<ValidateMemoization inputs={[a]} output={o} />;
24+
<ValidateMemoization inputs={[a, b]} output={x} />;
2525
</>
2626
);
2727
}
@@ -92,7 +92,7 @@ function Component(t0) {
9292
}
9393
let t5;
9494
if ($[8] !== o || $[9] !== t4) {
95-
t5 = <ValidateMemoization inputs={t4} output={o} alwaysCheck={true} />;
95+
t5 = <ValidateMemoization inputs={t4} output={o} />;
9696
$[8] = o;
9797
$[9] = t4;
9898
$[10] = t5;
@@ -110,7 +110,7 @@ function Component(t0) {
110110
}
111111
let t7;
112112
if ($[14] !== t6 || $[15] !== x) {
113-
t7 = <ValidateMemoization inputs={t6} output={x} alwaysCheck={true} />;
113+
t7 = <ValidateMemoization inputs={t6} output={x} />;
114114
$[14] = t6;
115115
$[15] = x;
116116
$[16] = t7;

compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/transitivity-add-captured-array-to-itself.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ function Component({a, b}) {
1616

1717
return (
1818
<>
19-
<ValidateMemoization inputs={[a]} output={o} alwaysCheck={true} />;
20-
<ValidateMemoization inputs={[a, b]} output={x} alwaysCheck={true} />;
19+
<ValidateMemoization inputs={[a]} output={o} />;
20+
<ValidateMemoization inputs={[a, b]} output={x} />;
2121
</>
2222
);
2323
}

compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/transitivity-capture-createfrom-lambda.expect.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ function Component({a, b}: {a: number; b: number}) {
2121
// mutates x
2222
typedMutate(z, b);
2323

24-
return <ValidateMemoization inputs={[a, b]} output={x} alwaysCheck={true} />;
24+
return <ValidateMemoization inputs={[a, b]} output={x} />;
2525
}
2626

2727
export const FIXTURE_ENTRYPOINT = {
@@ -85,7 +85,7 @@ function Component(t0) {
8585
}
8686
let t3;
8787
if ($[7] !== t2 || $[8] !== x) {
88-
t3 = <ValidateMemoization inputs={t2} output={x} alwaysCheck={true} />;
88+
t3 = <ValidateMemoization inputs={t2} output={x} />;
8989
$[7] = t2;
9090
$[8] = x;
9191
$[9] = t3;

compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/transitivity-capture-createfrom-lambda.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ function Component({a, b}: {a: number; b: number}) {
1717
// mutates x
1818
typedMutate(z, b);
1919

20-
return <ValidateMemoization inputs={[a, b]} output={x} alwaysCheck={true} />;
20+
return <ValidateMemoization inputs={[a, b]} output={x} />;
2121
}
2222

2323
export const FIXTURE_ENTRYPOINT = {

compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/transitivity-capture-createfrom.expect.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ function Component({a, b}: {a: number; b: number}) {
1717
// mutates x
1818
typedMutate(z, b);
1919

20-
return <ValidateMemoization inputs={[a, b]} output={x} alwaysCheck={true} />;
20+
return <ValidateMemoization inputs={[a, b]} output={x} />;
2121
}
2222

2323
export const FIXTURE_ENTRYPOINT = {
@@ -76,7 +76,7 @@ function Component(t0) {
7676
}
7777
let t3;
7878
if ($[7] !== t2 || $[8] !== x) {
79-
t3 = <ValidateMemoization inputs={t2} output={x} alwaysCheck={true} />;
79+
t3 = <ValidateMemoization inputs={t2} output={x} />;
8080
$[7] = t2;
8181
$[8] = x;
8282
$[9] = t3;

compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/transitivity-capture-createfrom.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ function Component({a, b}: {a: number; b: number}) {
1313
// mutates x
1414
typedMutate(z, b);
1515

16-
return <ValidateMemoization inputs={[a, b]} output={x} alwaysCheck={true} />;
16+
return <ValidateMemoization inputs={[a, b]} output={x} />;
1717
}
1818

1919
export const FIXTURE_ENTRYPOINT = {

compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/transitivity-createfrom-capture.expect.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ function Component({a, b}) {
1717
// does not mutate x, so x should not depend on b
1818
typedMutate(z, b);
1919

20-
return <ValidateMemoization inputs={[a]} output={x} alwaysCheck={true} />;
20+
return <ValidateMemoization inputs={[a]} output={x} />;
2121
}
2222

2323
export const FIXTURE_ENTRYPOINT = {
@@ -73,7 +73,7 @@ function Component(t0) {
7373
}
7474
let t4;
7575
if ($[4] !== t3 || $[5] !== x) {
76-
t4 = <ValidateMemoization inputs={t3} output={x} alwaysCheck={true} />;
76+
t4 = <ValidateMemoization inputs={t3} output={x} />;
7777
$[4] = t3;
7878
$[5] = x;
7979
$[6] = t4;

compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/transitivity-createfrom-capture.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ function Component({a, b}) {
1313
// does not mutate x, so x should not depend on b
1414
typedMutate(z, b);
1515

16-
return <ValidateMemoization inputs={[a]} output={x} alwaysCheck={true} />;
16+
return <ValidateMemoization inputs={[a]} output={x} />;
1717
}
1818

1919
export const FIXTURE_ENTRYPOINT = {

compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/transitivity-phi-assign-or-capture.expect.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ function Component({a, b}) {
2121
// could mutate x
2222
typedMutate(z, b);
2323

24-
return <ValidateMemoization inputs={[a, b]} output={x} alwaysCheck={true} />;
24+
return <ValidateMemoization inputs={[a, b]} output={x} />;
2525
}
2626

2727
export const FIXTURE_ENTRYPOINT = {
@@ -92,7 +92,7 @@ function Component(t0) {
9292
}
9393
let t4;
9494
if ($[9] !== t3 || $[10] !== x) {
95-
t4 = <ValidateMemoization inputs={t3} output={x} alwaysCheck={true} />;
95+
t4 = <ValidateMemoization inputs={t3} output={x} />;
9696
$[9] = t3;
9797
$[10] = x;
9898
$[11] = t4;

compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/transitivity-phi-assign-or-capture.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ function Component({a, b}) {
1717
// could mutate x
1818
typedMutate(z, b);
1919

20-
return <ValidateMemoization inputs={[a, b]} output={x} alwaysCheck={true} />;
20+
return <ValidateMemoization inputs={[a, b]} output={x} />;
2121
}
2222

2323
export const FIXTURE_ENTRYPOINT = {

compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/typed-identity-function-frozen-input.expect.md

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
```javascript
55
// @enableNewMutationAliasingModel
66

7+
import {useMemo} from 'react';
78
import {
89
identity,
910
makeObject_Primitives,
@@ -14,7 +15,7 @@ import {
1415

1516
function Component({a, b}) {
1617
// create a mutable value with input `a`
17-
const x = makeObject_Primitives(a);
18+
const x = useMemo(() => makeObject_Primitives(a), [a]);
1819

1920
// freeze the value
2021
useIdentity(x);
@@ -49,6 +50,7 @@ export const FIXTURE_ENTRYPOINT = {
4950
```javascript
5051
import { c as _c } from "react/compiler-runtime"; // @enableNewMutationAliasingModel
5152

53+
import { useMemo } from "react";
5254
import {
5355
identity,
5456
makeObject_Primitives,
@@ -61,38 +63,40 @@ function Component(t0) {
6163
const $ = _c(7);
6264
const { a, b } = t0;
6365
let t1;
66+
let t2;
6467
if ($[0] !== a) {
65-
t1 = makeObject_Primitives(a);
68+
t2 = makeObject_Primitives(a);
6669
$[0] = a;
67-
$[1] = t1;
70+
$[1] = t2;
6871
} else {
69-
t1 = $[1];
72+
t2 = $[1];
7073
}
74+
t1 = t2;
7175
const x = t1;
7276

7377
useIdentity(x);
7478

7579
const x2 = typedIdentity(x);
7680

7781
identity(x2, b);
78-
let t2;
82+
let t3;
7983
if ($[2] !== a) {
80-
t2 = [a];
84+
t3 = [a];
8185
$[2] = a;
82-
$[3] = t2;
86+
$[3] = t3;
8387
} else {
84-
t2 = $[3];
88+
t3 = $[3];
8589
}
86-
let t3;
87-
if ($[4] !== t2 || $[5] !== x) {
88-
t3 = <ValidateMemoization inputs={t2} output={x} />;
89-
$[4] = t2;
90+
let t4;
91+
if ($[4] !== t3 || $[5] !== x) {
92+
t4 = <ValidateMemoization inputs={t3} output={x} />;
93+
$[4] = t3;
9094
$[5] = x;
91-
$[6] = t3;
95+
$[6] = t4;
9296
} else {
93-
t3 = $[6];
97+
t4 = $[6];
9498
}
95-
return t3;
99+
return t4;
96100
}
97101

98102
export const FIXTURE_ENTRYPOINT = {

compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/typed-identity-function-frozen-input.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
// @enableNewMutationAliasingModel
22

3+
import {useMemo} from 'react';
34
import {
45
identity,
56
makeObject_Primitives,
@@ -10,7 +11,7 @@ import {
1011

1112
function Component({a, b}) {
1213
// create a mutable value with input `a`
13-
const x = makeObject_Primitives(a);
14+
const x = useMemo(() => makeObject_Primitives(a), [a]);
1415

1516
// freeze the value
1617
useIdentity(x);

0 commit comments

Comments
 (0)