Skip to content

std.meta.FieldEnum for type with one field does not work in std.EnumArray #22506

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

Open
Sandalmoth opened this issue Jan 16, 2025 · 1 comment · May be fixed by #23701
Open

std.meta.FieldEnum for type with one field does not work in std.EnumArray #22506

Sandalmoth opened this issue Jan 16, 2025 · 1 comment · May be fixed by #23701
Labels
bug Observed behavior contradicts documented or intended behavior standard library This issue involves writing Zig code for the standard library.
Milestone

Comments

@Sandalmoth
Copy link

Zig Version

0.14.0-dev.2647+5322459a0

Steps to Reproduce and Observed Behavior

Using an enum generated by std.meta.FieldEnum from a type with exactly one field in a std.EnumArray fails to compile if iterator.next() is called.

Example

const std = @import("std");

const A = struct {
    x: u32,
    // y: u32, // it works if A has a second field, or if A has no fields
};

const AEnum = std.meta.FieldEnum(A);

const MyEnum = enum { x };

pub fn main() void {
    // this doesn't compile
    var a = std.EnumArray(AEnum, u32).initFill(123);
    var it = a.iterator();
    while (it.next()) |n| _ = n;

    // this compiles
    var b = std.EnumArray(MyEnum, u32).initFill(123);
    var it2 = b.iterator();
    while (it2.next()) |n| _ = n;
}

fails to compile with the error message

/home/j/Programs/zig-linux-x86_64-0.14.0-dev.2647+5322459a0/lib/std/enums.zig:1158:39: error: value stored in comptime field does not match the default value of the field
                        .key = Indexer.keyForIndex(index),
                        ~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~
/home/j/Programs/zig-linux-x86_64-0.14.0-dev.2647+5322459a0/lib/std/enums.zig:1141:13: note: default value set here
            key: Key,

Expected Behavior

I would expect it to compile and behave like an enum implemented by hand.

@Sandalmoth Sandalmoth added the bug Observed behavior contradicts documented or intended behavior label Jan 16, 2025
@andrewrk andrewrk added the standard library This issue involves writing Zig code for the standard library. label Jan 25, 2025
@andrewrk andrewrk added this to the 0.15.0 milestone Jan 25, 2025
@poypoyan
Copy link
Contributor

poypoyan commented Mar 14, 2025

This looks like a variant of #19985 that doesn't compile. An example:

const std = @import("std");

// 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 Entry = struct {
    k: E,
};

pub fn main() void {
    const s = Entry{ .k = E.foo };
    _ = s;
}

When setting .tag_type to u1, the above compiles.
I'll investigate this further.

@poypoyan poypoyan linked a pull request Apr 27, 2025 that will close this issue
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 standard library This issue involves writing Zig code for the standard library.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants