zig icon indicating copy to clipboard operation
zig copied to clipboard

tuple of enum created with @Type and single field can't be initialized

Open travisstaloch opened this issue 1 year ago • 1 comments

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 }.

travisstaloch avatar May 16 '24 19:05 travisstaloch

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);
}

travisstaloch avatar May 16 '24 21:05 travisstaloch

Not sure when it was fixed, but both of these tests now compile and pass with 0.14.0-dev.2435+7575f2121. :tada:

travisstaloch avatar Dec 25 '24 01:12 travisstaloch

this should prolly have test coverage added to https://github.com/ziglang/zig/blob/master/test/behavior/type.zig just to be sure

nektro avatar Dec 25 '24 03:12 nektro