zig icon indicating copy to clipboard operation
zig copied to clipboard

enum coersion not working thinks value is comptime only

Open nektro opened this issue 1 year ago • 2 comments

Zig Version

0.10.0-dev.4644+e036cc48d

Steps to Reproduce and Observed Behavior

const E = enum(u8) {
    a,
    b,
    c,
    d,
};

const U = union(enum) {
    a: u32,
    b: u16,
};

pub fn main() void {
    var a: U = undefined;
    foo(E, switch (a) {
        .a => .a,
        .b => .b,
    });
}

fn foo(comptime T: type, the: T) void {
    _ = the;
}
test.zig:15:12: error: value with comptime-only type '@TypeOf(.enum_literal)' depends on runtime control flow
    foo(E, switch (a) {
           ^~~~~~
test.zig:15:20: note: runtime control flow here
    foo(E, switch (a) {
                   ^

Expected Behavior

successful run

nektro avatar Oct 30 '22 06:10 nektro

wrapping the switch in @as(E, ...) makes it work but the result location of it being passed as an argument should make that unnecessary

nektro avatar Oct 30 '22 06:10 nektro

Moving the reproduction from #14196 to here:

fn foo(comptime T: type, val: T) void {
    _ = val;
}

test {
    var b: bool = true;
    var i: u64 = 0;
    while (i < 10) : (i += 1) {
        foo(u16, if (b) 0 else 1);
    }
}
strange.zig:9:18: error: value with comptime-only type 'comptime_int' depends on runtime control flow
        foo(u16, if (b) 0 else 1);
                 ^~~~~~~~~~~~~~~
strange.zig:8:14: note: runtime control flow here
    while (i < 10) : (i += 1) {
           ~~^~~~

and an even simpler one:

fn foo(comptime T: type, val: T) void {
    _ = val;
}

test {
    var b: bool = true;
    foo(u16, if (b) 0 else 1);
}

The simpler one gives a more understandable compile error:

strange.zig:7:14: error: value with comptime-only type 'comptime_int' depends on runtime control flow
    foo(u16, if (b) 0 else 1);
             ^~~~~~~~~~~~~~~
strange.zig:7:18: note: runtime control flow here
    foo(u16, if (b) 0 else 1);
                 ^

squeek502 avatar Jan 04 '23 17:01 squeek502