Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
travisstaloch opened this issue May 16, 2024 · 3 comments
Closed
Labels
bug Observed behavior contradicts documented or intended behavior frontend Tokenization, parsing, AstGen, Sema, and Liveness.
Milestone

Comments

@travisstaloch
Copy link
Contributor

travisstaloch commented May 16, 2024

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 travisstaloch added the bug Observed behavior contradicts documented or intended behavior label May 16, 2024
@travisstaloch
Copy link
Contributor Author

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

@Vexu Vexu added the frontend Tokenization, parsing, AstGen, Sema, and Liveness. label May 18, 2024
@Vexu Vexu added this to the 0.13.0 milestone May 18, 2024
@travisstaloch
Copy link
Contributor Author

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

@nektro
Copy link
Contributor

nektro commented Dec 25, 2024

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Observed behavior contradicts documented or intended behavior frontend Tokenization, parsing, AstGen, Sema, and Liveness.
Projects
None yet
Development

No branches or pull requests

3 participants