Rng bugged?

The following code crashes the compiler with a SIGSEGV

const std = @import("std");
var rnd = std.rand.DefaultPrng.init(0);
var r = rnd.random().int(u32);

pub fn main() !void {
    const stdout = std.io.getStdOut().writer();
    try stdout.print("{d}\n", .{r});
}

But works well if the “var r=…” line is put inside the main. Anybody has an idea?

Interesting, in debug mode it only produces error: d0... which isn’t much help.

Which version of Zig?

On this install I’m still on 0.9.1 and it doesn’t crash, but warns me that the evaluation fails at compile time:

$ zig run 487.zig
/usr/lib/zig/std/rand/Xoshiro256.zig:26:40: error: unable to evaluate constant expression
    const r = math.rotl(u64, self.s[0] +% self.s[3], 23) +% self.s[0];
                                       ^
/usr/lib/zig/std/rand/Xoshiro256.zig:84:26: note: called from here
        var n = self.next();
                         ^
/usr/lib/zig/std/rand.zig:44:23: note: called from here
                fillFn(self, buf);
                      ^
/usr/lib/zig/std/rand.zig:56:17: note: called from here
        r.fillFn(r.ptr, buf);
                ^
/usr/lib/zig/std/rand.zig:84:16: note: called from here
        r.bytes(rand_bytes[0..]);
               ^
./487.zig:4:27: note: called from here
const r = rnd.random().int(u32);
                          ^
./487.zig:4:27: note: called from here
const r = rnd.random().int(u32);
                          ^
~/ZIG> more test2.zig
const std = @import("std");
var rnd = std.rand.DefaultPrng.init(0);
var r = rnd.random().int(u32);

pub fn main() !void {
    const stdout = std.io.getStdOut().writer();
    try stdout.print("{d}\n", .{r});
}

~/ZIG> zig build-exe test2.zig
fish: Job 2, 'zig build-exe test2.zig' terminated by signal SIGSEGV (Address boundary error)

~/ZIG [SIGSEGV]> zig version
0.10.0

Congratulations, you seem to have found a regression. :slight_smile:

Mind opening a bug report? :innocent:

Already did… :innocent:
But I think I just found another… :hot_face: