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.
Joe Savona committed
Mar 8, 2024 at 13:59 UTC
16852386a5ac9ccffa265ad96f2d7e1db544aecb
4 files changed
+77
-40
compiler/packages/babel-plugin-react-forget/src/ReactiveScopes/CodegenReactiveFunction.ts
+17
-1
@@ -1752,13 +1752,29 @@ function codegenInstructionValue(
1752
);
1753
break;
1754
}
1755
+ case "StoreLocal": {
1756
+ CompilerError.invariant(
1757
+ instrValue.lvalue.kind === InstructionKind.Reassign,
1758
+ {
1759
+ reason: `Unexpected StoreLocal in codegenInstructionValue`,
1760
+ description: null,
1761
+ loc: instrValue.loc,
1762
+ suggestions: null,
1763
+ }
1764
+ );
1765
+ value = t.assignmentExpression(
1766
+ "=",
1767
+ codegenLValue(cx, instrValue.lvalue.place),
1768
+ codegenPlaceToExpression(cx, instrValue.value)
1769
+ );
1770
+ break;
1771
+ }
1772
case "ReactiveFunctionValue":
1773
case "Memoize":
1774
case "Debugger":
1775
case "DeclareLocal":
1776
case "DeclareContext":
1777
case "Destructure":
1761
- case "StoreLocal":
1778
case "ObjectMethod":
1779
case "StoreContext": {
1780
CompilerError.invariant(false, {
compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/error.todo-reassign-in-while-loop-condition.expect.md
deleted
-38
@@ -1,38 +0,0 @@
1
-
2
-## Input
3
-
4
-```javascript
5
-import { makeArray } from "shared-runtime";
6
-
7
-// @flow
8
-function Component() {
9
- const items = makeArray(0, 1, 2);
10
- let item;
11
- let sum = 0;
12
- while ((item = items.pop())) {
13
- sum += item;
14
- }
15
- return [sum];
16
-}
17
-
18
-export const FIXTURE_ENTRYPOINT = {
19
- fn: Component,
20
- params: [{}],
21
-};
22
-
23
-```
24
-
25
-
26
-## Error
27
-
28
-```
29
- 6 | let item;
30
- 7 | let sum = 0;
31
-> 8 | while ((item = items.pop())) {
32
- | ^^^^ [ReactForget] Invariant: Unexpected StoreLocal in codegenInstructionValue (8:8)
33
- 9 | sum += item;
34
- 10 | }
35
- 11 | return [sum];
36
-```
37
-
38
-
\ No newline at end of file
compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/reassign-in-while-loop-condition.expect.md
new
+59
@@ -0,0 +1,59 @@
1
+
2
+## Input
3
+
4
+```javascript
5
+import { makeArray } from "shared-runtime";
6
+
7
+// @flow
8
+function Component() {
9
+ const items = makeArray(0, 1, 2);
10
+ let item;
11
+ let sum = 0;
12
+ while ((item = items.pop())) {
13
+ sum += item;
14
+ }
15
+ return [items, sum];
16
+}
17
+
18
+export const FIXTURE_ENTRYPOINT = {
19
+ fn: Component,
20
+ params: [{}],
21
+};
22
+
23
+```
24
+
25
+## Code
26
+
27
+```javascript
28
+import { unstable_useMemoCache as useMemoCache } from "react";
29
+import { makeArray } from "shared-runtime";
30
+
31
+// @flow
32
+function Component() {
33
+ const $ = useMemoCache(1);
34
+ let t0;
35
+ if ($[0] === Symbol.for("react.memo_cache_sentinel")) {
36
+ const items = makeArray(0, 1, 2);
37
+ let item;
38
+ let sum = 0;
39
+ while ((item = items.pop())) {
40
+ sum = sum + item;
41
+ }
42
+
43
+ t0 = [items, sum];
44
+ $[0] = t0;
45
+ } else {
46
+ t0 = $[0];
47
+ }
48
+ return t0;
49
+}
50
+
51
+export const FIXTURE_ENTRYPOINT = {
52
+ fn: Component,
53
+ params: [{}],
54
+};
55
+
56
+```
57
+
58
+### Eval output
59
+(kind: ok) [[],3]
\ No newline at end of file
compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/reassign-in-while-loop-condition.js
renamed
+1
-1
@@ -8,7 +8,7 @@ function Component() {
8
while ((item = items.pop())) {
9
sum += item;
10
}
11
- return [sum];
11
+ return [items, sum];
12
}
13
14
export const FIXTURE_ENTRYPOINT = {