zig icon indicating copy to clipboard operation
zig copied to clipboard

std.enums.EnumFieldStruct can't accept `null` as default value

Open Jack-Ji opened this issue 1 year ago • 2 comments

Zig Version

0.13.0-dev.211+6a65561e3

Steps to Reproduce and Observed Behavior

const std = @import("std");

const E = enum {
    a,
    b,
};

const ES = std.enums.EnumFieldStruct(E, ?u8, null);

pub fn main() void {
    const e = ES{ .a = 0 };
    _ = e;
}

zig build-exe gives:

enum_field_struct.zig:11:17: error: missing struct field: b
    const e = ES{ .a = 0 };
              ~~^~~~~~~~~~
.zvm/master/lib/std/enums.zig:27:12: note: struct 'enums.EnumFieldStruct(enum_field_struct.E,?u8,null)' declared here
    return @Type(.{ .Struct = .{
           ^~~~~
referenced by:
    callMain: .zvm/master/lib/std/start.zig:514:17
    callMainWithArgs: .zvm/master/lib/std/start.zig:482:12
    remaining reference traces hidden; use '-freference-trace' to see all reference traces

Expected Behavior

Compile successfully.

Jack-Ji avatar May 18 '24 13:05 Jack-Ji

Passing null here means you don't want a default value. You need to pass @as(?u8, null).

Lzard avatar May 18 '24 14:05 Lzard

IMO, using ?u8 as value type sort of means null is default value rather than no default at all. std.EnumMap is using the assumption in it's init/initFullWith methods either.

Jack-Ji avatar May 18 '24 14:05 Jack-Ji