@samitouri / QOS-React-2 / commits / 830127f680

[js] Remove InstrValue::ExpressionStatement

I added this as a quick workaround, since we didn't support unused logical/conditional expression statements. Now that we handle them we don't need InstructionValue::ExpressionStatement anymore. I found this when porting our lowering to Rust.

Joe Savona committed Jul 11, 2023 at 11:52 UTC 830127f680ec5955c4219e23b404343d33681413
13 files changed +8 -93
compiler/forget/packages/babel-plugin-react-forget/src/HIR/BuildHIR.ts
+1 -18
@@ -710,24 +710,7 @@ function lowerStatement(
710 case "ExpressionStatement": {
711 const stmt = stmtPath as NodePath<t.ExpressionStatement>;
712 const expression = stmt.get("expression");
713 - const value = lowerExpressionToTemporary(builder, expression);
714 - const exprNode = expression.node;
715 - if (
716 - exprNode.type === "LogicalExpression" ||
717 - exprNode.type === "ConditionalExpression"
718 - ) {
719 - const loc = exprNode.loc ?? GeneratedSource;
720 - builder.push({
721 - id: makeInstructionId(0),
722 - lvalue: buildTemporaryPlace(builder, loc),
723 - value: {
724 - kind: "ExpressionStatement",
725 - value,
726 - loc,
727 - },
728 - loc,
729 - });
730 - }
713 + lowerExpressionToTemporary(builder, expression);
714 return;
715 }
716 case "DoWhileStatement": {
compiler/forget/packages/babel-plugin-react-forget/src/HIR/HIR.ts
-8
@@ -730,14 +730,6 @@ export type InstructionValue =
730 value: Place; // the collection
731 loc: SourceLocation;
732 }
733 - // Represents a value that is unused but which we want to ensure is not dropped
734 - // Examples include logical or ternary expressions that appear as expression statements,
735 - // that appear only for their side effects
736 - | {
737 - kind: "ExpressionStatement";
738 - value: Place;
739 - loc: SourceLocation;
740 - }
733 // `debugger` statement
734 | { kind: "Debugger"; loc: SourceLocation }
735 /**
compiler/forget/packages/babel-plugin-react-forget/src/HIR/PrintHIR.ts
-4
@@ -503,10 +503,6 @@ export function printInstructionValue(instrValue: ReactiveValue): string {
503 value = `NextIterableOf ${printPlace(instrValue.value)}`;
504 break;
505 }
506 - case "ExpressionStatement": {
507 - value = `ExpressionStatement ${printPlace(instrValue.value)}`;
508 - break;
509 - }
506 case "Debugger": {
507 value = `Debugger`;
508 break;
compiler/forget/packages/babel-plugin-react-forget/src/HIR/visitors.ts
-8
@@ -188,10 +188,6 @@ export function* eachInstructionValueOperand(
188 yield instrValue.value;
189 break;
190 }
191 - case "ExpressionStatement": {
192 - yield instrValue.value;
193 - break;
194 - }
191 case "Debugger":
192 case "RegExpLiteral":
193 case "LoadGlobal":
@@ -463,10 +459,6 @@ export function mapInstructionOperands(
459 instrValue.value = fn(instrValue.value);
460 break;
461 }
466 - case "ExpressionStatement": {
467 - instrValue.value = fn(instrValue.value);
468 - break;
469 - }
462 case "Debugger":
463 case "RegExpLiteral":
464 case "LoadGlobal":
compiler/forget/packages/babel-plugin-react-forget/src/Inference/InferReferenceEffects.ts
-1
@@ -873,7 +873,6 @@ function inferBlock(
873 state.alias(lvalue, instrValue.value);
874 continue;
875 }
876 - case "ExpressionStatement":
876 case "TypeCastExpression": {
877 // A type cast expression has no effect at runtime, so it's equivalent to a raw
878 // identifier:
compiler/forget/packages/babel-plugin-react-forget/src/Optimization/DeadCodeElimination.ts
-5
@@ -194,11 +194,6 @@ function pruneableValue(value: InstructionValue, state: State): boolean {
194 // explicitly retain debugger statements to not break debugging workflows
195 return false;
196 }
197 - case "ExpressionStatement": {
198 - // We create ExpressionStatements specifically for expressions that would otherwise
199 - // be dropped but which we don't want to eliminate.
200 - return false;
201 - }
197 case "Await":
198 case "CallExpression":
199 case "ComputedDelete":
compiler/forget/packages/babel-plugin-react-forget/src/ReactiveScopes/CodegenReactiveFunction.ts
-4
@@ -1123,10 +1123,6 @@ function codegenInstructionValue(
1123 value = codegenPlace(cx, instrValue.value);
1124 break;
1125 }
1126 - case "ExpressionStatement": {
1127 - value = codegenPlace(cx, instrValue.value);
1128 - break;
1129 - }
1126 case "Debugger":
1127 case "DeclareLocal":
1128 case "DeclareContext":
compiler/forget/packages/babel-plugin-react-forget/src/ReactiveScopes/InferReactiveScopeVariables.ts
-1
@@ -242,7 +242,6 @@ function mayAllocate(env: Environment, instruction: Instruction): boolean {
242 case "TemplateLiteral":
243 case "Primitive":
244 case "NextIterableOf":
245 - case "ExpressionStatement":
245 case "Debugger": {
246 return false;
247 }
compiler/forget/packages/babel-plugin-react-forget/src/ReactiveScopes/PruneNonEscapingScopes.ts
+1 -2
@@ -450,8 +450,7 @@ function computeMemoizationInputs(
450 }
451 case "Await":
452 case "TypeCastExpression":
453 - case "NextIterableOf":
454 - case "ExpressionStatement": {
453 + case "NextIterableOf": {
454 return {
455 // Indirection for the inner value, memoized if the value is
456 lvalues:
compiler/forget/packages/babel-plugin-react-forget/src/TypeInference/InferTypes.ts
-1
@@ -268,7 +268,6 @@ function* generateInstructionTypes(
268 case "TaggedTemplateExpression":
269 case "Await":
270 case "NextIterableOf":
271 - case "ExpressionStatement":
271 case "UnsupportedNode":
272 case "Debugger":
273 break;
compiler/forget/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/ssa-renaming-ternary-destruction.expect.md
+2 -13
@@ -16,7 +16,7 @@ function foo(props) {
16 ```javascript
17 import { unstable_useMemoCache as useMemoCache } from "react";
18 function foo(props) {
19 - const $ = useMemoCache(5);
19 + const $ = useMemoCache(2);
20 const c_0 = $[0] !== props.bar;
21 let x;
22 if (c_0) {
@@ -27,18 +27,7 @@ function foo(props) {
27 } else {
28 x = $[1];
29 }
30 - const c_2 = $[2] !== props;
31 - let t0;
32 - if (c_2) {
33 - t0 = props.cond ? (([x] = [[]]), x.push(props.foo)) : null;
34 - $[2] = props;
35 - $[3] = t0;
36 - $[4] = x;
37 - } else {
38 - t0 = $[3];
39 - x = $[4];
40 - }
41 - t0;
30 + props.cond ? (([x] = [[]]), x.push(props.foo)) : null;
31 return x;
32 }
33
compiler/forget/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/ssa-renaming-ternary.expect.md
+2 -13
@@ -16,7 +16,7 @@ function foo(props) {
16 ```javascript
17 import { unstable_useMemoCache as useMemoCache } from "react";
18 function foo(props) {
19 - const $ = useMemoCache(5);
19 + const $ = useMemoCache(2);
20 const c_0 = $[0] !== props.bar;
21 let x;
22 if (c_0) {
@@ -27,18 +27,7 @@ function foo(props) {
27 } else {
28 x = $[1];
29 }
30 - const c_2 = $[2] !== props;
31 - let t0;
32 - if (c_2) {
33 - t0 = props.cond ? ((x = []), x.push(props.foo)) : null;
34 - $[2] = props;
35 - $[3] = t0;
36 - $[4] = x;
37 - } else {
38 - t0 = $[3];
39 - x = $[4];
40 - }
41 - t0;
30 + props.cond ? ((x = []), x.push(props.foo)) : null;
31 return x;
32 }
33
compiler/forget/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/ssa-renaming-unconditional-ternary.expect.md
+2 -15
@@ -18,7 +18,7 @@ function foo(props) {
18 ```javascript
19 import { unstable_useMemoCache as useMemoCache } from "react";
20 function foo(props) {
21 - const $ = useMemoCache(5);
21 + const $ = useMemoCache(2);
22 const c_0 = $[0] !== props.bar;
23 let x;
24 if (c_0) {
@@ -29,20 +29,7 @@ function foo(props) {
29 } else {
30 x = $[1];
31 }
32 - const c_2 = $[2] !== props;
33 - let t0;
34 - if (c_2) {
35 - t0 = props.cond
36 - ? ((x = []), x.push(props.foo))
37 - : ((x = []), x.push(props.bar));
38 - $[2] = props;
39 - $[3] = t0;
40 - $[4] = x;
41 - } else {
42 - t0 = $[3];
43 - x = $[4];
44 - }
45 - t0;
32 + props.cond ? ((x = []), x.push(props.foo)) : ((x = []), x.push(props.bar));
33 return x;
34 }
35