mirror of
https://github.com/facebook/react.git
synced 2025-11-01 09:12:30 +00:00
Fix assignment expression with context variables
Fixes the one case discovered in the previous PR; for AssignmentExpression we correctly lowered the store instruction to a local/context, but then always used a `LoadLocal` to read the result back. The load instruction appears like it might be dangling - i think what was happening is that DCE cleaned up the unused LoadLocal whereas it leaves the LoadContext alone. But this works for now, we can always clean up the extra instruction later since this case isn't too common.
This commit is contained in:
@@ -1869,6 +1869,7 @@ function lowerExpression(
|
||||
type: null,
|
||||
loc: exprLoc,
|
||||
});
|
||||
return { kind: "LoadLocal", place: identifier, loc: exprLoc };
|
||||
} else {
|
||||
lowerValueToTemporary(builder, {
|
||||
kind: "StoreContext",
|
||||
@@ -1879,8 +1880,8 @@ function lowerExpression(
|
||||
value: { ...binaryPlace },
|
||||
loc: exprLoc,
|
||||
});
|
||||
return { kind: "LoadContext", place: identifier, loc: exprLoc };
|
||||
}
|
||||
return { kind: "LoadLocal", place: identifier, loc: exprLoc };
|
||||
}
|
||||
case "MemberExpression": {
|
||||
// a.b.c += <right>
|
||||
|
||||
-25
@@ -1,25 +0,0 @@
|
||||
|
||||
## Input
|
||||
|
||||
```javascript
|
||||
function HomeDiscoStoreItemTileRating(props) {
|
||||
const item = useFragment();
|
||||
let count = 0;
|
||||
const aggregates = item?.aggregates || [];
|
||||
aggregates.forEach((aggregate) => {
|
||||
count += aggregate.count || 0;
|
||||
});
|
||||
|
||||
return <Text>{count}</Text>;
|
||||
}
|
||||
|
||||
```
|
||||
|
||||
|
||||
## Error
|
||||
|
||||
```
|
||||
[ReactForget] Invariant: Expected all references to a variable to be consistently local or context references. Identifier <unknown> count$6 is referenced as a local variable, but was previously referenced as a context variable (6:6)
|
||||
```
|
||||
|
||||
|
||||
+52
@@ -0,0 +1,52 @@
|
||||
|
||||
## Input
|
||||
|
||||
```javascript
|
||||
function HomeDiscoStoreItemTileRating(props) {
|
||||
const item = useFragment();
|
||||
let count = 0;
|
||||
const aggregates = item?.aggregates || [];
|
||||
aggregates.forEach((aggregate) => {
|
||||
count += aggregate.count || 0;
|
||||
});
|
||||
|
||||
return <Text>{count}</Text>;
|
||||
}
|
||||
|
||||
```
|
||||
|
||||
## Code
|
||||
|
||||
```javascript
|
||||
import { unstable_useMemoCache as useMemoCache } from "react";
|
||||
function HomeDiscoStoreItemTileRating(props) {
|
||||
const $ = useMemoCache(4);
|
||||
const item = useFragment();
|
||||
let count;
|
||||
if ($[0] !== item) {
|
||||
count = 0;
|
||||
const aggregates = item?.aggregates || [];
|
||||
aggregates.forEach((aggregate) => {
|
||||
count = count + (aggregate.count || 0);
|
||||
count;
|
||||
});
|
||||
$[0] = item;
|
||||
$[1] = count;
|
||||
} else {
|
||||
count = $[1];
|
||||
}
|
||||
|
||||
const t0 = count;
|
||||
let t1;
|
||||
if ($[2] !== t0) {
|
||||
t1 = <Text>{t0}</Text>;
|
||||
$[2] = t0;
|
||||
$[3] = t1;
|
||||
} else {
|
||||
t1 = $[3];
|
||||
}
|
||||
return t1;
|
||||
}
|
||||
|
||||
```
|
||||
|
||||
Reference in New Issue
Block a user