From 77fe774aef135007315802d1d91d6f28a937007b Mon Sep 17 00:00:00 2001 From: Joe Savona Date: Wed, 21 Aug 2024 18:21:24 -0700 Subject: [PATCH] [compiler] Add returnIdentifier to function expressions This gives us a place to store type information, used in follow-up PRs. [ghstack-poisoned] --- .../packages/babel-plugin-react-compiler/src/HIR/BuildHIR.ts | 3 +++ compiler/packages/babel-plugin-react-compiler/src/HIR/HIR.ts | 1 + .../src/Optimization/LowerContextAccess.ts | 2 ++ ...epro-named-function-with-shadowed-local-same-name.expect.md | 2 +- 4 files changed, 7 insertions(+), 1 deletion(-) diff --git a/compiler/packages/babel-plugin-react-compiler/src/HIR/BuildHIR.ts b/compiler/packages/babel-plugin-react-compiler/src/HIR/BuildHIR.ts index 20fac9d610a0..4fec81c91146 100644 --- a/compiler/packages/babel-plugin-react-compiler/src/HIR/BuildHIR.ts +++ b/compiler/packages/babel-plugin-react-compiler/src/HIR/BuildHIR.ts @@ -211,11 +211,14 @@ export function lower( null, ); + const returnIdentifier = builder.makeTemporary(func.node.loc ?? GeneratedSource); + return Ok({ id, params, fnType: parent == null ? env.fnType : 'Other', returnType: null, // TODO: extract the actual return type node if present + returnIdentifier, body: builder.build(), context, generator: func.node.generator === true, diff --git a/compiler/packages/babel-plugin-react-compiler/src/HIR/HIR.ts b/compiler/packages/babel-plugin-react-compiler/src/HIR/HIR.ts index e56c002c513b..8c8682aa8cc2 100644 --- a/compiler/packages/babel-plugin-react-compiler/src/HIR/HIR.ts +++ b/compiler/packages/babel-plugin-react-compiler/src/HIR/HIR.ts @@ -286,6 +286,7 @@ export type HIRFunction = { env: Environment; params: Array; returnType: t.FlowType | t.TSType | null; + returnIdentifier: Identifier; context: Array; effects: Array | null; body: HIR; diff --git a/compiler/packages/babel-plugin-react-compiler/src/Optimization/LowerContextAccess.ts b/compiler/packages/babel-plugin-react-compiler/src/Optimization/LowerContextAccess.ts index 8455a094f05e..5a21104d1d20 100644 --- a/compiler/packages/babel-plugin-react-compiler/src/Optimization/LowerContextAccess.ts +++ b/compiler/packages/babel-plugin-react-compiler/src/Optimization/LowerContextAccess.ts @@ -238,6 +238,7 @@ function emitSelectorFn(env: Environment, keys: Array): Instruction { phis: new Set(), }; + const returnIdentifier = createTemporaryPlace(env, GeneratedSource).identifier; const fn: HIRFunction = { loc: GeneratedSource, id: null, @@ -245,6 +246,7 @@ function emitSelectorFn(env: Environment, keys: Array): Instruction { env, params: [obj], returnType: null, + returnIdentifier, context: [], effects: null, body: { diff --git a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/error.todo-repro-named-function-with-shadowed-local-same-name.expect.md b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/error.todo-repro-named-function-with-shadowed-local-same-name.expect.md index db3a192eaf60..51b0bfd50697 100644 --- a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/error.todo-repro-named-function-with-shadowed-local-same-name.expect.md +++ b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/error.todo-repro-named-function-with-shadowed-local-same-name.expect.md @@ -22,7 +22,7 @@ function Component(props) { 7 | return hasErrors; 8 | } > 9 | return hasErrors(); - | ^^^^^^^^^ Invariant: [hoisting] Expected value for identifier to be initialized. hasErrors_0$16 (9:9) + | ^^^^^^^^^ Invariant: [hoisting] Expected value for identifier to be initialized. hasErrors_0$17 (9:9) 10 | } 11 | ```