From 7a3fcc9898d57a723613814bd19ec1d60805e5c8 Mon Sep 17 00:00:00 2001 From: Joe Savona Date: Thu, 22 Aug 2024 09:07:01 -0700 Subject: [PATCH] [compiler] Flatten returnIdentifier to just returnType We don't a full Identifier object for the return type, we can just store the type. ghstack-source-id: 4594d64ce3900ced3e461945697926489898318e Pull Request resolved: https://github.com/facebook/react/pull/30790 --- .../babel-plugin-react-compiler/src/HIR/BuildHIR.ts | 6 +----- .../packages/babel-plugin-react-compiler/src/HIR/HIR.ts | 2 +- .../babel-plugin-react-compiler/src/HIR/PrintHIR.ts | 6 ++---- .../src/Optimization/LowerContextAccess.ts | 7 ++----- .../src/TypeInference/InferTypes.ts | 8 ++++---- ...named-function-with-shadowed-local-same-name.expect.md | 2 +- 6 files changed, 11 insertions(+), 20 deletions(-) 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 1b0d9f455900d..06fcfbea7ecc0 100644 --- a/compiler/packages/babel-plugin-react-compiler/src/HIR/BuildHIR.ts +++ b/compiler/packages/babel-plugin-react-compiler/src/HIR/BuildHIR.ts @@ -211,16 +211,12 @@ export function lower( null, ); - const returnIdentifier = builder.makeTemporary( - func.node.loc ?? GeneratedSource, - ); - return Ok({ id, params, fnType: parent == null ? env.fnType : 'Other', returnTypeAnnotation: null, // TODO: extract the actual return type node if present - returnIdentifier, + returnType: makeType(), 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 1b866bb4c38a7..ea121c6fcd727 100644 --- a/compiler/packages/babel-plugin-react-compiler/src/HIR/HIR.ts +++ b/compiler/packages/babel-plugin-react-compiler/src/HIR/HIR.ts @@ -286,7 +286,7 @@ export type HIRFunction = { env: Environment; params: Array; returnTypeAnnotation: t.FlowType | t.TSType | null; - returnIdentifier: Identifier; + returnType: Type; context: Array; effects: Array | null; body: HIR; diff --git a/compiler/packages/babel-plugin-react-compiler/src/HIR/PrintHIR.ts b/compiler/packages/babel-plugin-react-compiler/src/HIR/PrintHIR.ts index 19a3a718285f4..a5ad303d7a587 100644 --- a/compiler/packages/babel-plugin-react-compiler/src/HIR/PrintHIR.ts +++ b/compiler/packages/babel-plugin-react-compiler/src/HIR/PrintHIR.ts @@ -72,7 +72,7 @@ export function printFunction(fn: HIRFunction): string { if (definition.length !== 0) { output.push(definition); } - output.push(printType(fn.returnIdentifier.type)); + output.push(printType(fn.returnType)); output.push(printHIR(fn.body)); output.push(...fn.directives); return output.join('\n'); @@ -556,9 +556,7 @@ export function printInstructionValue(instrValue: ReactiveValue): string { } }) .join(', ') ?? ''; - const type = printType( - instrValue.loweredFunc.func.returnIdentifier.type, - ).trim(); + const type = printType(instrValue.loweredFunc.func.returnType).trim(); value = `${kind} ${name} @deps[${deps}] @context[${context}] @effects[${effects}]${type !== '' ? ` return${type}` : ''}:\n${fn}`; break; } 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 7ae39d695ad53..e27b8f952148a 100644 --- a/compiler/packages/babel-plugin-react-compiler/src/Optimization/LowerContextAccess.ts +++ b/compiler/packages/babel-plugin-react-compiler/src/Optimization/LowerContextAccess.ts @@ -23,6 +23,7 @@ import { isUseContextHookType, makeBlockId, makeInstructionId, + makeType, markInstructionIds, promoteTemporary, reversePostorderBlocks, @@ -238,10 +239,6 @@ function emitSelectorFn(env: Environment, keys: Array): Instruction { phis: new Set(), }; - const returnIdentifier = createTemporaryPlace( - env, - GeneratedSource, - ).identifier; const fn: HIRFunction = { loc: GeneratedSource, id: null, @@ -249,7 +246,7 @@ function emitSelectorFn(env: Environment, keys: Array): Instruction { env, params: [obj], returnTypeAnnotation: null, - returnIdentifier, + returnType: makeType(), context: [], effects: null, body: { diff --git a/compiler/packages/babel-plugin-react-compiler/src/TypeInference/InferTypes.ts b/compiler/packages/babel-plugin-react-compiler/src/TypeInference/InferTypes.ts index a8859f129bcfc..d0d23f0823df8 100644 --- a/compiler/packages/babel-plugin-react-compiler/src/TypeInference/InferTypes.ts +++ b/compiler/packages/babel-plugin-react-compiler/src/TypeInference/InferTypes.ts @@ -88,7 +88,7 @@ function apply(func: HIRFunction, unifier: Unifier): void { } } } - func.returnIdentifier.type = unifier.get(func.returnIdentifier.type); + func.returnType = unifier.get(func.returnType); } type TypeEquation = { @@ -141,12 +141,12 @@ function* generate( } } if (returnTypes.length > 1) { - yield equation(func.returnIdentifier.type, { + yield equation(func.returnType, { kind: 'Phi', operands: returnTypes, }); } else if (returnTypes.length === 1) { - yield equation(func.returnIdentifier.type, returnTypes[0]!); + yield equation(func.returnType, returnTypes[0]!); } } @@ -363,7 +363,7 @@ function* generateInstructionTypes( yield equation(left, { kind: 'Function', shapeId: BuiltInFunctionId, - return: value.loweredFunc.func.returnIdentifier.type, + return: value.loweredFunc.func.returnType, }); break; } 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 51b0bfd506973..db3a192eaf604 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$17 (9:9) + | ^^^^^^^^^ Invariant: [hoisting] Expected value for identifier to be initialized. hasErrors_0$16 (9:9) 10 | } 11 | ```