I'm trying to use Random.enumValue(), but for some reason its assertion fails when I pass in my enum type. I tried compiler logging to make sure that it is an enum type and it is so I have no idea why my code seems to return true for the same condition that the standard library returns false (and errors).
const Mode = enum { reg, imm };
pub const Instruction = union(enum) {
mov: struct {
mode: Mode,
reg: u8,
val: u8
},
in: u8,
out: struct {
mode: Mode,
val: u8
},
jmp: isize,
// returns a random instruction
pub fn random(rng: Random) Instruction {
@compileLog(@typeInfo(Mode) == .Enum); // this returns true!
const t = rng.enumValue(Instruction);
return switch(t) {
.mov => .{ .mov = .{ .mode = rng.enumValue(Mode), .reg = rng.int(u8), .val = rng.int(u8) } },
.in => .{ .in = rng.int(u8) },
.out => .{ .out = .{ .mode = rng.enumValue(Mode), .val = rng.int(u8) } }, // this is line 30
.jmp => .{ .jmp = rng.intRangeAtMost(isize, -5, 5) }
};
}
};
here's the error:
/home/z/things/compilers/zig/lib/std/debug.zig:342:14: error: reached unreachable code
if (!ok) unreachable; // assertion failure
^~~~~~~~~~~
/home/z/things/compilers/zig/lib/std/rand.zig:84:24: note: called from here
comptime assert(@typeInfo(EnumType) == .Enum);
~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
referenced by:
enumValue: /home/z/things/compilers/zig/lib/std/rand.zig:70:36
random: src/program.zig:30:36
remaining reference traces hidden; use '-freference-trace' to see all reference traces
Compile Log Output:
@as(bool, true)