mach: better compile error for missing fields in App. (#548)

Stage2 Zig ends up coercing structs without any fields into consts.
The current error given is not entirely clear to the cause or how
to fix the issue for users.

Related to https://github.com/ziglang/zig/issues/12275
This commit is contained in:
Jeremia 2022-09-16 18:36:15 -05:00 committed by GitHub
parent 8b50f076a6
commit 7ed8829349
Failed to generate hash of commit

View file

@ -6,6 +6,19 @@ pub fn checkApplication(comptime app_pkg: type) void {
} }
const App = app_pkg.App; const App = app_pkg.App;
// If App has no fields, it gets interpretted as '*const App' when it should be '*App'
// This gives a more useful compiler error.
switch(@typeInfo(App)) {
.Struct => |app| {
if(app.fields.len == 0) {
@compileError("App must contain fields. Example: '_unused: i32,'");
}
},
else => {
@compileError("App must be a struct type. Found:" ++ @typeName(App));
}
}
if (@hasDecl(App, "init")) { if (@hasDecl(App, "init")) {
const InitFn = @TypeOf(@field(App, "init")); const InitFn = @TypeOf(@field(App, "init"));
if (InitFn != fn (app: *App, core: *Core) @typeInfo(@typeInfo(InitFn).Fn.return_type.?).ErrorUnion.error_set!void) if (InitFn != fn (app: *App, core: *Core) @typeInfo(@typeInfo(InitFn).Fn.return_type.?).ErrorUnion.error_set!void)