Skip to content

Commit 7a3fcc9

Browse files
committed
[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: 4594d64 Pull Request resolved: #30790
1 parent 98b5740 commit 7a3fcc9

File tree

6 files changed

+11
-20
lines changed

6 files changed

+11
-20
lines changed

compiler/packages/babel-plugin-react-compiler/src/HIR/BuildHIR.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -211,16 +211,12 @@ export function lower(
211211
null,
212212
);
213213

214-
const returnIdentifier = builder.makeTemporary(
215-
func.node.loc ?? GeneratedSource,
216-
);
217-
218214
return Ok({
219215
id,
220216
params,
221217
fnType: parent == null ? env.fnType : 'Other',
222218
returnTypeAnnotation: null, // TODO: extract the actual return type node if present
223-
returnIdentifier,
219+
returnType: makeType(),
224220
body: builder.build(),
225221
context,
226222
generator: func.node.generator === true,

compiler/packages/babel-plugin-react-compiler/src/HIR/HIR.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,7 @@ export type HIRFunction = {
286286
env: Environment;
287287
params: Array<Place | SpreadPattern>;
288288
returnTypeAnnotation: t.FlowType | t.TSType | null;
289-
returnIdentifier: Identifier;
289+
returnType: Type;
290290
context: Array<Place>;
291291
effects: Array<FunctionEffect> | null;
292292
body: HIR;

compiler/packages/babel-plugin-react-compiler/src/HIR/PrintHIR.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ export function printFunction(fn: HIRFunction): string {
7272
if (definition.length !== 0) {
7373
output.push(definition);
7474
}
75-
output.push(printType(fn.returnIdentifier.type));
75+
output.push(printType(fn.returnType));
7676
output.push(printHIR(fn.body));
7777
output.push(...fn.directives);
7878
return output.join('\n');
@@ -556,9 +556,7 @@ export function printInstructionValue(instrValue: ReactiveValue): string {
556556
}
557557
})
558558
.join(', ') ?? '';
559-
const type = printType(
560-
instrValue.loweredFunc.func.returnIdentifier.type,
561-
).trim();
559+
const type = printType(instrValue.loweredFunc.func.returnType).trim();
562560
value = `${kind} ${name} @deps[${deps}] @context[${context}] @effects[${effects}]${type !== '' ? ` return${type}` : ''}:\n${fn}`;
563561
break;
564562
}

compiler/packages/babel-plugin-react-compiler/src/Optimization/LowerContextAccess.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import {
2323
isUseContextHookType,
2424
makeBlockId,
2525
makeInstructionId,
26+
makeType,
2627
markInstructionIds,
2728
promoteTemporary,
2829
reversePostorderBlocks,
@@ -238,18 +239,14 @@ function emitSelectorFn(env: Environment, keys: Array<string>): Instruction {
238239
phis: new Set(),
239240
};
240241

241-
const returnIdentifier = createTemporaryPlace(
242-
env,
243-
GeneratedSource,
244-
).identifier;
245242
const fn: HIRFunction = {
246243
loc: GeneratedSource,
247244
id: null,
248245
fnType: 'Other',
249246
env,
250247
params: [obj],
251248
returnTypeAnnotation: null,
252-
returnIdentifier,
249+
returnType: makeType(),
253250
context: [],
254251
effects: null,
255252
body: {

compiler/packages/babel-plugin-react-compiler/src/TypeInference/InferTypes.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ function apply(func: HIRFunction, unifier: Unifier): void {
8888
}
8989
}
9090
}
91-
func.returnIdentifier.type = unifier.get(func.returnIdentifier.type);
91+
func.returnType = unifier.get(func.returnType);
9292
}
9393

9494
type TypeEquation = {
@@ -141,12 +141,12 @@ function* generate(
141141
}
142142
}
143143
if (returnTypes.length > 1) {
144-
yield equation(func.returnIdentifier.type, {
144+
yield equation(func.returnType, {
145145
kind: 'Phi',
146146
operands: returnTypes,
147147
});
148148
} else if (returnTypes.length === 1) {
149-
yield equation(func.returnIdentifier.type, returnTypes[0]!);
149+
yield equation(func.returnType, returnTypes[0]!);
150150
}
151151
}
152152

@@ -363,7 +363,7 @@ function* generateInstructionTypes(
363363
yield equation(left, {
364364
kind: 'Function',
365365
shapeId: BuiltInFunctionId,
366-
return: value.loweredFunc.func.returnIdentifier.type,
366+
return: value.loweredFunc.func.returnType,
367367
});
368368
break;
369369
}

compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/error.todo-repro-named-function-with-shadowed-local-same-name.expect.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ function Component(props) {
2222
7 | return hasErrors;
2323
8 | }
2424
> 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)
2626
10 | }
2727
11 |
2828
```

0 commit comments

Comments
 (0)