zig icon indicating copy to clipboard operation
zig copied to clipboard

Errdefer payload type doesn't match function signature

Open J-MR-T opened this issue 1 year ago • 0 comments

Zig Version

0.12.0

Steps to Reproduce and Observed Output

Not 100% sure whether you would classify this as a bad error message, or a straight up bug:

Try to compile this file:

const ParseError = error{OutOfMemory, Syntax};

pub fn fails() ParseError!void{
    errdefer |err| {
        switch(err) {
            error.Syntax => {},
            error.OutOfMemory => {},
        }
    }
    return error.Syntax;
}

pub fn main() !void{
    try fails();
}

The error message is currently

mini.zig:8:13: error: expected type 'error{Syntax}', found 'error{OutOfMemory}'
            error.OutOfMemory => {},
            ^~~~~~~~~~~~~~~~~

Which I don't find helpful. The problem seems to be, that errdefer infers the error payload type by what errors are actually returned, as opposed to the functions error union error type (one way to fix the issue is to cast the err type to ParseError using @as). This behavior seems counter intuitive to me. If that is intended, I think this should at least be diagnosed in a different way.

Expected Output

I would expect something telling me that the error I'm trying to switch on can not be thrown by the curent function. Or, again, that this is just valid.

J-MR-T avatar May 25 '24 17:05 J-MR-T