sema: Fix overflow when analyzing an inline switch prong range that ends on the maximum value of the switched type

This commit is contained in:
kcbanner 2023-06-23 16:35:44 -04:00 committed by Andrew Kelley
parent 9d66481e3d
commit 5fc5e4fbe0
2 changed files with 14 additions and 0 deletions

View file

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

View file

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