From 0e789d3f1320751689d36b889c2d5c1c9ae8b176 Mon Sep 17 00:00:00 2001 From: David Gonzalez Martin Date: Tue, 15 Apr 2025 11:41:08 -0600 Subject: [PATCH] Switch else empty --- src/bootstrap.zig | 10 +++++++++- tests/switch_else_empty.bbb | 23 +++++++++++++++++++++++ 2 files changed, 32 insertions(+), 1 deletion(-) create mode 100644 tests/switch_else_empty.bbb diff --git a/src/bootstrap.zig b/src/bootstrap.zig index 24e7d80..5646ff9 100644 --- a/src/bootstrap.zig +++ b/src/bootstrap.zig @@ -6561,7 +6561,7 @@ pub const Module = struct { const previous_exit_block = current_function.exit_block; defer current_function.exit_block = previous_exit_block; - const exit_block = module.llvm.context.create_basic_block("exit_block", null); + const exit_block = module.llvm.context.create_basic_block("exit_block", llvm_function); current_function.exit_block = exit_block; module.analyze(function, switch_statement.discriminant, .{}); @@ -6592,6 +6592,9 @@ pub const Module = struct { const else_block = if (else_clause_index) |i| switch_statement.clauses[i].basic_block else module.llvm.context.create_basic_block("switch.else_case_block", llvm_function); const switch_instruction = module.llvm.builder.create_switch(switch_statement.discriminant.llvm.?, else_block, total_discriminant_cases); + + var all_blocks_terminated = true; + for (switch_statement.clauses) |clause| { for (clause.values) |v| { switch_instruction.add_case(v.llvm.?, clause.basic_block); @@ -6601,6 +6604,7 @@ pub const Module = struct { module.llvm.builder.position_at_end(clause.basic_block); module.analyze_block(function, clause.block); if (module.llvm.builder.get_insert_block() != null) { + all_blocks_terminated = false; _ = module.llvm.builder.create_branch(exit_block); module.llvm.builder.clear_insertion_position(); } @@ -6613,6 +6617,10 @@ pub const Module = struct { _ = module.llvm.builder.create_unreachable(); module.llvm.builder.clear_insertion_position(); } + + if (!all_blocks_terminated) { + module.llvm.builder.position_at_end(exit_block); + } }, else => @trap(), } diff --git a/tests/switch_else_empty.bbb b/tests/switch_else_empty.bbb new file mode 100644 index 0000000..af7db27 --- /dev/null +++ b/tests/switch_else_empty.bbb @@ -0,0 +1,23 @@ +E = enum +{ + a, + b, + c, +} + +[export] main = fn [cc(c)] () s32 +{ + >some_enum: E = .b; + switch (some_enum) + { + .a => + { + return 1; + }, + else => + { + }, + } + + return 0; +}