start: refactor callMain return type checking

This commit is contained in:
gooncreeper
2024-07-15 09:29:07 +00:00
committed by GitHub
parent 5675553aed
commit 9002977051

View File

@@ -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),
}
}