Skip to content
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
Closed

allow multiple kinds of return types from main #535

andrewrk opened this issue Oct 14, 2017 · 3 comments
Labels
accepted This proposal is planned. enhancement Solving this issue will likely involve adding new logic or components to the codebase. proposal This issue suggests modifications. If it also has the "accepted" label then it is planned.
Milestone

Comments

@andrewrk
Copy link
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 accepted This proposal is planned. enhancement Solving this issue will likely involve adding new logic or components to the codebase. proposal This issue suggests modifications. If it also has the "accepted" label then it is planned. labels Oct 14, 2017
@andrewrk andrewrk added this to the 0.3.0 milestone Oct 14, 2017
@thejoshwolfe
Copy link
Sponsor Contributor

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

@skyfex
Copy link

skyfex commented Nov 29, 2017

Microcontroller firmware is one more usecase.

@andrewrk
Copy link
Member Author

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 allow return type of main to be noreturn allow multiple kinds of return types from main Jan 12, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
accepted This proposal is planned. enhancement Solving this issue will likely involve adding new logic or components to the codebase. proposal This issue suggests modifications. If it also has the "accepted" label then it is planned.
Projects
None yet
Development

No branches or pull requests

3 participants