Should calling linux syscalls on windows be a compile error?

I have written the following code that I just accidentally ran on windows:

    const scheduler: std.os.linux.SCHED.Mode = @enumFromInt(std.os.linux.sched_getscheduler(0));
    std.log.warn("Scheduler: {s}", .{@tagName(scheduler)});

Which gave me runtime panic:

thread 1440 panic: invalid enum value
C:\repos\gatorcat\src\cli\run.zig:70:48: 0x7ff6ba6535ee in run (gatorcat.exe.obj)
    const scheduler: std.os.linux.SCHED.Mode = @enumFromInt(std.os.linux.sched_getscheduler(0));

Shouldn’t it be a compile error to use the linux syscalls on windows?

Zig zen agrees:

  • Compile errors are better than runtime crashes.

I am not sure what the best way to achieve this is.
Exporting linux from lib/std/os.zig only when native_os == .linux solves the problem. But after that, I am not sure if std.os.linux is still visible in the documentation, unless you build the documentation in linux.

The other approach would be to stick explicit

if (builtin.os.tag != .linux) @compileError("unsupported OS");

checks in functions that require targeting Linux. Putting that in all the syscall0, syscall1, etc functions would probably handle most (if not all?) of it.

Unsure if there are parts of std.os.linux that are intended to be usable when not targeting Linux. I know there are in std.os.windows, e.g.:

2 Likes