zig
zig copied to clipboard
enum coersion not working thinks value is comptime only
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
wrapping the switch
in @as(E, ...)
makes it work but the result location of it being passed as an argument should make that unnecessary
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);
^