Skip to content

Commit 262a5be

Browse files
committed
[compiler] Remove redundant InferMutableContextVariables
This removes special casing for `PropertyStore` mutability inference within FunctionExpressions.
1 parent 5addb0b commit 262a5be

File tree

5 files changed

+54
-156
lines changed

5 files changed

+54
-156
lines changed

compiler/packages/babel-plugin-react-compiler/src/Inference/AnalyseFunctions.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ import {
1818
import {deadCodeElimination} from '../Optimization';
1919
import {inferReactiveScopeVariables} from '../ReactiveScopes';
2020
import {rewriteInstructionKindsBasedOnReassignment} from '../SSA';
21-
import {inferMutableContextVariables} from './InferMutableContextVariables';
2221
import {inferMutableRanges} from './InferMutableRanges';
2322
import inferReferenceEffects from './InferReferenceEffects';
2423

@@ -78,7 +77,6 @@ function lower(func: HIRFunction): void {
7877
}
7978

8079
function infer(loweredFunc: LoweredFunction): void {
81-
const knownMutated = inferMutableContextVariables(loweredFunc.func);
8280
for (const operand of loweredFunc.func.context) {
8381
const identifier = operand.identifier;
8482
CompilerError.invariant(operand.effect === Effect.Unknown, {
@@ -95,10 +93,11 @@ function infer(loweredFunc: LoweredFunction): void {
9593
* render
9694
*/
9795
operand.effect = Effect.Capture;
98-
} else if (knownMutated.has(operand)) {
99-
operand.effect = Effect.Mutate;
10096
} else if (isMutatedOrReassigned(identifier)) {
101-
// Note that this also reflects if identifier is ConditionallyMutated
97+
/**
98+
* Reflects direct reassignments, PropertyStores, and ConditionallyMutate
99+
* (directly or through maybe-aliases)
100+
*/
102101
operand.effect = Effect.Capture;
103102
} else {
104103
operand.effect = Effect.Read;

compiler/packages/babel-plugin-react-compiler/src/Inference/InferMutableContextVariables.ts

Lines changed: 0 additions & 105 deletions
This file was deleted.

compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/allow-mutate-global-in-effect-fixpoint.expect.md

Lines changed: 32 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,14 @@ function Component() {
1919

2020
// capture into a separate variable that is not a context variable.
2121
const y = x;
22+
/**
23+
* Note that this fixture currently produces a stale effect closure if `y = x
24+
* = someGlobal` changes between renders. Under current compiler assumptions,
25+
* that would be a rule of react violation.
26+
*/
2227
useEffect(() => {
2328
y.value = 'hello';
24-
}, []);
29+
});
2530

2631
useEffect(() => {
2732
setState(someGlobal.value);
@@ -46,57 +51,50 @@ import { useEffect, useState } from "react";
4651
let someGlobal = { value: null };
4752

4853
function Component() {
49-
const $ = _c(7);
54+
const $ = _c(5);
5055
const [state, setState] = useState(someGlobal);
56+
57+
let x = someGlobal;
58+
while (x == null) {
59+
x = someGlobal;
60+
}
61+
62+
const y = x;
5163
let t0;
52-
let t1;
53-
let t2;
5464
if ($[0] === Symbol.for("react.memo_cache_sentinel")) {
55-
let x = someGlobal;
56-
while (x == null) {
57-
x = someGlobal;
58-
}
59-
60-
const y = x;
61-
t0 = useEffect;
62-
t1 = () => {
65+
t0 = () => {
6366
y.value = "hello";
6467
};
65-
t2 = [];
6668
$[0] = t0;
69+
} else {
70+
t0 = $[0];
71+
}
72+
useEffect(t0);
73+
let t1;
74+
let t2;
75+
if ($[1] === Symbol.for("react.memo_cache_sentinel")) {
76+
t1 = () => {
77+
setState(someGlobal.value);
78+
};
79+
t2 = [someGlobal];
6780
$[1] = t1;
6881
$[2] = t2;
6982
} else {
70-
t0 = $[0];
7183
t1 = $[1];
7284
t2 = $[2];
7385
}
74-
t0(t1, t2);
75-
let t3;
86+
useEffect(t1, t2);
87+
88+
const t3 = String(state);
7689
let t4;
77-
if ($[3] === Symbol.for("react.memo_cache_sentinel")) {
78-
t3 = () => {
79-
setState(someGlobal.value);
80-
};
81-
t4 = [someGlobal];
90+
if ($[3] !== t3) {
91+
t4 = <div>{t3}</div>;
8292
$[3] = t3;
8393
$[4] = t4;
8494
} else {
85-
t3 = $[3];
8695
t4 = $[4];
8796
}
88-
useEffect(t3, t4);
89-
90-
const t5 = String(state);
91-
let t6;
92-
if ($[5] !== t5) {
93-
t6 = <div>{t5}</div>;
94-
$[5] = t5;
95-
$[6] = t6;
96-
} else {
97-
t6 = $[6];
98-
}
99-
return t6;
97+
return t4;
10098
}
10199

102100
export const FIXTURE_ENTRYPOINT = {

compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/allow-mutate-global-in-effect-fixpoint.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,14 @@ function Component() {
1515

1616
// capture into a separate variable that is not a context variable.
1717
const y = x;
18+
/**
19+
* Note that this fixture currently produces a stale effect closure if `y = x
20+
* = someGlobal` changes between renders. Under current compiler assumptions,
21+
* that would be a rule of react violation.
22+
*/
1823
useEffect(() => {
1924
y.value = 'hello';
20-
}, []);
25+
});
2126

2227
useEffect(() => {
2328
setState(someGlobal.value);

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

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -36,21 +36,22 @@ import { useSharedValue } from "react-native-reanimated";
3636
* of render
3737
*/
3838
function SomeComponent() {
39-
const $ = _c(3);
39+
const $ = _c(2);
4040
const sharedVal = useSharedValue(0);
41-
42-
const T0 = Button;
43-
const t0 = () => (sharedVal.value = Math.random());
44-
let t1;
45-
if ($[0] !== T0 || $[1] !== t0) {
46-
t1 = <T0 onPress={t0} title="Randomize" />;
47-
$[0] = T0;
41+
let t0;
42+
if ($[0] !== sharedVal) {
43+
t0 = (
44+
<Button
45+
onPress={() => (sharedVal.value = Math.random())}
46+
title="Randomize"
47+
/>
48+
);
49+
$[0] = sharedVal;
4850
$[1] = t0;
49-
$[2] = t1;
5051
} else {
51-
t1 = $[2];
52+
t0 = $[1];
5253
}
53-
return t1;
54+
return t0;
5455
}
5556

5657
```

0 commit comments

Comments
 (0)