Inline else not removing errors

fn bar() error{ Bar, Foo }!void {
    return error.Bar;
}

fn foo() error{Foo}!void {
    bar() catch |err| switch (err) {
        error.Bar => {},
        inline else => |e| return e,
    };
}

test {
    try foo();
}
> zig test foo.zig
foo.zig:8:35: error: expected type 'error{Foo}!void', found 'error{Bar,Foo}'
        inline else => |e| return e,
                                  ^
foo.zig:8:35: note: 'error.Bar' not a member of destination error set
foo.zig:5:20: note: function return type declared here
fn foo() error{Foo}!void {
         ~~~~~~~~~~^~~~~

I thought this would recognize that the e can’t be error.Bar. What should I do in a situation like this?

Issue: #31887
Fixed in #35207 (0.17.0)

1 Like

In 0.16.0 you can do return @errorCast(e), which has safety checked illegal behavior if e is not in the error union of the function, instead of doing this at comptime.

Thank you for this workaround. I started to update to master but ziglang/translate-c doesn’t compile on master. I’ll just stay on 0.16.0 for a bit longer.

You might need to update translate-c. The main branch of https://codeberg.org/ziglang/translate-c should compile with zig master.