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

Closed
andrewrk opened this Issue Oct 14, 2017 · 3 comments

Comments

Projects
None yet
3 participants
@andrewrk
Member

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

@thejoshwolfe

This comment has been minimized.

Member

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.

@skyfex

This comment has been minimized.

skyfex commented Nov 29, 2017

Microcontroller firmware is one more usecase.

@andrewrk

This comment has been minimized.

Member

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 => {
        root.main();
    },
    builtin.TypeId.Void => {
        root.main();
        std.os.exit(0);
    },
    builtin.TypeId.ErrorUnion => {
        if (builtin.mode == builtin.Mode.ReleaseFast) {
            root.main() catch std.os.exit(1);
            std.os.exit(0);
        } else {
            root.main() catch |err, trace| {
                std.debug.warn("error: {}\n", @errorName(err));
                std.debug.dumpStackTrace(trace);
                std.os.exit(1);
            };
            std.os.exit(0);
        }
    },
    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