zig icon indicating copy to clipboard operation
zig copied to clipboard

`@tagName()` fails with enum backed by signed int

Open travisstaloch opened this issue 2 years ago • 2 comments

Zig Version

0.11.0-dev.3704+729a051e9

Steps to Reproduce and Observed Behavior

it seems that using @tagName() with an i32 backed enum fails while it succeeds with a u32.

// /tmp/tmp.zig
pub const Ok = enum(u32) { a, b, c };
pub const Nok = enum(i32) { a, b, c };

test {
    _ = @tagName(Ok.a);
    _ = @tagName(Nok.a);
}
$ zig version
0.11.0-dev.3704+729a051e9

$ zig test /tmp/tmp.zig
/tmp/tmp.zig:6:9: error: no field with value '@intToEnum(tmp.Nok, 0)' in enum 'Nok'
    _ = @tagName(Nok.a);
        ^~~~~~~~~~~~~~~
/tmp/tmp.zig:2:17: note: declared here
pub const Nok = enum(i32) { a, b, c };

Expected Behavior

test should pass whether the enum is backed by a signed int or an unsigned int

travisstaloch avatar Jun 19 '23 15:06 travisstaloch

Note: this regressed sometime in the last week or so. It was working prior to then. I discovered because of this ci failure: https://github.com/travisstaloch/protobuf-zig/actions/runs/5300787367/jobs/9594532718

travisstaloch avatar Jun 19 '23 15:06 travisstaloch

maybe a more direct reproduction:

pub const Ok = enum(u32) { a, b, c };
pub const Nok = enum(i32) { a, b, c };

test {
    _ = @intToEnum(Ok, 0);
    _ = @intToEnum(Nok, 0);
}
$ zig test /tmp/tmp.zig
/tmp/tmp.zig:10:9: error: enum 'tmp.Nok' has no tag with value '0'
    _ = @intToEnum(Nok, 0);
        ^~~~~~~~~~~~~~~~~~
/tmp/tmp.zig:3:17: note: enum declared here
pub const Nok = enum(i32) { a, b, c };
                ^~~~~~~~~~~~~~~~~~~~~

travisstaloch avatar Jun 19 '23 15:06 travisstaloch