Zig: @tagName handles specified enum values incorrectly

Created on 3 May 2018  路  2Comments  路  Source: ziglang/zig

const std = @import("std");

const Foo = enum {
    A = 1,
    B = 0,
};
test "aoeu" {
    var b = Foo.B;
    std.debug.assert(std.mem.eql(u8, @tagName(b), "B")); // assertion failure
}

If you run the test at comptime it (correctly) passes.

This asks the question, what should we do for sparse values? If A = 0 and B = 999999 then it does not really make sense to do a table lookup for the tag name. But if A = 0 and B = 2 then it probably does. I suppose a heuristic is needed to determine whether a table or sparse set (with if-else checks), or some combination of both is best for the implementation of @tagName.

bug

All 2 comments

Why not implement it as a switch and let LLVM optimize it?

It seems clang 6.0.0 optimizes switches and nested ifs into table lookups.

Was this page helpful?
0 / 5 - 0 ratings