In ReleaseFast
mode, the optimizer uses the assumption that unreachable
code will never be hit to perform optimizations. However, zig test
even in ReleaseFast
mode still emits unreachable
as calls to panic
.
$ zig test test.zig
1/1 test "basic math"... OK
In fact, this is how std.debug.assert
is implemented:
$ zig test test.zig
1/1 test "this will fail"... reached unreachable code
/home/andy/Downloads/zig/docgen_tmp/test.zig:3:14: 0x206dab in assert (test)
if (!ok) unreachable; // assertion failure
^
assert(false);
^
/home/andy/Downloads/zig/lib/std/special/test_runner.zig:61:28: 0x22d9a1 in std.special.main (test)
} else test_fn.func();
^
/home/andy/Downloads/zig/lib/std/start.zig:334:37: 0x2072ad in std.start.posixCallMainAndExit (test)
^
/home/andy/Downloads/zig/lib/std/start.zig:162:5: 0x206fe2 in std.start._start (test)
@call(.{ .modifier = .never_inline }, posixCallMainAndExit, .{});
error: the following test command crashed:
docgen_tmp/zig-cache/o/2e7f715609483f0cd9100db66b70c1a0/test
At Compile-Time
test.zig
$ zig test test.zig
./docgen_tmp/test.zig:10:16: error: unreachable code
assert(@TypeOf(unreachable) == noreturn);
^
./docgen_tmp/test.zig:3:28: note: referenced here
test "type of unreachable" {
^