Skip to content

Commit bb3e970

Browse files
committed
[compiler] Enable new inference by default
1 parent 42c25a4 commit bb3e970

25 files changed

+277
-203
lines changed

compiler/packages/babel-plugin-react-compiler/src/HIR/Environment.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,7 @@ export const EnvironmentConfigSchema = z.object({
246246
/**
247247
* Enable a new model for mutability and aliasing inference
248248
*/
249-
enableNewMutationAliasingModel: z.boolean().default(false),
249+
enableNewMutationAliasingModel: z.boolean().default(true),
250250

251251
/**
252252
* Enables inference of optional dependency chains. Without this flag

compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/aliased-nested-scope-truncated-dep.expect.md

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -175,21 +175,14 @@ import {
175175
* and mutability.
176176
*/
177177
function Component(t0) {
178-
const $ = _c(4);
178+
const $ = _c(2);
179179
const { prop } = t0;
180180
let t1;
181181
if ($[0] !== prop) {
182182
const obj = shallowCopy(prop);
183183
const aliasedObj = identity(obj);
184-
let t2;
185-
if ($[2] !== obj) {
186-
t2 = [obj.id];
187-
$[2] = obj;
188-
$[3] = t2;
189-
} else {
190-
t2 = $[3];
191-
}
192-
const id = t2;
184+
185+
const id = [obj.id];
193186

194187
mutate(aliasedObj);
195188
setPropertyByKey(aliasedObj, "id", prop.id + 1);

compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/capturing-function-alias-computed-load-2-iife.expect.md

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,17 +25,25 @@ export const FIXTURE_ENTRYPOINT = {
2525
```javascript
2626
import { c as _c } from "react/compiler-runtime";
2727
function bar(a) {
28-
const $ = _c(2);
29-
let y;
28+
const $ = _c(4);
29+
let t0;
3030
if ($[0] !== a) {
31-
const x = [a];
31+
t0 = [a];
32+
$[0] = a;
33+
$[1] = t0;
34+
} else {
35+
t0 = $[1];
36+
}
37+
const x = t0;
38+
let y;
39+
if ($[2] !== x[0][1]) {
3240
y = {};
3341

3442
y = x[0][1];
35-
$[0] = a;
36-
$[1] = y;
43+
$[2] = x[0][1];
44+
$[3] = y;
3745
} else {
38-
y = $[1];
46+
y = $[3];
3947
}
4048
return y;
4149
}

compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/capturing-function-alias-computed-load-3-iife.expect.md

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,20 +29,29 @@ export const FIXTURE_ENTRYPOINT = {
2929
```javascript
3030
import { c as _c } from "react/compiler-runtime";
3131
function bar(a, b) {
32-
const $ = _c(3);
33-
let y;
32+
const $ = _c(6);
33+
let t0;
3434
if ($[0] !== a || $[1] !== b) {
35-
const x = [a, b];
35+
t0 = [a, b];
36+
$[0] = a;
37+
$[1] = b;
38+
$[2] = t0;
39+
} else {
40+
t0 = $[2];
41+
}
42+
const x = t0;
43+
let y;
44+
if ($[3] !== x[0][1] || $[4] !== x[1][0]) {
3645
y = {};
3746
let t = {};
3847

3948
y = x[0][1];
4049
t = x[1][0];
41-
$[0] = a;
42-
$[1] = b;
43-
$[2] = y;
50+
$[3] = x[0][1];
51+
$[4] = x[1][0];
52+
$[5] = y;
4453
} else {
45-
y = $[2];
54+
y = $[5];
4655
}
4756
return y;
4857
}

compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/capturing-function-alias-computed-load-4-iife.expect.md

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,17 +25,25 @@ export const FIXTURE_ENTRYPOINT = {
2525
```javascript
2626
import { c as _c } from "react/compiler-runtime";
2727
function bar(a) {
28-
const $ = _c(2);
29-
let y;
28+
const $ = _c(4);
29+
let t0;
3030
if ($[0] !== a) {
31-
const x = [a];
31+
t0 = [a];
32+
$[0] = a;
33+
$[1] = t0;
34+
} else {
35+
t0 = $[1];
36+
}
37+
const x = t0;
38+
let y;
39+
if ($[2] !== x[0].a[1]) {
3240
y = {};
3341

3442
y = x[0].a[1];
35-
$[0] = a;
36-
$[1] = y;
43+
$[2] = x[0].a[1];
44+
$[3] = y;
3745
} else {
38-
y = $[1];
46+
y = $[3];
3947
}
4048
return y;
4149
}

compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/capturing-function-alias-computed-load-iife.expect.md

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,17 +24,25 @@ export const FIXTURE_ENTRYPOINT = {
2424
```javascript
2525
import { c as _c } from "react/compiler-runtime";
2626
function bar(a) {
27-
const $ = _c(2);
28-
let y;
27+
const $ = _c(4);
28+
let t0;
2929
if ($[0] !== a) {
30-
const x = [a];
30+
t0 = [a];
31+
$[0] = a;
32+
$[1] = t0;
33+
} else {
34+
t0 = $[1];
35+
}
36+
const x = t0;
37+
let y;
38+
if ($[2] !== x[0]) {
3139
y = {};
3240

3341
y = x[0];
34-
$[0] = a;
35-
$[1] = y;
42+
$[2] = x[0];
43+
$[3] = y;
3644
} else {
37-
y = $[1];
45+
y = $[3];
3846
}
3947
return y;
4048
}

compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/error.invalid-impure-functions-in-render.expect.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ function Component() {
2020
2 |
2121
3 | function Component() {
2222
> 4 | const date = Date.now();
23-
| ^^^^^^^^ InvalidReact: Calling an impure function can produce unstable results. (https://react.dev/reference/rules/components-and-hooks-must-be-pure#components-and-hooks-must-be-idempotent). `Date.now` is an impure function whose results may change on every call (4:4)
23+
| ^^^^^^^^^^ InvalidReact: Calling an impure function can produce unstable results. (https://react.dev/reference/rules/components-and-hooks-must-be-pure#components-and-hooks-must-be-idempotent). `Date.now` is an impure function whose results may change on every call (4:4)
2424
2525
InvalidReact: Calling an impure function can produce unstable results. (https://react.dev/reference/rules/components-and-hooks-must-be-pure#components-and-hooks-must-be-idempotent). `performance.now` is an impure function whose results may change on every call (5:5)
2626

compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/error.invalid-non-imported-reanimated-shared-value-writes.expect.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ function SomeComponent() {
2727
9 | return (
2828
10 | <Button
2929
> 11 | onPress={() => (sharedVal.value = Math.random())}
30-
| ^^^^^^^^^ InvalidReact: Mutating a value returned from a function whose return value should not be mutated. Found mutation of `sharedVal` (11:11)
30+
| ^^^^^^^^^ InvalidReact: This mutates a variable that React considers immutable. Found mutation of `sharedVal` (11:11)
3131
12 | title="Randomize"
3232
13 | />
3333
14 | );

compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/error.mutate-hook-argument.expect.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ function useHook(a, b) {
1616
1 | function useHook(a, b) {
1717
> 2 | b.test = 1;
1818
| ^ InvalidReact: Mutating component props or hook arguments is not allowed. Consider using a local variable instead (2:2)
19+
20+
InvalidReact: Mutating component props or hook arguments is not allowed. Consider using a local variable instead (3:3)
1921
3 | a.test = 2;
2022
4 | }
2123
5 |

compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/error.not-useEffect-external-mutate.expect.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ function Component(props) {
2121
4 | foo(() => {
2222
> 5 | x.a = 10;
2323
| ^ InvalidReact: Writing to a variable defined outside a component or hook is not allowed. Consider using an effect (5:5)
24+
25+
InvalidReact: Writing to a variable defined outside a component or hook is not allowed. Consider using an effect (6:6)
2426
6 | x.a = 20;
2527
7 | });
2628
8 | }

compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/error.reassignment-to-global-indirect.expect.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ function Component() {
2121
3 | // Cannot assign to globals
2222
> 4 | someUnknownGlobal = true;
2323
| ^^^^^^^^^^^^^^^^^ InvalidReact: Unexpected reassignment of a variable which was defined outside of the component. Components and hooks should be pure and side-effect free, but variable reassignment is a form of side-effect. If this variable is used in rendering, use useState instead. (https://react.dev/reference/rules/components-and-hooks-must-be-pure#side-effects-must-run-outside-of-render) (4:4)
24+
25+
InvalidReact: Unexpected reassignment of a variable which was defined outside of the component. Components and hooks should be pure and side-effect free, but variable reassignment is a form of side-effect. If this variable is used in rendering, use useState instead. (https://react.dev/reference/rules/components-and-hooks-must-be-pure#side-effects-must-run-outside-of-render) (5:5)
2426
5 | moduleLocal = true;
2527
6 | };
2628
7 | foo();

compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/error.reassignment-to-global.expect.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ function Component() {
1818
2 | // Cannot assign to globals
1919
> 3 | someUnknownGlobal = true;
2020
| ^^^^^^^^^^^^^^^^^ InvalidReact: Unexpected reassignment of a variable which was defined outside of the component. Components and hooks should be pure and side-effect free, but variable reassignment is a form of side-effect. If this variable is used in rendering, use useState instead. (https://react.dev/reference/rules/components-and-hooks-must-be-pure#side-effects-must-run-outside-of-render) (3:3)
21+
22+
InvalidReact: Unexpected reassignment of a variable which was defined outside of the component. Components and hooks should be pure and side-effect free, but variable reassignment is a form of side-effect. If this variable is used in rendering, use useState instead. (https://react.dev/reference/rules/components-and-hooks-must-be-pure#side-effects-must-run-outside-of-render) (4:4)
2123
4 | moduleLocal = true;
2224
5 | }
2325
6 |

compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/error.todo-repro-named-function-with-shadowed-local-same-name.expect.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ function Component(props) {
2222
7 | return hasErrors;
2323
8 | }
2424
> 9 | return hasErrors();
25-
| ^^^^^^^^^ Invariant: [hoisting] Expected value for identifier to be initialized. hasErrors_0$15 (9:9)
25+
| ^^^^^^^^^ Invariant: [InferMutationAliasingEffects] Expected value kind to be initialized. <unknown> hasErrors_0$15:TFunction (9:9)
2626
10 | }
2727
11 |
2828
```

compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/infer-effect-dependencies/bailout-retry/mutate-after-useeffect-optional-chain.expect.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ export const FIXTURE_ENTRYPOINT = {
4848
## Logs
4949
5050
```
51-
{"kind":"CompileError","fnLoc":{"start":{"line":5,"column":0,"index":139},"end":{"line":12,"column":1,"index":384},"filename":"mutate-after-useeffect-optional-chain.ts"},"detail":{"reason":"This mutates a variable that React considers immutable","description":null,"loc":{"start":{"line":10,"column":2,"index":345},"end":{"line":10,"column":5,"index":348},"filename":"mutate-after-useeffect-optional-chain.ts","identifierName":"arr"},"suggestions":null,"severity":"InvalidReact"}}
51+
{"kind":"CompileError","fnLoc":{"start":{"line":5,"column":0,"index":139},"end":{"line":12,"column":1,"index":384},"filename":"mutate-after-useeffect-optional-chain.ts"},"detail":{"reason":"Updating a value used previously in an effect function or as an effect dependency is not allowed. Consider moving the mutation before calling useEffect()","description":null,"severity":"InvalidReact","suggestions":null,"loc":{"start":{"line":10,"column":2,"index":345},"end":{"line":10,"column":5,"index":348},"filename":"mutate-after-useeffect-optional-chain.ts","identifierName":"arr"}}}
5252
{"kind":"AutoDepsDecorations","fnLoc":{"start":{"line":9,"column":2,"index":304},"end":{"line":9,"column":39,"index":341},"filename":"mutate-after-useeffect-optional-chain.ts"},"decorations":[{"start":{"line":9,"column":24,"index":326},"end":{"line":9,"column":27,"index":329},"filename":"mutate-after-useeffect-optional-chain.ts","identifierName":"arr"}]}
5353
{"kind":"CompileSuccess","fnLoc":{"start":{"line":5,"column":0,"index":139},"end":{"line":12,"column":1,"index":384},"filename":"mutate-after-useeffect-optional-chain.ts"},"fnName":"Component","memoSlots":0,"memoBlocks":0,"memoValues":0,"prunedMemoBlocks":0,"prunedMemoValues":0}
5454
```

compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/infer-effect-dependencies/bailout-retry/mutate-after-useeffect-ref-access.expect.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ export const FIXTURE_ENTRYPOINT = {
4747
## Logs
4848

4949
```
50-
{"kind":"CompileError","fnLoc":{"start":{"line":6,"column":0,"index":148},"end":{"line":11,"column":1,"index":311},"filename":"mutate-after-useeffect-ref-access.ts"},"detail":{"reason":"Mutating component props or hook arguments is not allowed. Consider using a local variable instead","description":null,"loc":{"start":{"line":9,"column":2,"index":269},"end":{"line":9,"column":16,"index":283},"filename":"mutate-after-useeffect-ref-access.ts"},"suggestions":null,"severity":"InvalidReact"}}
50+
{"kind":"CompileError","fnLoc":{"start":{"line":6,"column":0,"index":148},"end":{"line":11,"column":1,"index":311},"filename":"mutate-after-useeffect-ref-access.ts"},"detail":{"reason":"Mutating component props or hook arguments is not allowed. Consider using a local variable instead","description":null,"severity":"InvalidReact","suggestions":null,"loc":{"start":{"line":9,"column":2,"index":269},"end":{"line":9,"column":16,"index":283},"filename":"mutate-after-useeffect-ref-access.ts"}}}
5151
{"kind":"AutoDepsDecorations","fnLoc":{"start":{"line":8,"column":2,"index":227},"end":{"line":8,"column":40,"index":265},"filename":"mutate-after-useeffect-ref-access.ts"},"decorations":[{"start":{"line":8,"column":24,"index":249},"end":{"line":8,"column":30,"index":255},"filename":"mutate-after-useeffect-ref-access.ts","identifierName":"arrRef"}]}
5252
{"kind":"CompileSuccess","fnLoc":{"start":{"line":6,"column":0,"index":148},"end":{"line":11,"column":1,"index":311},"filename":"mutate-after-useeffect-ref-access.ts"},"fnName":"Component","memoSlots":0,"memoBlocks":0,"memoValues":0,"prunedMemoBlocks":0,"prunedMemoValues":0}
5353
```

compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/infer-effect-dependencies/bailout-retry/mutate-after-useeffect.expect.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ export const FIXTURE_ENTRYPOINT = {
4747
## Logs
4848

4949
```
50-
{"kind":"CompileError","fnLoc":{"start":{"line":4,"column":0,"index":101},"end":{"line":11,"column":1,"index":222},"filename":"mutate-after-useeffect.ts"},"detail":{"reason":"This mutates a variable that React considers immutable","description":null,"loc":{"start":{"line":9,"column":2,"index":194},"end":{"line":9,"column":5,"index":197},"filename":"mutate-after-useeffect.ts","identifierName":"arr"},"suggestions":null,"severity":"InvalidReact"}}
50+
{"kind":"CompileError","fnLoc":{"start":{"line":4,"column":0,"index":101},"end":{"line":11,"column":1,"index":222},"filename":"mutate-after-useeffect.ts"},"detail":{"reason":"Updating a value used previously in an effect function or as an effect dependency is not allowed. Consider moving the mutation before calling useEffect()","description":null,"severity":"InvalidReact","suggestions":null,"loc":{"start":{"line":9,"column":2,"index":194},"end":{"line":9,"column":5,"index":197},"filename":"mutate-after-useeffect.ts","identifierName":"arr"}}}
5151
{"kind":"AutoDepsDecorations","fnLoc":{"start":{"line":6,"column":2,"index":149},"end":{"line":8,"column":4,"index":190},"filename":"mutate-after-useeffect.ts"},"decorations":[{"start":{"line":7,"column":4,"index":171},"end":{"line":7,"column":7,"index":174},"filename":"mutate-after-useeffect.ts","identifierName":"arr"},{"start":{"line":7,"column":4,"index":171},"end":{"line":7,"column":7,"index":174},"filename":"mutate-after-useeffect.ts","identifierName":"arr"},{"start":{"line":7,"column":13,"index":180},"end":{"line":7,"column":16,"index":183},"filename":"mutate-after-useeffect.ts","identifierName":"foo"}]}
5252
{"kind":"CompileSuccess","fnLoc":{"start":{"line":4,"column":0,"index":101},"end":{"line":11,"column":1,"index":222},"filename":"mutate-after-useeffect.ts"},"fnName":"Component","memoSlots":0,"memoBlocks":0,"memoValues":0,"prunedMemoBlocks":0,"prunedMemoValues":0}
5353
```

compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/infer-effect-dependencies/no-emit/retry-no-emit.expect.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ export const FIXTURE_ENTRYPOINT = {
5252
## Logs
5353

5454
```
55-
{"kind":"CompileError","fnLoc":{"start":{"line":5,"column":0,"index":163},"end":{"line":13,"column":1,"index":357},"filename":"retry-no-emit.ts"},"detail":{"reason":"This mutates a variable that React considers immutable","description":null,"loc":{"start":{"line":11,"column":2,"index":320},"end":{"line":11,"column":6,"index":324},"filename":"retry-no-emit.ts","identifierName":"arr2"},"suggestions":null,"severity":"InvalidReact"}}
55+
{"kind":"CompileError","fnLoc":{"start":{"line":5,"column":0,"index":163},"end":{"line":13,"column":1,"index":357},"filename":"retry-no-emit.ts"},"detail":{"reason":"This mutates a variable that React considers immutable","description":null,"severity":"InvalidReact","suggestions":null,"loc":{"start":{"line":11,"column":2,"index":320},"end":{"line":11,"column":6,"index":324},"filename":"retry-no-emit.ts","identifierName":"arr2"}}}
5656
{"kind":"AutoDepsDecorations","fnLoc":{"start":{"line":7,"column":2,"index":216},"end":{"line":7,"column":36,"index":250},"filename":"retry-no-emit.ts"},"decorations":[{"start":{"line":7,"column":31,"index":245},"end":{"line":7,"column":34,"index":248},"filename":"retry-no-emit.ts","identifierName":"arr"}]}
5757
{"kind":"AutoDepsDecorations","fnLoc":{"start":{"line":10,"column":2,"index":274},"end":{"line":10,"column":44,"index":316},"filename":"retry-no-emit.ts"},"decorations":[{"start":{"line":10,"column":25,"index":297},"end":{"line":10,"column":29,"index":301},"filename":"retry-no-emit.ts","identifierName":"arr2"},{"start":{"line":10,"column":25,"index":297},"end":{"line":10,"column":29,"index":301},"filename":"retry-no-emit.ts","identifierName":"arr2"},{"start":{"line":10,"column":35,"index":307},"end":{"line":10,"column":42,"index":314},"filename":"retry-no-emit.ts","identifierName":"propVal"}]}
5858
{"kind":"CompileSuccess","fnLoc":{"start":{"line":5,"column":0,"index":163},"end":{"line":13,"column":1,"index":357},"filename":"retry-no-emit.ts"},"fnName":"Foo","memoSlots":0,"memoBlocks":0,"memoValues":0,"prunedMemoBlocks":0,"prunedMemoValues":0}

compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/infer-effect-dependencies/reactive-setState.expect.md

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -34,22 +34,28 @@ import { print } from "shared-runtime";
3434
* setState types are not enough to determine to omit from deps. Must also take reactivity into account.
3535
*/
3636
function ReactiveRefInEffect(props) {
37-
const $ = _c(2);
37+
const $ = _c(4);
3838
const [, setState1] = useRef("initial value");
3939
const [, setState2] = useRef("initial value");
4040
let setState;
41-
if (props.foo) {
42-
setState = setState1;
41+
if ($[0] !== props.foo) {
42+
if (props.foo) {
43+
setState = setState1;
44+
} else {
45+
setState = setState2;
46+
}
47+
$[0] = props.foo;
48+
$[1] = setState;
4349
} else {
44-
setState = setState2;
50+
setState = $[1];
4551
}
4652
let t0;
47-
if ($[0] !== setState) {
53+
if ($[2] !== setState) {
4854
t0 = () => print(setState);
49-
$[0] = setState;
50-
$[1] = t0;
55+
$[2] = setState;
56+
$[3] = t0;
5157
} else {
52-
t0 = $[1];
58+
t0 = $[3];
5359
}
5460
useEffect(t0, [setState]);
5561
}

0 commit comments

Comments
 (0)