authorgravatar for thatlemon@gmail.comLemonBoy <thatlemon@gmail.com> 2020-12-29 12:58:47+01:00
committergravatar for thatlemon@gmail.comLemonBoy <thatlemon@gmail.com> 2020-12-30 09:42:20+01:00
log88634f0481fb1bd78f8d018b76cab6590691c8a9
tree68f4e1cc2f26f176de68c880e83b8f2484165928
parent3634d44d08c73326c7783cf2a3095acb50eb2e1c

stage1: Allow variable capture for multi-prong switch arms

Handle the multi-prong case as we do with range cases. Closes #7188

2 files changed, 22 insertions(+), 0 deletions(-)

src/stage1/ir.cpp+2
...@@ -24207,6 +24207,8 @@ static IrInstGen *ir_analyze_instruction_switch_var(IrAnalyze *ira, IrInstSrcSwi...@@ -24207,6 +24207,8 @@ static IrInstGen *ir_analyze_instruction_switch_var(IrAnalyze *ira, IrInstSrcSwi
24207 ref_type->data.pointer.allow_zero);24207 ref_type->data.pointer.allow_zero);
24208 return ir_analyze_ptr_cast(ira, &instruction->base.base, target_value_ptr,24208 return ir_analyze_ptr_cast(ira, &instruction->base.base, target_value_ptr,
24209 &instruction->target_value_ptr->base, new_target_value_ptr_type, &instruction->base.base, false, false);24209 &instruction->target_value_ptr->base, new_target_value_ptr_type, &instruction->base.base, false, false);
24210 } else if (instruction->prongs_len > 1) {
24211 return target_value_ptr;
24210 } else {24212 } else {
24211 ir_add_error(ira, &instruction->base.base,24213 ir_add_error(ira, &instruction->base.base,
24212 buf_sprintf("switch on type '%s' provides no expression parameter", buf_ptr(&target_type->name)));24214 buf_sprintf("switch on type '%s' provides no expression parameter", buf_ptr(&target_type->name)));
test/stage1/behavior/switch.zig+20
...@@ -436,6 +436,26 @@ test "switch with disjoint range" {...@@ -436,6 +436,26 @@ test "switch with disjoint range" {
436 }436 }
437}437}
438438
439test "switch variable for range and multiple prongs" {
440 const S = struct {
441 fn doTheTest() void {
442 var u: u8 = 16;
443 doTheSwitch(u);
444 comptime doTheSwitch(u);
445 var v: u8 = 42;
446 doTheSwitch(v);
447 comptime doTheSwitch(v);
448 }
449 fn doTheSwitch(q: u8) void {
450 switch (q) {
451 0...40 => |x| expect(x == 16),
452 41, 42, 43 => |x| expect(x == 42),
453 else => expect(false),
454 }
455 }
456 };
457}
458
439var state: u32 = 0;459var state: u32 = 0;
440fn poll() void {460fn poll() void {
441 switch (state) {461 switch (state) {