authorgravatar for david@vortan.devDavid Rubin <david@vortan.dev> 2024-01-17 16:46:00-08:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2024-01-18 00:46:00+00:00
log6e5bdb5397b5ec5ff3dd6e3ca6f7f93a9b6bfde9
tree305f2ad46eb3fc5d37dbf3bfb82a9e0ddf64bf71
parentec358d6db5a10989590e11bbbbd3bed9f81cf1f4
signaturebadge-check Signed by PGP key B5690EEEBB952194

add type check to `zirSwitchBlockErrUnion`


2 files changed, 22 insertions(+), 3 deletions(-)

src/Sema.zig+11-3
......@@ -11255,10 +11255,18 @@ fn zirSwitchBlockErrUnion(sema: *Sema, block: *Block, inst: Zir.Inst.Index) Comp
1125511255 defer seen_errors.deinit();
1125611256
1125711257 const operand_ty = sema.typeOf(raw_operand_val);
11258 const operand_err_set_ty = if (extra.data.bits.payload_is_ref)
11259 operand_ty.childType(mod).errorUnionSet(mod)
11258 const operand_err_set = if (extra.data.bits.payload_is_ref)
11259 operand_ty.childType(mod)
1126011260 else
11261 operand_ty.errorUnionSet(mod);
11261 operand_ty;
11262
11263 if (operand_err_set.zigTypeTag(mod) != .ErrorUnion) {
11264 return sema.fail(block, switch_src, "expected error union type, found '{}'", .{
11265 operand_ty.fmt(mod),
11266 });
11267 }
11268
11269 const operand_err_set_ty = operand_err_set.errorUnionSet(mod);
1126211270
1126311271 const block_inst: Air.Inst.Index = @enumFromInt(sema.air_instructions.len);
1126411272 try sema.air_instructions.append(gpa, .{
test/cases/compile_errors/switch_on_non_err_union.zig created+11
......@@ -0,0 +1,11 @@
1pub fn main() void {
2 false catch |err| switch (err) {
3 else => {},
4 };
5}
6
7// error
8// backend=stage2
9// target=native
10//
11// :2:23: error: expected error union type, found 'bool'