From 0a0f84443fcb987fb6652ea4edaf8b5581cf5b66 Mon Sep 17 00:00:00 2001 From: David Rubin Date: Tue, 16 Jan 2024 14:30:20 -0800 Subject: [PATCH 1/2] add type check to `zirSwitchBlockErrUnion` --- src/Sema.zig | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/src/Sema.zig b/src/Sema.zig index 941c91fcd0bd..fcd7f2a1d8cc 100644 --- a/src/Sema.zig +++ b/src/Sema.zig @@ -11255,10 +11255,18 @@ fn zirSwitchBlockErrUnion(sema: *Sema, block: *Block, inst: Zir.Inst.Index) Comp defer seen_errors.deinit(); const operand_ty = sema.typeOf(raw_operand_val); - const operand_err_set_ty = if (extra.data.bits.payload_is_ref) - operand_ty.childType(mod).errorUnionSet(mod) + const operand_err_set = if (extra.data.bits.payload_is_ref) + operand_ty.childType(mod) else - operand_ty.errorUnionSet(mod); + operand_ty; + + if (operand_err_set.zigTypeTag(mod) != .ErrorUnion) { + return sema.fail(block, switch_src, "expected error union type, found '{}'", .{ + operand_ty.fmt(mod), + }); + } + + const operand_err_set_ty = operand_err_set.errorUnionSet(mod); const block_inst: Air.Inst.Index = @enumFromInt(sema.air_instructions.len); try sema.air_instructions.append(gpa, .{ From 4e81c74b01c0b0bf15ac8bd576f2c6d01488311b Mon Sep 17 00:00:00 2001 From: David Rubin Date: Tue, 16 Jan 2024 19:58:15 -0800 Subject: [PATCH 2/2] add test case --- test/cases/compile_errors/switch_on_non_err_union.zig | 11 +++++++++++ 1 file changed, 11 insertions(+) create mode 100644 test/cases/compile_errors/switch_on_non_err_union.zig diff --git a/test/cases/compile_errors/switch_on_non_err_union.zig b/test/cases/compile_errors/switch_on_non_err_union.zig new file mode 100644 index 000000000000..87624b21dc27 --- /dev/null +++ b/test/cases/compile_errors/switch_on_non_err_union.zig @@ -0,0 +1,11 @@ +pub fn main() void { + false catch |err| switch (err) { + else => {}, + }; +} + +// error +// backend=stage2 +// target=native +// +// :2:23: error: expected error union type, found 'bool'