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 {
497497const bad_main_ret = "expected return type of main to be 'void', '!void', 'noreturn', 'u8', or '!u8'";
498498
499499pub inline fn callMain() u8 {
500 switch (@typeInfo(@typeInfo(@TypeOf(root.main)).Fn.return_type.?)) {
501 .NoReturn => {
502 root.main();
503 },
504 .Void => {
500 const ReturnType = @typeInfo(@TypeOf(root.main)).Fn.return_type.?;
501
502 switch (ReturnType) {
503 void => {
505504 root.main();
506505 return 0;
507506 },
508 .Int => |info| {
509 if (info.bits != 8 or info.signedness == .signed) {
510 @compileError(bad_main_ret);
511 }
507 noreturn, u8 => {
512508 return root.main();
513509 },
514 .ErrorUnion => {
510 else => {
511 if (@typeInfo(ReturnType) != .ErrorUnion) @compileError(bad_main_ret);
512
515513 const result = root.main() catch |err| {
516514 std.log.err("{s}", .{@errorName(err)});
517515 if (@errorReturnTrace()) |trace| {
......@@ -519,18 +517,13 @@ pub inline fn callMain() u8 {
519517 }
520518 return 1;
521519 };
522 switch (@typeInfo(@TypeOf(result))) {
523 .Void => return 0,
524 .Int => |info| {
525 if (info.bits != 8 or info.signedness == .signed) {
526 @compileError(bad_main_ret);
527 }
528 return result;
529 },
520
521 return switch (@TypeOf(result)) {
522 void => 0,
523 u8 => result,
530524 else => @compileError(bad_main_ret),
531 }
525 };
532526 },
533 else => @compileError(bad_main_ret),
534527 }
535528}
536529