mirror of
https://github.com/ziglang/zig.git
synced 2024-12-03 10:28:48 +00:00
stage1: Allow variable capture for multi-prong switch arms
Handle the multi-prong case as we do with range cases. Closes #7188
This commit is contained in:
parent
3634d44d08
commit
88634f0481
@ -24207,6 +24207,8 @@ static IrInstGen *ir_analyze_instruction_switch_var(IrAnalyze *ira, IrInstSrcSwi
|
||||
ref_type->data.pointer.allow_zero);
|
||||
return ir_analyze_ptr_cast(ira, &instruction->base.base, target_value_ptr,
|
||||
&instruction->target_value_ptr->base, new_target_value_ptr_type, &instruction->base.base, false, false);
|
||||
} else if (instruction->prongs_len > 1) {
|
||||
return target_value_ptr;
|
||||
} else {
|
||||
ir_add_error(ira, &instruction->base.base,
|
||||
buf_sprintf("switch on type '%s' provides no expression parameter", buf_ptr(&target_type->name)));
|
||||
|
@ -436,6 +436,26 @@ test "switch with disjoint range" {
|
||||
}
|
||||
}
|
||||
|
||||
test "switch variable for range and multiple prongs" {
|
||||
const S = struct {
|
||||
fn doTheTest() void {
|
||||
var u: u8 = 16;
|
||||
doTheSwitch(u);
|
||||
comptime doTheSwitch(u);
|
||||
var v: u8 = 42;
|
||||
doTheSwitch(v);
|
||||
comptime doTheSwitch(v);
|
||||
}
|
||||
fn doTheSwitch(q: u8) void {
|
||||
switch (q) {
|
||||
0...40 => |x| expect(x == 16),
|
||||
41, 42, 43 => |x| expect(x == 42),
|
||||
else => expect(false),
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
var state: u32 = 0;
|
||||
fn poll() void {
|
||||
switch (state) {
|
||||
|
Loading…
Reference in New Issue
Block a user