New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

allow multiple kinds of return types from main #535

andrewrk opened this Issue Oct 14, 2017 · 3 comments


None yet
3 participants

andrewrk commented Oct 14, 2017

Currently, this is the main entry point function:

pub fn main() -> %void {


The bootstrap code runs it, checks the return value, and makes the exit/ExitProcess syscall.

If a zig user wants to do their own process exiting, however, they should be able to make the return type noreturn, and then the bootstrap code does not need to handle cleanup.

This shaves off a couple of bytes of bloat for some of our more hardcore users such as demoscene programmers.

@andrewrk andrewrk added this to the 0.3.0 milestone Oct 14, 2017


This comment has been minimized.


thejoshwolfe commented Oct 16, 2017

Another usecase for returning noreturn is if a program can't stop, won't stop. This is common for daemons such as groovebasin.


This comment has been minimized.

skyfex commented Nov 29, 2017

Microcontroller firmware is one more usecase.


This comment has been minimized.


andrewrk commented Jan 12, 2018

more proposal:

allow multiple kinds of return types from main

  • noreturn - main never returns
  • void - main does not fail
  • %void - possibly print an error return trace
switch (@typeId(@returnType(root.main))) {
    builtin.TypeId.NoReturn => {
    builtin.TypeId.Void => {
    builtin.TypeId.ErrorUnion => {
        if (builtin.mode == builtin.Mode.ReleaseFast) {
            root.main() catch std.os.exit(1);
        } else {
            root.main() catch |err, trace| {
                std.debug.warn("error: {}\n", @errorName(err));
    else => @compileError("expected return type of main to be 'noreturn', 'void', or '%void'"),

@andrewrk andrewrk changed the title from allow return type of main to be `noreturn` to allow multiple kinds of return types from main Jan 12, 2018

@andrewrk andrewrk closed this in 7b57454 Jan 15, 2018

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment