mirror of
https://codeberg.org/ziglang/zig.git
synced 2026-03-08 01:24:49 +01:00
compiler: handle switch rewrite review feedback
This commit is contained in:
parent
3a4a7d2ca3
commit
01546e68cd
11 changed files with 128 additions and 115 deletions
|
|
@ -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,
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue