Zig has wrong size of `usize` for my machine

I just downloaded the latest master release 0.14.0-dev.1655+4d09fb491 and I’m trying to rebuild zls, however I get a compiler error:

zig build -Doptimize=ReleaseSafe
/home/joe/.local/lib/zig/std/Build/Fuzz/WebServer.zig:633:9: error: expected type 'usize', found 'u64'
        file_size,
        ^~~~~~~~~
/home/joe/.local/lib/zig/std/Build/Fuzz/WebServer.zig:633:9: note: unsigned 32-bit int cannot represent all possible unsigned 64-bit values
/home/joe/.local/lib/zig/std/posix.zig:4697:13: note: parameter type declared here
    length: usize,
            ^~~~~
referenced by:
    coverageRun: /home/joe/.local/lib/zig/std/Build/Fuzz/WebServer.zig:562:50
    callFn__anon_68434: /home/joe/.local/lib/zig/std/Thread.zig:409:13
    10 reference(s) hidden; use '-freference-trace=12' to see all references

Can anyone else reproduce this to confirm this isn’t my mistake? My zls clone is up to date (I have git pulled) and so is my lib/zig directory.

Oh this is strange. I found this issue:

// on systems where usize != u64
_ = &std.Build.Fuzz.WebServer.coverageRun;
_ = &std.Build.Fuzz.WebServer.run;

So this is because zig thinks my system’s usize is 32 bits, but my machine is x86_64, what’s going on?

$ uname -m
x86_64
#include <stdio.h>
int main() {
    printf("%lu\n", sizeof(void*)); // 8
}
const std = @import("std");
pub fn main() !void {
    std.debug.print("{d}\n", .{@sizeOf(usize)}); // 4
}

If you run zig env, what’s the value of target? And/or what value does the native key from the output of zig targets contain?

Sorry, I think I downloaded the x86 instead of x86_64 release :man_facepalming:. Thanks.

It’s good that you found the issue, but I would expect 32-bit Zig to still be capable of outputting a 64-bit binary, and if your computer is 64-bit, Zig should choose a 64-bit target as default. Maybe this warrants opening an issue.

1 Like

I think this is covered by the issue I linked in my second post: Compiler errors in std pt. 2 · Issue #21094 · ziglang/zig · GitHub.

I don’t think so. That issue just shows that Fuzz.Webserver incorrectly assumes a 64-bit usize. In this case, Zig is choosing the wrong target because of it being 32-bit.

I see. I’m not very familiar with how zig chooses the target but it seems like it defaults to builtin.cpu.arch, which for my installation is x86:

From std.zig.system.resolveTargetQuery:

    // Until https://github.com/ziglang/zig/issues/4592 is implemented (support detecting the
    // native CPU architecture as being different than the current target), we use this:
    const cpu_arch = query.cpu_arch orelse builtin.cpu.arch;

So it seems like that issue already covers this: support detecting the native CPU architecture as being different than the current target · Issue #4592 · ziglang/zig · GitHub

3 Likes