Skip to content

Commit 467b4f6

Browse files
committed
[compiler] Receiver is mutate? for functions wo signatures
ghstack-source-id: 71bc929 Pull Request resolved: facebook/react#33380
1 parent c015802 commit 467b4f6

File tree

3 files changed

+80
-2
lines changed

3 files changed

+80
-2
lines changed

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

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -519,6 +519,9 @@ function applyEffect(
519519
)
520520
: null;
521521
if (signatureEffects != null) {
522+
if (DEBUG) {
523+
console.log('apply aliasing signature effects');
524+
}
522525
for (const signatureEffect of signatureEffects) {
523526
applyEffect(
524527
state,
@@ -530,6 +533,9 @@ function applyEffect(
530533
);
531534
}
532535
} else if (effect.signature != null) {
536+
if (DEBUG) {
537+
console.log('apply legacy signature effects');
538+
}
533539
const legacyEffects = computeEffectsForLegacySignature(
534540
state,
535541
effect.signature,
@@ -548,6 +554,9 @@ function applyEffect(
548554
);
549555
}
550556
} else {
557+
if (DEBUG) {
558+
console.log('default effects');
559+
}
551560
applyEffect(
552561
state,
553562
{
@@ -567,7 +576,7 @@ function applyEffect(
567576
* - All operands are captured into (but not directly aliased as)
568577
* every other argument.
569578
*/
570-
for (const arg of [effect.function, ...effect.args]) {
579+
for (const arg of [effect.receiver, effect.function, ...effect.args]) {
571580
const operand = arg.kind === 'Identifier' ? arg : arg.place;
572581
if (operand !== effect.function || effect.mutatesFunction) {
573582
applyEffect(
@@ -599,7 +608,11 @@ function applyEffect(
599608
aliased,
600609
effects,
601610
);
602-
for (const otherArg of [effect.function, ...effect.args]) {
611+
for (const otherArg of [
612+
effect.receiver,
613+
effect.function,
614+
...effect.args,
615+
]) {
603616
const other =
604617
otherArg.kind === 'Identifier' ? otherArg : otherArg.place;
605618
if (other === arg) {
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
2+
## Input
3+
4+
```javascript
5+
// @enableNewMutationAliasingModel
6+
function useHook({el1, el2}) {
7+
const s = new Set();
8+
const arr = makeArray(el1);
9+
s.add(arr);
10+
// Mutate after store
11+
arr.push(el2);
12+
13+
s.add(makeArray(el2));
14+
return s.size;
15+
}
16+
17+
```
18+
19+
## Code
20+
21+
```javascript
22+
import { c as _c } from "react/compiler-runtime"; // @enableNewMutationAliasingModel
23+
function useHook(t0) {
24+
const $ = _c(5);
25+
const { el1, el2 } = t0;
26+
let s;
27+
if ($[0] !== el1 || $[1] !== el2) {
28+
s = new Set();
29+
const arr = makeArray(el1);
30+
s.add(arr);
31+
32+
arr.push(el2);
33+
let t1;
34+
if ($[3] !== el2) {
35+
t1 = makeArray(el2);
36+
$[3] = el2;
37+
$[4] = t1;
38+
} else {
39+
t1 = $[4];
40+
}
41+
s.add(t1);
42+
$[0] = el1;
43+
$[1] = el2;
44+
$[2] = s;
45+
} else {
46+
s = $[2];
47+
}
48+
return s.size;
49+
}
50+
51+
```
52+
53+
### Eval output
54+
(kind: exception) Fixture not implemented
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// @enableNewMutationAliasingModel
2+
function useHook({el1, el2}) {
3+
const s = new Set();
4+
const arr = makeArray(el1);
5+
s.add(arr);
6+
// Mutate after store
7+
arr.push(el2);
8+
9+
s.add(makeArray(el2));
10+
return s.size;
11+
}

0 commit comments

Comments
 (0)