Search code examples
zig

zig: if statement in print function produces wrong output


I have this code:

const std = @import("std");
const print = std.debug.print;
pub fn main() void {
    var n: u8 = 1;
    print("{s}\n", .{if (n == 0) "0" else "1"});
}

But it does not do what I want. The output is "0".

Could anyone tell me what happend? Thanks a lot.


Solution

  • Zig is not yet stable and in special stage1 (the current bootstrapping compiler) will not be, as the intention is to delete it and replace the bootstrapping compiler with generated c code one from stage2 (as the new bootstrapping compiler).

    In contrast to that will stage2 (currently in development) handle these things properly.

    This is a known miscompilation.

    Please first search upstream, under https://github.com/ziglang/zig/issues with labels stage1 and miscompilation.