compiler: handle switch rewrite review feedback

This commit is contained in:
Matthew Lugg 2026-01-11 14:27:00 +00:00
parent 3a4a7d2ca3
commit 01546e68cd
No known key found for this signature in database
GPG key ID: 3F5B7DCCBF4AF02E
11 changed files with 128 additions and 115 deletions

View file

@ -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>

View file

@ -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:

View file

@ -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,
}
}