@samitouri / QOS-React / commits / 217a0efcd9

[compiler] Add returnIdentifier to function expressions

This gives us a place to store type information, used in follow-up PRs. ghstack-source-id: ee0bfa253f63c30ccaac083b9f1f72b76617f19c Pull Request resolved: https://github.com/facebook/react/pull/30784

Joe Savona committed Aug 21, 2024 at 18:21 UTC 217a0efcd90ef04556e0256e0eff9313bdbbcaca
4 files changed +7 -1
compiler/packages/babel-plugin-react-compiler/src/HIR/BuildHIR.ts
+3
@@ -211,11 +211,14 @@ export function lower(
211 null,
212 );
213
214 + const returnIdentifier = builder.makeTemporary(func.node.loc ?? GeneratedSource);
215 +
216 return Ok({
217 id,
218 params,
219 fnType: parent == null ? env.fnType : 'Other',
220 returnType: null, // TODO: extract the actual return type node if present
221 + returnIdentifier,
222 body: builder.build(),
223 context,
224 generator: func.node.generator === true,
compiler/packages/babel-plugin-react-compiler/src/HIR/HIR.ts
+1
@@ -286,6 +286,7 @@ export type HIRFunction = {
286 env: Environment;
287 params: Array<Place | SpreadPattern>;
288 returnType: t.FlowType | t.TSType | null;
289 + returnIdentifier: Identifier;
290 context: Array<Place>;
291 effects: Array<FunctionEffect> | null;
292 body: HIR;
compiler/packages/babel-plugin-react-compiler/src/Optimization/LowerContextAccess.ts
+2
@@ -238,6 +238,7 @@ function emitSelectorFn(env: Environment, keys: Array<string>): Instruction {
238 phis: new Set(),
239 };
240
241 + const returnIdentifier = createTemporaryPlace(env, GeneratedSource).identifier;
242 const fn: HIRFunction = {
243 loc: GeneratedSource,
244 id: null,
@@ -245,6 +246,7 @@ function emitSelectorFn(env: Environment, keys: Array<string>): Instruction {
246 env,
247 params: [obj],
248 returnType: null,
249 + returnIdentifier,
250 context: [],
251 effects: null,
252 body: {
compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/error.todo-repro-named-function-with-shadowed-local-same-name.expect.md
+1 -1
@@ -22,7 +22,7 @@ function Component(props) {
22 7 | return hasErrors;
23 8 | }
24 > 9 | return hasErrors();
25 - | ^^^^^^^^^ Invariant: [hoisting] Expected value for identifier to be initialized. hasErrors_0$16 (9:9)
25 + | ^^^^^^^^^ Invariant: [hoisting] Expected value for identifier to be initialized. hasErrors_0$17 (9:9)
26 10 | }
27 11 |
28 ```