Commit 697c768730

Andrew Kelley <superjoe30@gmail.com>
2016-11-26 10:03:39
IR: support switch with range
1 parent bbf785b
Changed files (2)
src/ir.cpp
@@ -630,6 +630,7 @@ static IrInstruction *ir_build_phi(IrBuilder *irb, AstNode *source_node,
     phi_instruction->incoming_values = incoming_values;
 
     for (size_t i = 0; i < incoming_count; i += 1) {
+        ir_ref_bb(incoming_blocks[i]);
         ir_ref_instruction(incoming_values[i]);
     }
 
@@ -2784,8 +2785,8 @@ static void ir_start_bb(IrAnalyze *ira, IrBasicBlock *old_bb, IrBasicBlock *cons
     ira->old_irb.current_basic_block = old_bb;
     ira->const_predecessor_bb = const_predecessor_bb;
 
-    assert(old_bb->other);
-    ira->new_irb.exec->basic_block_list.append(old_bb->other);
+    if (old_bb->other)
+        ira->new_irb.exec->basic_block_list.append(old_bb->other);
 }
 
 static void ir_finish_bb(IrAnalyze *ira) {
test/self_hosted2.zig
@@ -20,6 +20,35 @@ fn inlinedLoop() {
     assert(sum == 15);
 }
 
+fn switchWithNumbers() {
+    testSwitchWithNumbers(13);
+}
+
+fn testSwitchWithNumbers(x: u32) {
+    const result = switch (x) {
+        1, 2, 3, 4 ... 8 => false,
+        13 => true,
+        else => false,
+    };
+    assert(result);
+}
+
+fn switchWithAllRanges() {
+    assert(testSwitchWithAllRanges(50, 3) == 1);
+    assert(testSwitchWithAllRanges(101, 0) == 2);
+    assert(testSwitchWithAllRanges(300, 5) == 3);
+    assert(testSwitchWithAllRanges(301, 6) == 6);
+}
+
+fn testSwitchWithAllRanges(x: u32, y: u32) -> u32 {
+    switch (x) {
+        0 ... 100 => 1,
+        101 ... 200 => 2,
+        201 ... 300 => 3,
+        else => y,
+    }
+}
+
 fn assert(ok: bool) {
     if (!ok)
         @unreachable();
@@ -29,6 +58,8 @@ fn runAllTests() {
     emptyFunctionWithComments();
     disabledExternFn();
     inlinedLoop();
+    switchWithNumbers();
+    switchWithAllRanges();
 }
 
 export nakedcc fn _start() -> unreachable {