Zig Version
0.16.0-dev.2261+d6b3dd25a
Steps to Reproduce and Observed Behavior
build
~/zig-0.16/zig build-exe repro.zig
repro.zig
const std = @import("std");
const linux = std.os.linux;
pub fn main() !void {
retryIfInterrupted(pauseWithError, .{}) catch |e| switch(e) {
error.SomeOtherError => return,
};
}
fn retryIfInterrupted(comptime function: anytype, args: anytype) !@typeInfo(@typeInfo(@TypeOf(function)).@"fn".return_type.?).error_union.payload {
while (true) {
return @call(.auto, function, args) catch |e| switch (e) {
error.Interrupted => continue,
else => @errorCast(e),
};
}
}
fn pauseWithError() !void {
switch (linux.errno(linux.pause())) {
.SUCCESS => return,
.INTR => return error.Interrupted,
.INVAL => return error.SomeOtherError,
else => |e| {
std.debug.panic("unexpected errno: {d}", .{e});
},
}
}
Replacing @errorCast(e) with e causes a compiler error instead of a crash.
Side note: is this code possible to write? retryIfInterrupted infers that it could return error.Interrupted but it's handled.
Expected Behavior
No crash.