Skip to content

Commit 88634f0

Browse files
committed
stage1: Allow variable capture for multi-prong switch arms
Handle the multi-prong case as we do with range cases. Closes #7188
1 parent 3634d44 commit 88634f0

File tree

2 files changed

+22
-0
lines changed

2 files changed

+22
-0
lines changed

src/stage1/ir.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24207,6 +24207,8 @@ static IrInstGen *ir_analyze_instruction_switch_var(IrAnalyze *ira, IrInstSrcSwi
2420724207
ref_type->data.pointer.allow_zero);
2420824208
return ir_analyze_ptr_cast(ira, &instruction->base.base, target_value_ptr,
2420924209
&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;
2421024212
} else {
2421124213
ir_add_error(ira, &instruction->base.base,
2421224214
buf_sprintf("switch on type '%s' provides no expression parameter", buf_ptr(&target_type->name)));

test/stage1/behavior/switch.zig

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -436,6 +436,26 @@ test "switch with disjoint range" {
436436
}
437437
}
438438

439+
test "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+
439459
var state: u32 = 0;
440460
fn poll() void {
441461
switch (state) {

0 commit comments

Comments
 (0)