Skip to content

Commit 66e5e92

Browse files
authored
Merge pull request #7592 from LemonBoy/fix-7188
Allow variable captures on multi-prong switch arms
2 parents d957244 + 2561168 commit 66e5e92

File tree

4 files changed

+26
-10
lines changed

4 files changed

+26
-10
lines changed

lib/std/dwarf.zig

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -413,10 +413,7 @@ pub const DwarfInfo = struct {
413413
var this_unit_offset: u64 = 0;
414414

415415
while (this_unit_offset < try seekable.getEndPos()) {
416-
seekable.seekTo(this_unit_offset) catch |err| switch (err) {
417-
error.EndOfStream => unreachable,
418-
else => return err,
419-
};
416+
try seekable.seekTo(this_unit_offset);
420417

421418
var is_64: bool = undefined;
422419
const unit_length = try readUnitLength(in, di.endian, &is_64);
@@ -520,10 +517,7 @@ pub const DwarfInfo = struct {
520517
var this_unit_offset: u64 = 0;
521518

522519
while (this_unit_offset < try seekable.getEndPos()) {
523-
seekable.seekTo(this_unit_offset) catch |err| switch (err) {
524-
error.EndOfStream => unreachable,
525-
else => return err,
526-
};
520+
try seekable.seekTo(this_unit_offset);
527521

528522
var is_64: bool = undefined;
529523
const unit_length = try readUnitLength(in, di.endian, &is_64);

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) {

test/stage1/behavior/union.zig

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -742,7 +742,7 @@ test "@unionInit on union w/ tag but no fields" {
742742
const Data = union(Type) {
743743
no_op: void,
744744

745-
pub fn decode(buf: []const u8) !Data {
745+
pub fn decode(buf: []const u8) Data {
746746
return @unionInit(Data, "no_op", {});
747747
}
748748
};
@@ -753,7 +753,7 @@ test "@unionInit on union w/ tag but no fields" {
753753

754754
fn doTheTest() void {
755755
var data: Data = .{ .no_op = .{} };
756-
var o = try Data.decode(&[_]u8{});
756+
var o = Data.decode(&[_]u8{});
757757
expectEqual(Type.no_op, o);
758758
}
759759
};

0 commit comments

Comments
 (0)