Skip to content

Allow variable captures on multi-prong switch arms #7592

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jan 4, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 2 additions & 8 deletions lib/std/dwarf.zig
Original file line number Diff line number Diff line change
Expand Up @@ -413,10 +413,7 @@ pub const DwarfInfo = struct {
var this_unit_offset: u64 = 0;

while (this_unit_offset < try seekable.getEndPos()) {
seekable.seekTo(this_unit_offset) catch |err| switch (err) {
error.EndOfStream => unreachable,
else => return err,
};
try seekable.seekTo(this_unit_offset);

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

while (this_unit_offset < try seekable.getEndPos()) {
seekable.seekTo(this_unit_offset) catch |err| switch (err) {
error.EndOfStream => unreachable,
else => return err,
};
try seekable.seekTo(this_unit_offset);

var is_64: bool = undefined;
const unit_length = try readUnitLength(in, di.endian, &is_64);
Expand Down
2 changes: 2 additions & 0 deletions src/stage1/ir.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)));
Expand Down
20 changes: 20 additions & 0 deletions test/stage1/behavior/switch.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
4 changes: 2 additions & 2 deletions test/stage1/behavior/union.zig
Original file line number Diff line number Diff line change
Expand Up @@ -742,7 +742,7 @@ test "@unionInit on union w/ tag but no fields" {
const Data = union(Type) {
no_op: void,

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

fn doTheTest() void {
var data: Data = .{ .no_op = .{} };
var o = try Data.decode(&[_]u8{});
var o = Data.decode(&[_]u8{});
expectEqual(Type.no_op, o);
}
};
Expand Down