Skip to content

Inferred enum types produce error: values of type '(enum literal)' must be comptime known #10294

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

Closed
hazeycode opened this issue Dec 7, 2021 · 1 comment
Labels
bug Observed behavior contradicts documented or intended behavior

Comments

@hazeycode
Copy link
Contributor

Zig Version

0.8.1

Steps to Reproduce

not working:

test "char-enum mapping" {
    const Foo = enum(u8) { A, B, C, Unknown };

    const val: Foo = if (std.meta.intToEnum(
        enum(u8) {
            a = 'a',
            b = 'b',
            c = 'c',
        },
        'x',
    )) |e| switch (e) {
        .a => .A,
        .b => .B,
        .c => .C,
    } else |_| .Unknown;

    try std.testing.expectEqual(Foo.Unknown, val);
}

working:

test "char-enum mapping" {
    const Foo = enum(u8) { A, B, C, Unknown };

    const val: Foo = if (std.meta.intToEnum(
        enum(u8) {
            a = 'a',
            b = 'b',
            c = 'c',
        },
        'x',
    )) |e| switch (e) {
        .a => Foo.A,
        .b => Foo.B,
        .c => Foo.C,
    } else |_| Foo.Unknown;

    try std.testing.expectEqual(Foo.Unknown, val);
}

Expected Behavior

I wouldn't expect to have to specify the enum type in each switch case here. It should be inferred from the type of val.

Actual Behavior

produces a confusing compiler error error: values of type '(enum literal)' must be comptime known

@hazeycode hazeycode added the bug Observed behavior contradicts documented or intended behavior label Dec 7, 2021
@Vexu
Copy link
Member

Vexu commented Dec 7, 2021

Duplicate of #3750 etc. Fixed in stage2.

@Vexu Vexu closed this as completed Dec 7, 2021
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
Projects
None yet
Development

No branches or pull requests

2 participants