zig icon indicating copy to clipboard operation
zig copied to clipboard

`std.debug.print` print comptime-int zero will fail

Open jinzhongjia opened this issue 7 months ago • 4 comments

Zig Version

0.15.0-dev.669+561ab59ce

Steps to Reproduce and Observed Behavior

Just this code:

const std = @import("std");

const Empty = struct {};

pub fn main() void {
    std.debug.print("{d}\n", .{@sizeOf(Empty)});
}

When I run zig build to compile code, zig will report error:

install
└─ install zig
   └─ zig build-exe zig Debug native 1 errors
C:\Users\jin\scoop\apps\zig-dev\0.15.0-dev.669\lib\std\debug.zig:554:14: error: reached unreachable code
    if (!ok) unreachable; // assertion failure
             ^~~~~~~~~~~
C:\Users\jin\scoop\apps\zig-dev\0.15.0-dev.669\lib\std\math\log2.zig:18:29: note: called at comptime here
            std.debug.assert(x > 0);
            ~~~~~~~~~~~~~~~~^~~~~~~
C:\Users\jin\scoop\apps\zig-dev\0.15.0-dev.669\lib\std\math.zig:783:43: note: called at comptime here
                else => |pos_max| 1 + log2(pos_max),
                                      ~~~~^~~~~~~~~
C:\Users\jin\scoop\apps\zig-dev\0.15.0-dev.669\lib\std\fmt.zig:745:41: note: called at comptime here
        const Int = math.IntFittingRange(value, value);
                    ~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~
referenced by:
    formatValue__anon_26293: C:\Users\jin\scoop\apps\zig-dev\0.15.0-dev.669\lib\std\fmt.zig:729:53
    formatType__anon_26271: C:\Users\jin\scoop\apps\zig-dev\0.15.0-dev.669\lib\std\fmt.zig:520:31
    8 reference(s) hidden; use '-freference-trace=10' to see all references

Expected Behavior

The expected behavior should be successful in compilation

jinzhongjia avatar May 31 '25 04:05 jinzhongjia

Almost certainly an effect of 58d2bd601e3b25e818d6bca2b3267052a12f9917 cc @jacobly0

Rexicon226 avatar May 31 '25 04:05 Rexicon226

Better reduction:

const Empty = struct {};

comptime {
    const X = @sizeOf(Empty);
    @compileLog(X);
    switch (X) {
        0 => {},
        else => @compileError("here"),
    }
}
empty.zig:8:17: error: here
        else => @compileError("here"),
                ^~~~~~~~~~~~~~~~~~~~~

Compile Log Output:
@as(comptime_int, 0)

Rexicon226 avatar May 31 '25 04:05 Rexicon226

This problem has been around for at least a week

jinzhongjia avatar May 31 '25 05:05 jinzhongjia

The core bug has been around for much longer, it seems. I can reproduce the issue on 0.13.0.

Rexicon226 avatar May 31 '25 06:05 Rexicon226