ziglang / zig

General-purpose programming language and toolchain for maintaining robust, optimal, and reusable software.
https://ziglang.org
MIT License
34.88k stars 2.55k forks source link

Segmentation fault compiling call to function returning `undefined` element of empty error set in `comptime` block #21298

Open amp-59 opened 2 months ago

amp-59 commented 2 months ago

Zig Version

0.14.0-dev.1411+a670f5519

Steps to Reproduce and Observed Behaviour

Compile example program with zig build-obj error_name_error.zig error_name_error.zig:

fn fn0() error{} {
    return undefined;
}
comptime {
    _ = fn0();
}

Output:

zig build-obj error_name_error.zig
Segmentation fault
mlugg commented 1 month ago

Hmm... the issue here is in checking whether to add the return value to the error return trace. But, this makes me think -- should returning undefined from a function returning an error set or union be considered Illegal Behavior? After all, at runtime, deciding whether a function is put in the error return trace requires a branch on the error tag, so if that tag is undef, that check exhibits Unchecked Illegal Behavior (hence UB). cc @andrewrk