#`Random.enumValue()` assertion failing for seemingly no reason

1 messages · Page 1 of 1 (latest)

rancid rampart
#

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)
zenith dirge
#

Instruction is a union(enum) where only one can be active?

hardy lance
#

@typeInfo(Mode) == .Enum, but @typeInfo(Instruction) == .Union which is what you're passing on the second line of random

hardy lance
#

you could use rng.enumValue(std.meta.Tag(Instruction))

rancid rampart
#

so the line number was just wrong ig

hardy lance
#

ah yeah didnt notice that part

rancid rampart
#

that did fix it tho

#

strange how it reported the wrong line