mirror of
https://codeberg.org/ziglang/zig.git
synced 2026-04-26 13:01:34 +03:00
compiler: handle switch rewrite review feedback
This commit is contained in:
+1
-1
@@ -2595,7 +2595,7 @@ or
|
||||
|
||||
{#header_close#}
|
||||
|
||||
{#header_open|Switching on errors#}
|
||||
{#header_open|Switching on Errors#}
|
||||
<p>
|
||||
When switching on errors, some special cases are allowed to simplify generic programming patterns:
|
||||
</p>
|
||||
|
||||
@@ -12,7 +12,10 @@ test "unreachable else prong" {
|
||||
switch (openFile0()) {
|
||||
error.AccessDenied, error.FileNotFound => |e| return e,
|
||||
error.OutOfMemory => {},
|
||||
else => unreachable, // technically unreachable, but will still compile!
|
||||
// 'openFile0' cannot return any more errors, so an 'else' prong would be
|
||||
// statically known to be unreachable. Nonetheless, in this case, adding
|
||||
// one does not raise an "unreachable else prong" compile error:
|
||||
else => unreachable,
|
||||
}
|
||||
|
||||
// Allowed unreachable else prongs are:
|
||||
|
||||
@@ -20,7 +20,10 @@ test "switch on tagged union" {
|
||||
}
|
||||
|
||||
switch (c) {
|
||||
.ok => |_, tag| try expect(tag == .ok),
|
||||
.ok => |_, tag| {
|
||||
// Because we're in the '.ok' prong, 'tag' is compile-time known to be '.ok':
|
||||
comptime std.debug.assert(tag == .ok);
|
||||
},
|
||||
.not_ok => unreachable,
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user