[compiler][be] Promote destructured params to temporaries
Addresses a follow-up from the previous PR. Destructured function params are currently not eagerly promoted to temporaries: we wait until PromotedUsedTemporaries. But params _always_ have to be named, so we can promote when constructing HIR. ghstack-source-id: a6f665762ebcb7b06b118fcaf7515b8021645eae Pull Request resolved: https://github.com/facebook/react/pull/30332
Joe Savona committed
Jul 16, 2024 at 10:51 UTC
b810442c0e85183fe0be5fb1719ed4f1c6d8e9ab
4 files changed
+84
-11
compiler/packages/babel-plugin-react-compiler/src/HIR/BuildHIR.ts
+1
@@ -129,6 +129,7 @@ export function lower(
129
reactive: false,
130
loc: param.node.loc ?? GeneratedSource,
131
};
132
+ promoteTemporary(place.identifier);
133
params.push(place);
134
lowerAssignment(
135
builder,
compiler/packages/babel-plugin-react-compiler/src/ReactiveScopes/CodegenReactiveFunction.ts
-11
@@ -41,7 +41,6 @@ import {
41
ValidIdentifierName,
42
getHookKind,
43
makeIdentifierName,
44
- promoteTemporary,
44
} from "../HIR/HIR";
45
import { printIdentifier, printPlace } from "../HIR/PrintHIR";
46
import { eachPatternOperand } from "../HIR/visitors";
@@ -278,16 +277,6 @@ export function codegenFunction(
277
pruneUnusedLValues(reactiveFunction);
278
pruneHoistedContexts(reactiveFunction);
279
281
- /*
282
- * TODO: temporary function params (due to destructuring) should always be
283
- * promoted so that they can be renamed
284
- */
285
- for (const param of reactiveFunction.params) {
286
- const place = param.kind === "Identifier" ? param : param.place;
287
- if (place.identifier.name === null) {
288
- promoteTemporary(place.identifier);
289
- }
290
- }
280
const identifiers = renameVariables(reactiveFunction);
281
logReactiveFunction("Outline", reactiveFunction);
282
const codegen = codegenReactiveFunction(
compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/outlined-destructured-params.expect.md
new
+65
@@ -0,0 +1,65 @@
1
+
2
+## Input
3
+
4
+```javascript
5
+import { Stringify } from "shared-runtime";
6
+
7
+function Component(props) {
8
+ // test outlined functions with destructured parameters - the
9
+ // temporary for the destructured param must be promoted
10
+ return (
11
+ <>
12
+ {props.items.map(({ id, name }) => (
13
+ <Stringify key={id} name={name} />
14
+ ))}
15
+ </>
16
+ );
17
+}
18
+
19
+export const FIXTURE_ENTRYPOINT = {
20
+ fn: Component,
21
+ params: [{ items: [{ id: 1, name: "one" }] }],
22
+};
23
+
24
+```
25
+
26
+## Code
27
+
28
+```javascript
29
+import { c as _c } from "react/compiler-runtime";
30
+import { Stringify } from "shared-runtime";
31
+
32
+function Component(props) {
33
+ const $ = _c(4);
34
+ let t0;
35
+ if ($[0] !== props.items) {
36
+ t0 = props.items.map(_temp);
37
+ $[0] = props.items;
38
+ $[1] = t0;
39
+ } else {
40
+ t0 = $[1];
41
+ }
42
+ let t1;
43
+ if ($[2] !== t0) {
44
+ t1 = <>{t0}</>;
45
+ $[2] = t0;
46
+ $[3] = t1;
47
+ } else {
48
+ t1 = $[3];
49
+ }
50
+ return t1;
51
+}
52
+function _temp(t0) {
53
+ const { id, name } = t0;
54
+ return <Stringify key={id} name={name} />;
55
+}
56
+
57
+export const FIXTURE_ENTRYPOINT = {
58
+ fn: Component,
59
+ params: [{ items: [{ id: 1, name: "one" }] }],
60
+};
61
+
62
+```
63
+
64
+### Eval output
65
+(kind: ok) <div>{"name":"one"}</div>
\ No newline at end of file
compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/outlined-destructured-params.js
new
+18
@@ -0,0 +1,18 @@
1
+import { Stringify } from "shared-runtime";
2
+
3
+function Component(props) {
4
+ // test outlined functions with destructured parameters - the
5
+ // temporary for the destructured param must be promoted
6
+ return (
7
+ <>
8
+ {props.items.map(({ id, name }) => (
9
+ <Stringify key={id} name={name} />
10
+ ))}
11
+ </>
12
+ );
13
+}
14
+
15
+export const FIXTURE_ENTRYPOINT = {
16
+ fn: Component,
17
+ params: [{ items: [{ id: 1, name: "one" }] }],
18
+};