Skip to content

Commit e5c9be4

Browse files
committed
[compiler] Disambiguate between void, implicit, and explicit returns
Adds a new property to ReturnTerminals to disambiguate whether it was explicit, implicit (arrow function expressions), or void (where it was omitted). I will use this property in the next PR adding a new validation pass.
1 parent eaee530 commit e5c9be4

File tree

6 files changed

+19
-1
lines changed

6 files changed

+19
-1
lines changed

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,7 @@ export function lower(
189189
const fallthrough = builder.reserve('block');
190190
const terminal: ReturnTerminal = {
191191
kind: 'return',
192+
returnVariant: 'Implicit',
192193
loc: GeneratedSource,
193194
value: lowerExpressionToTemporary(builder, body),
194195
id: makeInstructionId(0),
@@ -219,6 +220,7 @@ export function lower(
219220
builder.terminate(
220221
{
221222
kind: 'return',
223+
returnVariant: 'Void',
222224
loc: GeneratedSource,
223225
value: lowerValueToTemporary(builder, {
224226
kind: 'Primitive',
@@ -302,6 +304,7 @@ function lowerStatement(
302304
}
303305
const terminal: ReturnTerminal = {
304306
kind: 'return',
307+
returnVariant: 'Explicit',
305308
loc: stmt.node.loc ?? GeneratedSource,
306309
value,
307310
id: makeInstructionId(0),

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -446,8 +446,20 @@ export type ThrowTerminal = {
446446
};
447447
export type Case = {test: Place | null; block: BlockId};
448448

449+
export type ReturnVariant = 'Void' | 'Implicit' | 'Explicit';
449450
export type ReturnTerminal = {
450451
kind: 'return';
452+
/**
453+
* Void:
454+
* () => { ... }
455+
* function() { ... }
456+
* Implicit (ArrowFunctionExpression only):
457+
* () => foo
458+
* Explicit:
459+
* () => { return ... }
460+
* function () { return ... }
461+
*/
462+
returnVariant: ReturnVariant;
451463
loc: SourceLocation;
452464
value: Place;
453465
id: InstructionId;

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@ export function printTerminal(terminal: Terminal): Array<string> | string {
215215
break;
216216
}
217217
case 'return': {
218-
value = `[${terminal.id}] Return${
218+
value = `[${terminal.id}] Return ${terminal.returnVariant}${
219219
terminal.value != null ? ' ' + printPlace(terminal.value) : ''
220220
}`;
221221
if (terminal.effects != null) {

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -777,6 +777,7 @@ export function mapTerminalSuccessors(
777777
case 'return': {
778778
return {
779779
kind: 'return',
780+
returnVariant: terminal.returnVariant,
780781
loc: terminal.loc,
781782
value: terminal.value,
782783
id: makeInstructionId(0),

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,7 @@ function emitSelectorFn(env: Environment, keys: Array<string>): Instruction {
237237
terminal: {
238238
id: makeInstructionId(0),
239239
kind: 'return',
240+
returnVariant: 'Explicit',
240241
loc: GeneratedSource,
241242
value: arrayInstr.lvalue,
242243
effects: null,

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -352,6 +352,7 @@ function emitOutlinedFn(
352352
terminal: {
353353
id: makeInstructionId(0),
354354
kind: 'return',
355+
returnVariant: 'Explicit',
355356
loc: GeneratedSource,
356357
value: instructions.at(-1)!.lvalue,
357358
effects: null,

0 commit comments

Comments
 (0)