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:
Joe Savona
2024-03-08 13:59:22 -08:00
parent 5f3ed3f724
commit 16852386a5
4 changed files with 77 additions and 40 deletions
@@ -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, {
@@ -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];
```
@@ -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]
@@ -8,7 +8,7 @@ function Component() {
while ((item = items.pop())) {
sum += item;
}
return [sum];
return [items, sum];
}
export const FIXTURE_ENTRYPOINT = {