From 7ed8829349937dac0f7abd0f6f96d8b68476b0d8 Mon Sep 17 00:00:00 2001 From: Jeremia Date: Fri, 16 Sep 2022 18:36:15 -0500 Subject: [PATCH] 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 --- src/platform/common.zig | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/src/platform/common.zig b/src/platform/common.zig index 116b4ff8..d5fbfd7c 100644 --- a/src/platform/common.zig +++ b/src/platform/common.zig @@ -6,6 +6,19 @@ pub fn checkApplication(comptime app_pkg: type) void { } 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")) { const InitFn = @TypeOf(@field(App, "init")); if (InitFn != fn (app: *App, core: *Core) @typeInfo(@typeInfo(InitFn).Fn.return_type.?).ErrorUnion.error_set!void)