From 5fc5e4fbe04ccbe5ea37b07c1153a7c5bd2b4346 Mon Sep 17 00:00:00 2001 From: kcbanner Date: Fri, 23 Jun 2023 16:35:44 -0400 Subject: [PATCH] sema: Fix overflow when analyzing an inline switch prong range that ends on the maximum value of the switched type --- src/Sema.zig | 2 ++ test/behavior/switch.zig | 12 ++++++++++++ 2 files changed, 14 insertions(+) diff --git a/src/Sema.zig b/src/Sema.zig index ddc02c1ece..0277dab4ee 100644 --- a/src/Sema.zig +++ b/src/Sema.zig @@ -11646,6 +11646,8 @@ fn zirSwitchBlock(sema: *Sema, block: *Block, inst: Zir.Inst.Index, operand_is_r cases_extra.appendAssumeCapacity(@intCast(u32, case_block.instructions.items.len)); cases_extra.appendAssumeCapacity(@intFromEnum(item_ref)); cases_extra.appendSliceAssumeCapacity(case_block.instructions.items); + + if (item.compareScalar(.eq, item_last, operand_ty, mod)) break; } } diff --git a/test/behavior/switch.zig b/test/behavior/switch.zig index f9fb9c7659..bcbfc81ed4 100644 --- a/test/behavior/switch.zig +++ b/test/behavior/switch.zig @@ -785,3 +785,15 @@ test "switch pointer capture peer type resolution" { try expectEqual(U{ .a = 111 }, ua); try expectEqual(U{ .b = 222 }, ub); } + +test "inline switch range that includes the maximum value of the switched type" { + if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest; + + const inputs: [3]u8 = .{ 0, 254, 255 }; + for (inputs) |input| { + switch (input) { + inline 254...255 => |val| try expectEqual(input, val), + else => |val| try expectEqual(input, val), + } + } +}