Making it harder to swallow `error.Canceled`

One thing that I realized people might not be aware of is that you can defensively program against errors being allowed at callsites:

test {
    foo() catch |err| switch (err) {
        error.WriteFailed => comptime unreachable,
        error.ReadFailed => comptime unreachable,
        error.Canceled => comptime unreachable,
        else => |e| return e,
    };
}

fn foo() !void {
    // ...
}

This compiles. However if you return error.Canceled for example from foo, then you get a compile error:

test.zig:5:36: error: reached unreachable code
        error.Canceled => comptime unreachable,
                                   ^~~~~~~~~~~
9 Likes