diff --git a/doc/langref.html.in b/doc/langref.html.in index 442e4ac52b..dbe98ce708 100644 --- a/doc/langref.html.in +++ b/doc/langref.html.in @@ -2893,6 +2893,50 @@ test "switch using enum literals" { } {#code_end#} {#header_close#} + + {#header_open|Non-exhaustive enum#} +

+ A Non-exhaustive enum can be created by adding a trailing '_' field. + It must specify a tag type and cannot consume every enumeration value. +

+

+ {#link|@intToEnum#} on a non-exhaustive enum cannot fail. +

+

+ A switch on a non-exhaustive enum can include a '_' prong with the following properties: +

+

+ {#code_begin|test#} +const std = @import("std"); +const assert = std.debug.assert; + +const Number = enum(u8) { + One, + Two, + Three, + _, +}; + +test "switch on non-exhaustive enum" { + const number = Number.One; + const result = switch (number) { + .One => true, + .Two, + .Three => false, + _ => false, + }; + assert(result); + const is_one = switch (number) { + .One => true, + else => false, + }; + assert(is_one); +} + {#code_end#} + {#header_close#} {#header_close#} {#header_open|union#} diff --git a/test/compile_errors.zig b/test/compile_errors.zig index 9a76319af5..702cc76524 100644 --- a/test/compile_errors.zig +++ b/test/compile_errors.zig @@ -10,9 +10,8 @@ pub fn addCases(cases: *tests.CompileErrorContext) void { \\}; \\const B = enum(u1) { \\ a, - \\ b, \\ _, - \\ c, + \\ b, \\}; \\pub export fn entry() void { \\ _ = A;