Skip to content
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

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

Open
Jack-Ji opened this issue May 18, 2024 · 2 comments
Open

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

Jack-Ji opened this issue May 18, 2024 · 2 comments
Labels
bug Observed behavior contradicts documented or intended behavior

Comments

@Jack-Ji
Copy link
Contributor

Jack-Ji commented May 18, 2024

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 Jack-Ji added the bug Observed behavior contradicts documented or intended behavior label May 18, 2024
@Jack-Ji Jack-Ji changed the title std.enums.EnumFieldStruct can't accept with null as default value std.enums.EnumFieldStruct can't accept null as default value May 18, 2024
@Lzard
Copy link

Lzard commented May 18, 2024

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

@Jack-Ji
Copy link
Contributor Author

Jack-Ji commented May 18, 2024

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.

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