zig
zig copied to clipboard
tuple of enum created with @Type and single field can't be initialized
Zig Version
0.13.0-dev.75+5c9eb4081
Steps to Reproduce and Observed Behavior
/tmp/tmp.zig
test {
// these work fine
// const E = enum { foo };
// const E = enum(u0) { foo };
const E = @Type(.{ .Enum = .{
.tag_type = u0,
.is_exhaustive = true,
.fields = &.{.{ .name = "foo", .value = 0 }},
.decls = &.{},
} });
const s: struct { E } = .{.foo};
_ = s;
}
$ zig test /tmp/tmp.zig
/tmp/tmp.zig:90:32: error: value stored in comptime field does not match the default value of the field
const s: struct { E } = .{.foo};
~^~~
Expected Behavior
This should work considering that the above works with enum { foo } and enum(u0) { foo }.
Here is a larger repro of original code which has the same issue.
test {
const S = struct { foo: u8 };
var s: S = undefined;
const Fe = std.meta.FieldEnum(S);
const name = "foo";
const fe = std.meta.stringToEnum(Fe, name) orelse return error.InvalidField;
switch (fe) {
inline else => |tag| {
@field(s, @tagName(tag)) = 42;
},
}
try std.testing.expectEqual(42, s.foo);
}
Not sure when it was fixed, but both of these tests now compile and pass with 0.14.0-dev.2435+7575f2121. :tada:
this should prolly have test coverage added to https://github.com/ziglang/zig/blob/master/test/behavior/type.zig just to be sure