Skip to content
Language referenceZig 0.17.1Official documentation

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_noreturn.zig
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);
}
Terminal
$ 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:

test_noreturn_from_exit.zig
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;
}
Terminal
$ zig test test_noreturn_from_exit.zig -target x86_64-windows --test-no-exec