authorgravatar for goon.pri.low@gmail.comKendall Condon <goon.pri.low@gmail.com> 2024-07-15 09:29:07+00:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2024-07-15 12:29:07+03:00
log9002977051b810ec023589c7e89283acb3ca9fbf
treeac133cc279368631d46e6a87cd76d6c4a7aeb2f3
parent5675553aedf72c3cf6135025fe216fb92f09f3fa
signaturebadge-check Signed by PGP key B5690EEEBB952194

start: refactor callMain return type checking


1 files changed, 13 insertions(+), 20 deletions(-)

lib/std/start.zig+13-20
...@@ -497,21 +497,19 @@ fn mainWithoutEnv(c_argc: c_int, c_argv: [*][*:0]c_char) callconv(.C) c_int {...@@ -497,21 +497,19 @@ fn mainWithoutEnv(c_argc: c_int, c_argv: [*][*:0]c_char) callconv(.C) c_int {
497const bad_main_ret = "expected return type of main to be 'void', '!void', 'noreturn', 'u8', or '!u8'";497const bad_main_ret = "expected return type of main to be 'void', '!void', 'noreturn', 'u8', or '!u8'";
498498
499pub inline fn callMain() u8 {499pub inline fn callMain() u8 {
500 switch (@typeInfo(@typeInfo(@TypeOf(root.main)).Fn.return_type.?)) {500 const ReturnType = @typeInfo(@TypeOf(root.main)).Fn.return_type.?;
501 .NoReturn => {501
502 root.main();502 switch (ReturnType) {
503 },503 void => {
504 .Void => {
505 root.main();504 root.main();
506 return 0;505 return 0;
507 },506 },
508 .Int => |info| {507 noreturn, u8 => {
509 if (info.bits != 8 or info.signedness == .signed) {
510 @compileError(bad_main_ret);
511 }
512 return root.main();508 return root.main();
513 },509 },
514 .ErrorUnion => {510 else => {
511 if (@typeInfo(ReturnType) != .ErrorUnion) @compileError(bad_main_ret);
512
515 const result = root.main() catch |err| {513 const result = root.main() catch |err| {
516 std.log.err("{s}", .{@errorName(err)});514 std.log.err("{s}", .{@errorName(err)});
517 if (@errorReturnTrace()) |trace| {515 if (@errorReturnTrace()) |trace| {
...@@ -519,18 +517,13 @@ pub inline fn callMain() u8 {...@@ -519,18 +517,13 @@ pub inline fn callMain() u8 {
519 }517 }
520 return 1;518 return 1;
521 };519 };
522 switch (@typeInfo(@TypeOf(result))) {520
523 .Void => return 0,521 return switch (@TypeOf(result)) {
524 .Int => |info| {522 void => 0,
525 if (info.bits != 8 or info.signedness == .signed) {523 u8 => result,
526 @compileError(bad_main_ret);
527 }
528 return result;
529 },
530 else => @compileError(bad_main_ret),524 else => @compileError(bad_main_ret),
531 }525 };
532 },526 },
533 else => @compileError(bad_main_ret),
534 }527 }
535}528}
536529