Skip to content

std.enums.EnumArray.initDefault and std.enums.EnumMap.initFullWithDefault fail with non-exhaustive enums #22104

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
DanikVitek opened this issue Nov 29, 2024 · 2 comments
Labels
bug Observed behavior contradicts documented or intended behavior

Comments

@DanikVitek
Copy link

DanikVitek commented Nov 29, 2024

Zig Version

0.13.0

Steps to Reproduce and Observed Behavior

  1. Declare a non-exhaustive sparse enum
  2. Try initializing the EnumArray via init or initDefault / EnumMap via initFullWith or initFullWithDefault
  3. -> compilation error similar to "no field with value '@enumFromInt(1)' in enum 'main.E'"

EnumArray code example:

const std = @import("std");

pub fn main() !void {
    const E = enum(u3) {
        A = 0,
        B = 1, // 2,
        C = 2, // 4,
        _,
    };

    const EA = std.EnumArray(E, []const u8).initDefault("default", .{
        .A = "A",
        .B = "B",
        .C = "C",
    });

    std.debug.print("{s}\n", .{EA.get(E.A)});
}

Error message:

C:\Program Files\zigup\zig\0.13.0\files\lib\std\enums.zig:1094:29: error: no field with value '@enumFromInt(3)' in enum 'main.E'
                const tag = @tagName(key);
                            ^~~~~~~~~~~~~
src\main.zig:4:15: note: declared here
    const E = enum(u3) {
              ^~~~
referenced by:
    main: src\main.zig:11:56
    callMain: C:\Program Files\zigup\zig\0.13.0\files\lib\std\start.zig:524:32

EnumMap code example:

const std = @import("std");

pub fn main() !void {
    const E = enum(u3) {
        A = 0,
        B = 1,
        C = 2,
        _,
    };

    const EM = std.EnumMap(E, []const u8).initFullWithDefault("default", .{
        .A = "A",
        .B = "B",
        .C = "C",
    });

    std.debug.print("{?s}\n", .{EM.get(E.A)});
}

Error message:

C:\Program Files\zigup\zig\0.13.0\files\lib\std\enums.zig:507:29: error: no field with value '@enumFromInt(3)' in enum 'main.E'
                const tag = @tagName(key);
                            ^~~~~~~~~~~~~
src\main.zig:4:15: note: declared here
    const E = enum(u3) {
              ^~~~
referenced by:
    main: src\main.zig:11:62
    callMain: C:\Program Files\zigup\zig\0.13.0\files\lib\std\start.zig:524:32

Expected Behavior

Successful compilation with valid behavior

@DanikVitek DanikVitek added the bug Observed behavior contradicts documented or intended behavior label Nov 29, 2024
@DanikVitek DanikVitek changed the title std.enums.EnumArray.initDefault fails with sparse non-exhaustive enums std.enums.EnumArray.initDefault fails with non-exhaustive enums Nov 29, 2024
@DanikVitek DanikVitek changed the title std.enums.EnumArray.initDefault fails with non-exhaustive enums std.enums.EnumArray.initDefault and std.enums.EnumMap.initFullWithDefault fails with non-exhaustive enums Nov 29, 2024
@DanikVitek DanikVitek changed the title std.enums.EnumArray.initDefault and std.enums.EnumMap.initFullWithDefault fails with non-exhaustive enums std.enums.EnumArray.initDefault and std.enums.EnumMap.initFullWithDefault fail with non-exhaustive enums Nov 29, 2024
@DanikVitek
Copy link
Author

I might just not fully understand the application to the non-exhaustive enums, as they are expected to be valid for the whole range of the backing integer

@rohlem
Copy link
Contributor

rohlem commented Dec 1, 2024

Seems like this was last fixed for EnumMap.init in #19309 / 8a7b04f , but the methods you mentioned are still missing the check (were probably overlooked).
If you want to experiment with master, you can find it on the ziglang download page.
I assume a PR for this would be accepted, it you want to give it a try.
(IMO it would be nicer to update the single loop instead of having a branch to two different ones, f.e. using std.enums.tagName instead of @tagName, but that's up to the implementer.)

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