Skip to content

Compile error when using EnumSet with non-exhaustive enum #19418

Closed
@sjb3d

Description

@sjb3d

Zig Version

0.12.0-dev.3433+640acf862

Steps to Reproduce and Observed Behavior

EnumIndexer supports non-exhaustive enums, so I was expecting that EnumSet (and EnumMap) would support this too.

This can be useful to implement bitfields with sparse usage, which are common in external libraries. For example, using an enum backed by u5, the EnumSet becomes a 32-bit bitfield.

The compile error can be reproduced using this:

const std = @import("std");

const BitIndices = enum(u5) {
    a = 0,
    b = 1,
    c = 4,
    _,
};

const BitField = std.enums.EnumSet(BitIndices);

export fn example() void {
    var flags = BitField.init(.{ .a = true, .b = true });

    flags.insert(.c);
    flags.remove(.a);

    std.debug.assert(flags.contains(.b));
}

Which produces an error from EnumSet.init as follows:

/opt/compiler-explorer/zig-master/lib/std/enums.zig:268:29: error: no field with value '@enumFromInt(2)' in enum 'BitIndices'

Expected Behavior

Successful compile and test.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugObserved behavior contradicts documented or intended behavior

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions