noreturn
noreturn is the type of:
breakcontinuereturnunreachablewhile (true) {}
When resolving types together, such as if clauses or switch prongs,
the noreturn type is compatible with every other type. Consider:
fn foo(condition: bool, b: u32) void {
const a = if (condition) b else return;
_ = a;
@panic("do something with a");
}
test "noreturn" {
foo(false, 1);
}$ zig test test_noreturn.zig
1/1 test_noreturn.test.noreturn...OK
All 1 tests passed.
Another use case for noreturn is the exit function:
const std = @import("std");
const expectEqual = std.testing.expectEqual;
extern "kernel32" fn ExitProcess(exit_code: c_uint) callconv(.winapi) noreturn;
test "foo" {
const value = bar() catch ExitProcess(1);
try expectEqual(1234, value);
}
fn bar() anyerror!u32 {
return 1234;
}$ zig test test_noreturn_from_exit.zig -target x86_64-windows --test-no-exec