[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
Joe Savona committed
Aug 22, 2024 at 09:07 UTC
7a3fcc9898d57a723613814bd19ec1d60805e5c8
6 files changed
+11
-20
compiler/packages/babel-plugin-react-compiler/src/HIR/BuildHIR.ts
+1
-5
@@ -211,16 +211,12 @@ export function lower(
211
null,
212
);
213
214
- const returnIdentifier = builder.makeTemporary(
215
- func.node.loc ?? GeneratedSource,
216
- );
217
-
214
return Ok({
215
id,
216
params,
217
fnType: parent == null ? env.fnType : 'Other',
218
returnTypeAnnotation: null, // TODO: extract the actual return type node if present
223
- returnIdentifier,
219
+ returnType: makeType(),
220
body: builder.build(),
221
context,
222
generator: func.node.generator === true,
compiler/packages/babel-plugin-react-compiler/src/HIR/HIR.ts
+1
-1
@@ -286,7 +286,7 @@ export type HIRFunction = {
286
env: Environment;
287
params: Array<Place | SpreadPattern>;
288
returnTypeAnnotation: t.FlowType | t.TSType | null;
289
- returnIdentifier: Identifier;
289
+ returnType: Type;
290
context: Array<Place>;
291
effects: Array<FunctionEffect> | null;
292
body: HIR;
compiler/packages/babel-plugin-react-compiler/src/HIR/PrintHIR.ts
+2
-4
@@ -72,7 +72,7 @@ export function printFunction(fn: HIRFunction): string {
72
if (definition.length !== 0) {
73
output.push(definition);
74
}
75
- output.push(printType(fn.returnIdentifier.type));
75
+ output.push(printType(fn.returnType));
76
output.push(printHIR(fn.body));
77
output.push(...fn.directives);
78
return output.join('\n');
@@ -556,9 +556,7 @@ export function printInstructionValue(instrValue: ReactiveValue): string {
556
}
557
})
558
.join(', ') ?? '';
559
- const type = printType(
560
- instrValue.loweredFunc.func.returnIdentifier.type,
561
- ).trim();
559
+ const type = printType(instrValue.loweredFunc.func.returnType).trim();
560
value = `${kind} ${name} @deps[${deps}] @context[${context}] @effects[${effects}]${type !== '' ? ` return${type}` : ''}:\n${fn}`;
561
break;
562
}
compiler/packages/babel-plugin-react-compiler/src/Optimization/LowerContextAccess.ts
+2
-5
@@ -23,6 +23,7 @@ import {
23
isUseContextHookType,
24
makeBlockId,
25
makeInstructionId,
26
+ makeType,
27
markInstructionIds,
28
promoteTemporary,
29
reversePostorderBlocks,
@@ -238,10 +239,6 @@ function emitSelectorFn(env: Environment, keys: Array<string>): Instruction {
239
phis: new Set(),
240
};
241
241
- const returnIdentifier = createTemporaryPlace(
242
- env,
243
- GeneratedSource,
244
- ).identifier;
242
const fn: HIRFunction = {
243
loc: GeneratedSource,
244
id: null,
@@ -249,7 +246,7 @@ function emitSelectorFn(env: Environment, keys: Array<string>): Instruction {
246
env,
247
params: [obj],
248
returnTypeAnnotation: null,
252
- returnIdentifier,
249
+ returnType: makeType(),
250
context: [],
251
effects: null,
252
body: {
compiler/packages/babel-plugin-react-compiler/src/TypeInference/InferTypes.ts
+4
-4
@@ -88,7 +88,7 @@ function apply(func: HIRFunction, unifier: Unifier): void {
88
}
89
}
90
}
91
- func.returnIdentifier.type = unifier.get(func.returnIdentifier.type);
91
+ func.returnType = unifier.get(func.returnType);
92
}
93
94
type TypeEquation = {
@@ -141,12 +141,12 @@ function* generate(
141
}
142
}
143
if (returnTypes.length > 1) {
144
- yield equation(func.returnIdentifier.type, {
144
+ yield equation(func.returnType, {
145
kind: 'Phi',
146
operands: returnTypes,
147
});
148
} else if (returnTypes.length === 1) {
149
- yield equation(func.returnIdentifier.type, returnTypes[0]!);
149
+ yield equation(func.returnType, returnTypes[0]!);
150
}
151
}
152
@@ -363,7 +363,7 @@ function* generateInstructionTypes(
363
yield equation(left, {
364
kind: 'Function',
365
shapeId: BuiltInFunctionId,
366
- return: value.loweredFunc.func.returnIdentifier.type,
366
+ return: value.loweredFunc.func.returnType,
367
});
368
break;
369
}
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$17 (9:9)
25
+ | ^^^^^^^^^ Invariant: [hoisting] Expected value for identifier to be initialized. hasErrors_0$16 (9:9)
26
10 | }
27
11 |
28
```