Skip to content

Commit fd4ec83

Browse files
committed
improvement(semantic/cfg): better control flow for switch statements.
1 parent 20d3e34 commit fd4ec83

5 files changed

+28
-16
lines changed

crates/oxc_semantic/src/builder.rs

+5-4
Original file line numberDiff line numberDiff line change
@@ -1069,12 +1069,13 @@ impl<'a> Visit<'a> for SemanticBuilder<'a> {
10691069
);
10701070
}
10711071

1072-
if let Some(last) = switch_case_conditions.last() {
1073-
self.cfg.add_edge(*last, self.cfg.current_node_ix, EdgeType::Normal);
1072+
let end_of_switch_case_statement = self.cfg.new_basic_block_normal();
1073+
1074+
if let Some(last) = ends_of_switch_cases.last() {
1075+
self.cfg.add_edge(*last, end_of_switch_case_statement, EdgeType::Normal);
10741076
}
10751077

1076-
let current_node_ix = self.cfg.current_node_ix;
1077-
self.cfg.ctx(None).mark_break(current_node_ix).resolve();
1078+
self.cfg.ctx(None).mark_break(end_of_switch_case_statement).resolve();
10781079
/* cfg */
10791080

10801081
self.leave_scope();

crates/oxc_semantic/tests/integration/snapshots/integration__cfg__cfg_files@switch_in_while.js-2.snap

+3-1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ digraph {
1414
7 [ label = "" ]
1515
8 [ label = "ExpressionStatement" ]
1616
9 [ label = "" ]
17+
10 [ label = "" ]
1718
1 -> 0 [ label = "Error(Implicit)" ]
1819
2 -> 0 [ label = "Error(Implicit)" ]
1920
3 -> 0 [ label = "Error(Implicit)" ]
@@ -29,8 +30,9 @@ digraph {
2930
6 -> 7 [ label = "Normal" ]
3031
3 -> 4 [ label = "Normal" ]
3132
3 -> 7 [ label = "Normal" ]
32-
7 -> 8 [ label = "Normal" ]
3333
9 -> 0 [ label = "Error(Implicit)" ]
34+
8 -> 9 [ label = "Normal" ]
35+
10 -> 0 [ label = "Error(Implicit)" ]
3436
1 -> 2 [ label = "Normal" ]
3537
2 -> 3 [ label = "Normal" ]
3638
8 -> 2 [ label = "Backedge" ]

crates/oxc_semantic/tests/integration/snapshots/integration__cfg__cfg_files@switch_in_while.js.snap

+4
Original file line numberDiff line numberDiff line change
@@ -44,3 +44,7 @@ bb8: {
4444
bb9: {
4545

4646
}
47+
48+
bb10: {
49+
50+
}

crates/oxc_semantic/tests/integration/snapshots/integration__cfg__cfg_files@switch_statement.js-2.snap

+11-9
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,10 @@ digraph {
2323
16 [ label = "ExpressionStatement\nExpressionStatement\nExpressionStatement" ]
2424
17 [ label = "" ]
2525
18 [ label = "ExpressionStatement\nreturn <value>" ]
26-
19 [ label = "unreachable\nExpressionStatement\nreturn <value>" ]
27-
20 [ label = "unreachable" ]
28-
21 [ label = "" ]
26+
19 [ label = "unreachable" ]
27+
20 [ label = "ExpressionStatement\nreturn <value>" ]
28+
21 [ label = "unreachable" ]
29+
22 [ label = "" ]
2930
1 -> 0 [ label = "Error(Implicit)" ]
3031
3 -> 2 [ label = "Error(Implicit)" ]
3132
1 -> 3 [ label = "NewFunction" ]
@@ -81,11 +82,12 @@ digraph {
8182
16 -> 17 [ label = "Normal" ]
8283
3 -> 15 [ label = "Normal" ]
8384
3 -> 17 [ label = "Normal" ]
84-
17 -> 19 [ label = "Normal" ]
85-
5 -> 19 [ label = "Jump" ]
86-
10 -> 19 [ label = "Jump" ]
8785
20 -> 2 [ label = "Error(Implicit)" ]
88-
19 -> 20 [ label = "Unreachable" , style = "dotted" ]
89-
21 -> 0 [ label = "Error(Implicit)" ]
90-
1 -> 21 [ label = "Normal" ]
86+
19 -> 20 [ label = "Normal" ]
87+
5 -> 20 [ label = "Jump" ]
88+
10 -> 20 [ label = "Jump" ]
89+
21 -> 2 [ label = "Error(Implicit)" ]
90+
20 -> 21 [ label = "Unreachable" , style = "dotted" ]
91+
22 -> 0 [ label = "Error(Implicit)" ]
92+
1 -> 22 [ label = "Normal" ]
9193
}

crates/oxc_semantic/tests/integration/snapshots/integration__cfg__cfg_files@switch_statement.js.snap

+5-2
Original file line numberDiff line numberDiff line change
@@ -90,14 +90,17 @@ bb18: {
9090

9191
bb19: {
9292
unreachable
93+
}
94+
95+
bb20: {
9396
statement
9497
return <value>
9598
}
9699

97-
bb20: {
100+
bb21: {
98101
unreachable
99102
}
100103

101-
bb21: {
104+
bb22: {
102105

103106
}

0 commit comments

Comments
 (0)