mirror of
https://github.com/facebook/react.git
synced 2025-11-01 09:12:30 +00:00
Support more cases of reassignment within value blocks
Fixes T175283039 — it's totally fine to have a StoreLocal as an instruction in a value block, so long as its a reassignment.
This commit is contained in:
+17
-1
@@ -1752,13 +1752,29 @@ function codegenInstructionValue(
|
||||
);
|
||||
break;
|
||||
}
|
||||
case "StoreLocal": {
|
||||
CompilerError.invariant(
|
||||
instrValue.lvalue.kind === InstructionKind.Reassign,
|
||||
{
|
||||
reason: `Unexpected StoreLocal in codegenInstructionValue`,
|
||||
description: null,
|
||||
loc: instrValue.loc,
|
||||
suggestions: null,
|
||||
}
|
||||
);
|
||||
value = t.assignmentExpression(
|
||||
"=",
|
||||
codegenLValue(cx, instrValue.lvalue.place),
|
||||
codegenPlaceToExpression(cx, instrValue.value)
|
||||
);
|
||||
break;
|
||||
}
|
||||
case "ReactiveFunctionValue":
|
||||
case "Memoize":
|
||||
case "Debugger":
|
||||
case "DeclareLocal":
|
||||
case "DeclareContext":
|
||||
case "Destructure":
|
||||
case "StoreLocal":
|
||||
case "ObjectMethod":
|
||||
case "StoreContext": {
|
||||
CompilerError.invariant(false, {
|
||||
|
||||
-38
@@ -1,38 +0,0 @@
|
||||
|
||||
## Input
|
||||
|
||||
```javascript
|
||||
import { makeArray } from "shared-runtime";
|
||||
|
||||
// @flow
|
||||
function Component() {
|
||||
const items = makeArray(0, 1, 2);
|
||||
let item;
|
||||
let sum = 0;
|
||||
while ((item = items.pop())) {
|
||||
sum += item;
|
||||
}
|
||||
return [sum];
|
||||
}
|
||||
|
||||
export const FIXTURE_ENTRYPOINT = {
|
||||
fn: Component,
|
||||
params: [{}],
|
||||
};
|
||||
|
||||
```
|
||||
|
||||
|
||||
## Error
|
||||
|
||||
```
|
||||
6 | let item;
|
||||
7 | let sum = 0;
|
||||
> 8 | while ((item = items.pop())) {
|
||||
| ^^^^ [ReactForget] Invariant: Unexpected StoreLocal in codegenInstructionValue (8:8)
|
||||
9 | sum += item;
|
||||
10 | }
|
||||
11 | return [sum];
|
||||
```
|
||||
|
||||
|
||||
+59
@@ -0,0 +1,59 @@
|
||||
|
||||
## Input
|
||||
|
||||
```javascript
|
||||
import { makeArray } from "shared-runtime";
|
||||
|
||||
// @flow
|
||||
function Component() {
|
||||
const items = makeArray(0, 1, 2);
|
||||
let item;
|
||||
let sum = 0;
|
||||
while ((item = items.pop())) {
|
||||
sum += item;
|
||||
}
|
||||
return [items, sum];
|
||||
}
|
||||
|
||||
export const FIXTURE_ENTRYPOINT = {
|
||||
fn: Component,
|
||||
params: [{}],
|
||||
};
|
||||
|
||||
```
|
||||
|
||||
## Code
|
||||
|
||||
```javascript
|
||||
import { unstable_useMemoCache as useMemoCache } from "react";
|
||||
import { makeArray } from "shared-runtime";
|
||||
|
||||
// @flow
|
||||
function Component() {
|
||||
const $ = useMemoCache(1);
|
||||
let t0;
|
||||
if ($[0] === Symbol.for("react.memo_cache_sentinel")) {
|
||||
const items = makeArray(0, 1, 2);
|
||||
let item;
|
||||
let sum = 0;
|
||||
while ((item = items.pop())) {
|
||||
sum = sum + item;
|
||||
}
|
||||
|
||||
t0 = [items, sum];
|
||||
$[0] = t0;
|
||||
} else {
|
||||
t0 = $[0];
|
||||
}
|
||||
return t0;
|
||||
}
|
||||
|
||||
export const FIXTURE_ENTRYPOINT = {
|
||||
fn: Component,
|
||||
params: [{}],
|
||||
};
|
||||
|
||||
```
|
||||
|
||||
### Eval output
|
||||
(kind: ok) [[],3]
|
||||
+1
-1
@@ -8,7 +8,7 @@ function Component() {
|
||||
while ((item = items.pop())) {
|
||||
sum += item;
|
||||
}
|
||||
return [sum];
|
||||
return [items, sum];
|
||||
}
|
||||
|
||||
export const FIXTURE_ENTRYPOINT = {
|
||||
Reference in New Issue
Block a user