Skip to content

std.meta.stringToEnum erroneous compile error with std.meta.FieldEnum #17842

Open
@gcoakes

Description

@gcoakes

Zig Version

0.12.0-dev.1369+a09ba455c

Steps to Reproduce and Observed Behavior

When std.meta.FieldEnum is used on a struct with a single field, then there is a compile error produced when attempting to convert a string to that enum. i.e.:

const std = @import("std");

test {
    const S = struct { a: usize };
    const E = std.meta.FieldEnum(S);
    try std.testing.expectEqual(@as(?E, E.a), std.meta.stringToEnum(E, "a"));
}

The error:

$ zig test foo.zig
/home/gcoakes/.local/share/zig-linux-x86_64-0.12.0-dev.1369+a09ba455c/lib/std/comptime_string_map.zig:55:63: error: expected type 'meta.FieldEnum(foo.test_0.S)', found 'u0'
                sorted_kvs[i] = .{ .key = kv.@"0", .value = kv.@"1" };
                                                            ~~^~~~~
/home/gcoakes/.local/share/zig-linux-x86_64-0.12.0-dev.1369+a09ba455c/lib/std/meta.zig:563:12: note: enum declared here
    return @Type(.{
           ^~~~~
/home/gcoakes/.local/share/zig-linux-x86_64-0.12.0-dev.1369+a09ba455c/lib/std/comptime_string_map.zig:14:36: note: called from here
    return ComptimeStringMapWithEql(V, kvs_list, defaultEql);
           ~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
/home/gcoakes/.local/share/zig-linux-x86_64-0.12.0-dev.1369+a09ba455c/lib/std/meta.zig:38:42: note: called from here
        const map = std.ComptimeStringMap(T, kvs);
                    ~~~~~~~~~~~~~~~~~~~~~^~~~~~~~
referenced by:
    test_0: foo.zig:6:68
    remaining reference traces hidden; use '-freference-trace' to see all reference traces

I added a @compileLog in std.meta.stringToEnum logging the kvs_array which shows the value is undefined when it was expected to be E.a:

Compile Log Output:
@as([1]meta.stringToEnum__anon_1077__struct_1083, .{ .{"a", undefined} })

Oddly, this is not the case for a enum created through the normal mechanism:

const std = @import("std");

test {
    // const S = struct { a: usize };
    // const E = std.meta.FieldEnum(S);
    const E = enum { a };
    try std.testing.expectEqual(@as(?E, E.a), std.meta.stringToEnum(E, "a"));
}

Expected Behavior

That first test should compile and pass.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugObserved behavior contradicts documented or intended behaviorfrontendTokenization, parsing, AstGen, Sema, and Liveness.

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions