[compiler] Support for non-declatation for in/of iterators
ghstack-source-id: a28801e022561029e2f46c3dcb858bd4a81dea6a Pull Request resolved: https://github.com/facebook/react/pull/31710
Mike Vitousek committed
Dec 9, 2024 at 10:48 UTC
76d603a72aa14e93e73a68e0f96024a1841902f3
2 files changed
+35
-29
compiler/packages/babel-plugin-react-compiler/src/HIR/BuildHIR.ts
+35
-23
@@ -1078,6 +1078,12 @@ function lowerStatement(
1078
const left = stmt.get('left');
1079
const leftLoc = left.node.loc ?? GeneratedSource;
1080
let test: Place;
1081
+ const advanceIterator = lowerValueToTemporary(builder, {
1082
+ kind: 'IteratorNext',
1083
+ loc: leftLoc,
1084
+ iterator: {...iterator},
1085
+ collection: {...value},
1086
+ });
1087
if (left.isVariableDeclaration()) {
1088
const declarations = left.get('declarations');
1089
CompilerError.invariant(declarations.length === 1, {
@@ -1087,12 +1093,6 @@ function lowerStatement(
1093
suggestions: null,
1094
});
1095
const id = declarations[0].get('id');
1090
- const advanceIterator = lowerValueToTemporary(builder, {
1091
- kind: 'IteratorNext',
1092
- loc: leftLoc,
1093
- iterator: {...iterator},
1094
- collection: {...value},
1095
- });
1096
const assign = lowerAssignment(
1097
builder,
1098
leftLoc,
@@ -1103,13 +1103,19 @@ function lowerStatement(
1103
);
1104
test = lowerValueToTemporary(builder, assign);
1105
} else {
1106
- builder.errors.push({
1107
- reason: `(BuildHIR::lowerStatement) Handle ${left.type} inits in ForOfStatement`,
1108
- severity: ErrorSeverity.Todo,
1109
- loc: left.node.loc ?? null,
1110
- suggestions: null,
1106
+ CompilerError.invariant(left.isLVal(), {
1107
+ loc: leftLoc,
1108
+ reason: 'Expected ForOf init to be a variable declaration or lval',
1109
});
1112
- return;
1110
+ const assign = lowerAssignment(
1111
+ builder,
1112
+ leftLoc,
1113
+ InstructionKind.Reassign,
1114
+ left,
1115
+ advanceIterator,
1116
+ 'Assignment',
1117
+ );
1118
+ test = lowerValueToTemporary(builder, assign);
1119
}
1120
builder.terminateWithContinuation(
1121
{
@@ -1166,6 +1172,11 @@ function lowerStatement(
1172
const left = stmt.get('left');
1173
const leftLoc = left.node.loc ?? GeneratedSource;
1174
let test: Place;
1175
+ const nextPropertyTemp = lowerValueToTemporary(builder, {
1176
+ kind: 'NextPropertyOf',
1177
+ loc: leftLoc,
1178
+ value,
1179
+ });
1180
if (left.isVariableDeclaration()) {
1181
const declarations = left.get('declarations');
1182
CompilerError.invariant(declarations.length === 1, {
@@ -1175,11 +1186,6 @@ function lowerStatement(
1186
suggestions: null,
1187
});
1188
const id = declarations[0].get('id');
1178
- const nextPropertyTemp = lowerValueToTemporary(builder, {
1179
- kind: 'NextPropertyOf',
1180
- loc: leftLoc,
1181
- value,
1182
- });
1189
const assign = lowerAssignment(
1190
builder,
1191
leftLoc,
@@ -1190,13 +1196,19 @@ function lowerStatement(
1196
);
1197
test = lowerValueToTemporary(builder, assign);
1198
} else {
1193
- builder.errors.push({
1194
- reason: `(BuildHIR::lowerStatement) Handle ${left.type} inits in ForInStatement`,
1195
- severity: ErrorSeverity.Todo,
1196
- loc: left.node.loc ?? null,
1197
- suggestions: null,
1199
+ CompilerError.invariant(left.isLVal(), {
1200
+ loc: leftLoc,
1201
+ reason: 'Expected ForIn init to be a variable declaration or lval',
1202
});
1199
- return;
1203
+ const assign = lowerAssignment(
1204
+ builder,
1205
+ leftLoc,
1206
+ InstructionKind.Reassign,
1207
+ left,
1208
+ nextPropertyTemp,
1209
+ 'Assignment',
1210
+ );
1211
+ test = lowerValueToTemporary(builder, assign);
1212
}
1213
builder.terminateWithContinuation(
1214
{
compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/error.todo-kitchensink.expect.md
-6
@@ -98,12 +98,6 @@ Todo: (BuildHIR::lowerExpression) Handle tagged template with interpolations (30
98
99
Todo: (BuildHIR::lowerExpression) Handle tagged template where cooked value is different from raw value (34:34)
100
101
-Todo: (BuildHIR::lowerStatement) Handle Identifier inits in ForOfStatement (36:36)
102
-
103
-Todo: (BuildHIR::lowerStatement) Handle ArrayPattern inits in ForOfStatement (38:38)
104
-
105
-Todo: (BuildHIR::lowerStatement) Handle ObjectPattern inits in ForOfStatement (40:40)
106
-
101
Todo: (BuildHIR::node.lowerReorderableExpression) Expression type `MemberExpression` cannot be safely reordered (57:57)
102
103
Todo: (BuildHIR::node.lowerReorderableExpression) Expression type `BinaryExpression` cannot be safely reordered (53:53)