From de207594e40d659eb2d6d9fd3813c66ee5ae4340 Mon Sep 17 00:00:00 2001 From: rpkak Date: Tue, 11 Aug 2026 10:22:28 +0200 Subject: [PATCH] InternPool: don't differentiate between positive and negative zero for ints (#36451) Added a test, which fails on master. Reviewed-on: https://codeberg.org/ziglang/zig/pulls/36451 Reviewed-by: mlugg --- src/InternPool.zig | 6 +++--- test/behavior/math.zig | 7 +++++++ 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/src/InternPool.zig b/src/InternPool.zig index a5c3bdd044920a3fa745249467879385ed20fbec..96af077d99935e56e6290ca47b544b6e6bd076fc 100644 --- a/src/InternPool.zig +++ b/src/InternPool.zig @@ -2672,7 +2672,7 @@ pub const Key = union(enum) { const big_int = int.storage.toBigInt(&buffer); std.hash.autoHash(&hasher, int.ty); - std.hash.autoHash(&hasher, big_int.positive); + std.hash.autoHash(&hasher, big_int.positive or big_int.eqlZero()); for (big_int.limbs) |limb| std.hash.autoHash(&hasher, limb); return hasher.final(); }, @@ -7682,7 +7682,7 @@ pub fn get(ip: *InternPool, gpa: Allocator, io: Io, tid: Zcu.PerThread.Id, key: return gop.put(); } else |_| {} - const tag: Tag = if (big_int.positive) .int_positive else .int_negative; + const tag: Tag = if (big_int.positive or big_int.eqlZero()) .int_positive else .int_negative; try addInt(ip, gpa, io, tid, int.ty, tag, big_int.limbs); }, inline .u64, .i64 => |x| { @@ -7699,7 +7699,7 @@ pub fn get(ip: *InternPool, gpa: Allocator, io: Io, tid: Zcu.PerThread.Id, key: var buf: [2]Limb = undefined; const big_int = BigIntMutable.init(&buf, x).toConst(); - const tag: Tag = if (big_int.positive) .int_positive else .int_negative; + const tag: Tag = if (big_int.positive or big_int.eqlZero()) .int_positive else .int_negative; try addInt(ip, gpa, io, tid, int.ty, tag, big_int.limbs); }, } diff --git a/test/behavior/math.zig b/test/behavior/math.zig index e76ca9850f3ce2bc9ca356910e76407dfae07fb1..aafcd83b04fa770ba9620ce354569877d413f5bd 100644 --- a/test/behavior/math.zig +++ b/test/behavior/math.zig @@ -2691,3 +2691,10 @@ test "i96 operations" { try expect(12345678910111213 == Op_i96.do(.{ .b = .{ .inner = .{ .x = 1234567891011121314 }, .flag = true } })); try expect(1234567891021121314 == Op_i96.do(.{ .c = .{ .inner = .{ .x = 123456789101112131415 }, .flag = true } })); } + +test "zero returned from @mod matches zero in switch" { + try expect(switch (@mod(-2, 2)) { + 0 => true, + else => false, + }); +} -- 2.54.0