diff options
| author | gooncreeper <149842806+gooncreeper@users.noreply.github.com> | 2024-07-15 09:29:07 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-07-15 12:29:07 +0300 |
| commit | 9002977051b810ec023589c7e89283acb3ca9fbf (patch) | |
| tree | ac133cc279368631d46e6a87cd76d6c4a7aeb2f3 /lib/std/start.zig | |
| parent | 5675553aedf72c3cf6135025fe216fb92f09f3fa (diff) | |
| download | zig-9002977051b810ec023589c7e89283acb3ca9fbf.tar.gz zig-9002977051b810ec023589c7e89283acb3ca9fbf.zip | |
start: refactor callMain return type checking
Diffstat (limited to 'lib/std/start.zig')
| -rw-r--r-- | lib/std/start.zig | 33 |
1 files changed, 13 insertions, 20 deletions
diff --git a/lib/std/start.zig b/lib/std/start.zig index 187048dfd3..0f169f112d 100644 --- a/lib/std/start.zig +++ b/lib/std/start.zig @@ -497,21 +497,19 @@ fn mainWithoutEnv(c_argc: c_int, c_argv: [*][*:0]c_char) callconv(.C) c_int { const bad_main_ret = "expected return type of main to be 'void', '!void', 'noreturn', 'u8', or '!u8'"; pub inline fn callMain() u8 { - switch (@typeInfo(@typeInfo(@TypeOf(root.main)).Fn.return_type.?)) { - .NoReturn => { - root.main(); - }, - .Void => { + const ReturnType = @typeInfo(@TypeOf(root.main)).Fn.return_type.?; + + switch (ReturnType) { + void => { root.main(); return 0; }, - .Int => |info| { - if (info.bits != 8 or info.signedness == .signed) { - @compileError(bad_main_ret); - } + noreturn, u8 => { return root.main(); }, - .ErrorUnion => { + else => { + if (@typeInfo(ReturnType) != .ErrorUnion) @compileError(bad_main_ret); + const result = root.main() catch |err| { std.log.err("{s}", .{@errorName(err)}); if (@errorReturnTrace()) |trace| { @@ -519,18 +517,13 @@ pub inline fn callMain() u8 { } return 1; }; - switch (@typeInfo(@TypeOf(result))) { - .Void => return 0, - .Int => |info| { - if (info.bits != 8 or info.signedness == .signed) { - @compileError(bad_main_ret); - } - return result; - }, + + return switch (@TypeOf(result)) { + void => 0, + u8 => result, else => @compileError(bad_main_ret), - } + }; }, - else => @compileError(bad_main_ret), } } |
