@samitouri / QOS-React-2 / commits / 7a7538920e

Broken test for labeled statements

When we convert a LabeledStatement to HIR we can end up emitting "consecutive" blocks, ie where there are two blocks such that control flow will always go from from one block to the other, with no other way to reach the second block but through the first. Example: ```javascript label: { foo(); break label; } bar(); ``` Converts to ``` bb0: foo() goto bb1: bb1: bar(); ... ``` Ideally in this case we would merge these into a single block: * When debugging, the extra goto makes it look like there is conditional control flow when there isn't. If the code is consecutive it's easier to understand that if it's a single block. * Conversion from HIR -> AST relies on consecutive code all being in a single block, so this breaks codegen (we never visit the goto target since all gotos are assumed to be safe to convert to a break or continue). This PR adds a failing test case, the next PR fixes it.

Joe Savona committed Jan 9, 2023 at 13:04 UTC 7a7538920eee213e70351d467e514b4e7be637a9
4 files changed +56 -4
compiler/forget/src/__tests__/fixtures/hir/inverted-if.expect.md
+22 -4
@@ -10,6 +10,7 @@ function foo(a, b, c) {
10 break label;
11 }
12 }
13 + return y;
14 }
15
16 ```
@@ -18,12 +19,29 @@ function foo(a, b, c) {
19
20 ```javascript
21 function foo(a, b, c) {
21 - const y = [];
22 - if (a) {
23 - if (b) {
24 - y.push(c);
22 + const $ = React.useMemoCache();
23 + const c_0 = $[0] !== a;
24 + const c_1 = $[1] !== b;
25 + const c_2 = $[2] !== c;
26 + let y;
27 + if (c_0 || c_1 || c_2) {
28 + y = [];
29 +
30 + if (a) {
31 + if (b) {
32 + y.push(c);
33 + }
34 }
35 +
36 + $[0] = a;
37 + $[1] = b;
38 + $[2] = c;
39 + $[3] = y;
40 + } else {
41 + y = $[3];
42 }
43 +
44 + return y;
45 }
46
47 ```
compiler/forget/src/__tests__/fixtures/hir/inverted-if.js
+1
@@ -6,4 +6,5 @@ function foo(a, b, c) {
6 break label;
7 }
8 }
9 + return y;
10 }
compiler/forget/src/__tests__/fixtures/hir/unconditional-break-label.expect.md new
+25
@@ -0,0 +1,25 @@
1 +
2 +## Input
3 +
4 +```javascript
5 +function foo(a) {
6 + let x = 0;
7 + bar: {
8 + x = 1;
9 + break bar;
10 + }
11 + return a + x;
12 +}
13 +
14 +```
15 +
16 +## Code
17 +
18 +```javascript
19 +function foo(a) {
20 + const x = 0;
21 + const x$0 = 1;
22 +}
23 +
24 +```
25 +
\ No newline at end of file
compiler/forget/src/__tests__/fixtures/hir/unconditional-break-label.js new
+8
@@ -0,0 +1,8 @@
1 +function foo(a) {
2 + let x = 0;
3 + bar: {
4 + x = 1;
5 + break bar;
6 + }
7 + return a + x;
8 +}