@samitouri / QOS-React-1 / commits / 21c5631570

[be] Prune implicit breaks when flattening unused labels

A labeled block will generally end with an implicit break out of the label. However, if there are no _explicit_ breaks to the label, we'll end up with a ReactiveFunction along the lines of: ``` bb1: { ...instructions with no explicit `break bb1`... (implicit) break; } ``` The `PruneUnusedLabels` pass removes such unused labels, inlining the content of label terminal into the surrounding block. However, we weren't pruning the `break`! This wasn't a problem in practice since codegen, and future passes, would just ignore this. But it's more correct to go and find these unnecessary implicit breaks and prune them, which this PR does. Again, this shouldn't have any impact other than producing cleaner ReactiveFunction data during debugging.

Joe Savona committed Feb 14, 2024 at 15:36 UTC 21c563157003ded36fcc54589a6075d2dd157c90
2 files changed +14 -1
compiler/packages/babel-plugin-react-forget/src/ReactiveScopes/PrintReactiveFunction.ts
+3
@@ -132,6 +132,9 @@ function writeReactiveInstruction(
132 break;
133 }
134 case "terminal": {
135 + if (instr.label !== null) {
136 + writer.write(`bb${instr.label}: `);
137 + }
138 writeTerminal(writer, instr.terminal);
139 break;
140 }
compiler/packages/babel-plugin-react-forget/src/ReactiveScopes/PruneUnusedLabels.ts
+11 -1
@@ -45,7 +45,17 @@ class Transform extends ReactiveFunctionTransform<Labels> {
45 const isReachableLabel = stmt.label !== null && state.has(stmt.label);
46 if (stmt.terminal.kind === "label" && !isReachableLabel) {
47 // Flatten labeled terminals where the label isn't necessary
48 - return { kind: "replace-many", value: stmt.terminal.block };
48 + const block = [...stmt.terminal.block];
49 + const last = block.at(-1);
50 + if (
51 + last !== undefined &&
52 + last.kind === "terminal" &&
53 + last.terminal.kind === "break" &&
54 + last.terminal.label === null
55 + ) {
56 + block.pop();
57 + }
58 + return { kind: "replace-many", value: block };
59 } else {
60 if (!isReachableLabel) {
61 stmt.label = null;