unreachable

    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.

    test_unreachable.zig

    1. $ zig test test_unreachable.zig
    2. 1/1 test "basic math"... OK
    3. All 1 tests passed.

    In fact, this is how std.debug.assert is implemented:

    test.zig

    1. $ zig test test.zig
    2. /home/andy/Downloads/zig/docgen_tmp/test.zig:3:14: 0x207f3b in assert (test)
    3. if (!ok) unreachable; // assertion failure
    4. ^
    5. /home/andy/Downloads/zig/docgen_tmp/test.zig:8:11: 0x2079ce in test "this will fail" (test)
    6. assert(false);
    7. /home/andy/Downloads/zig/lib/std/special/test_runner.zig:80:28: 0x22f423 in std.special.main (test)
    8. } else test_fn.func();
    9. ^
    10. /home/andy/Downloads/zig/lib/std/start.zig:543:22: 0x227e1c in std.start.callMain (test)
    11. root.main();
    12. ^
    13. /home/andy/Downloads/zig/lib/std/start.zig:495:12: 0x20923e in std.start.callMainWithArgs (test)
    14. return @call(.{ .modifier = .always_inline }, callMain, .{});
    15. /home/andy/Downloads/zig/lib/std/start.zig:409:17: 0x2082d6 in std.start.posixCallMainAndExit (test)
    16. std.os.exit(@call(.{ .modifier = .always_inline }, callMainWithArgs, .{ argc, argv, envp }));
    17. ^
    18. @call(.{ .modifier = .never_inline }, posixCallMainAndExit, .{});
    19. ^
    20. error: the following test command crashed:
    21. docgen_tmp/zig-cache/o/8e4964e05ddaf866e77360a551b3c05b/test /home/andy/Downloads/zig/build-release/zig

    At Compile-Time

    test.zig

    Shell

    1. $ zig test test.zig
    2. docgen_tmp/test.zig:10:16: error: unreachable code
    3. assert(@TypeOf(unreachable) == noreturn);
    4. ^
    5. docgen_tmp/test.zig:10:24: note: control flow is diverted here
    6. assert(@TypeOf(unreachable) == noreturn);