noreturn

noreturn

noreturn is the type of:

  • break
  • continue
  • return
  • unreachable
  • while (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 noreturn...OK
All tests passed.

Another use case for noreturn is the exit function:

test.zig

pub extern "kernel32" stdcallcc fn ExitProcess(exit_code: c_uint) noreturn;

test "foo" {
    const value = bar() catch ExitProcess(1);
    assert(value == 1234);
}

fn bar() anyerror!u32 {
    return 1234;
}

const assert = @import("std").debug.assert;
``` ```
$ zig test test.zig
Created /home/andy/dev/zig/docgen_tmp/test.exe but skipping execution because it is non-native.