noreturn
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:
test.zig
fn foo(condition: bool, b: u32) void {
const a = if (condition) b else return;
@panic("do something with a");
}
test "noreturn" {
foo(false, 1);
}
``` ```
$ zig test test.zig
Test [1/1] test "noreturn"...
All 1 tests passed.
Another use case for noreturn is the exit function:
test.zig
pub extern "kernel32" fn ExitProcess(exit_code: c_uint) callconv(if (@import("builtin").target.cpu.arch == .i386) .Stdcall else .C) noreturn;
test "foo" {
const value = bar() catch ExitProcess(1);
try expect(value == 1234);
}
fn bar() anyerror!u32 {
return 1234;
}
const expect = @import("std").testing.expect;
``` ```
$ zig test test.zig -target x86_64-windows
warning: created docgen_tmp/zig-cache/o/107d50a53d4f82371a6656c54d415ac0/test.exe but skipping execution because it is non-native
