Skip to content

Commit

Permalink
Value: implement orderAgainstZeroInner for errors
Browse files Browse the repository at this point in the history
  • Loading branch information
Rexicon226 committed Dec 28, 2024
1 parent 30169d1 commit 61d251f
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/Value.zig
Original file line number Diff line number Diff line change
Expand Up @@ -1066,6 +1066,7 @@ pub fn orderAgainstZeroInner(
.float => |float| switch (float.storage) {
inline else => |x| std.math.order(x, 0),
},
.err => .gt, // error values cannot be 0
else => unreachable,
},
};
Expand Down
25 changes: 25 additions & 0 deletions test/behavior/switch_loop.zig
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,31 @@ test "switch loop on enum" {
try comptime S.doTheTest();
}

test "switch loop with error set" {
if (builtin.zig_backend == .stage2_wasm) return error.SkipZigTest; // TODO
if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO
if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO
if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO
if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest; // TODO

const S = struct {
const E = error{ Foo, Bar, Baz };

fn doTheTest() !void {
var start: E = undefined;
start = error.Foo;
const result: u32 = s: switch (start) {
error.Foo => continue :s error.Bar,
error.Bar => continue :s error.Baz,
error.Baz => 123,
};
try expect(result == 123);
}
};
try S.doTheTest();
try comptime S.doTheTest();
}

test "switch loop on tagged union" {
if (builtin.zig_backend == .stage2_wasm) return error.SkipZigTest; // TODO
if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO
Expand Down

0 comments on commit 61d251f

Please sign in to comment.