std.hash_map: fix removeByPtr with array key type

Resolves: https://codeberg.org/ziglang/zig/issues/32086
This commit is contained in:
Matthew Lugg
2026-05-06 10:04:54 +01:00
committed by mlugg
parent 52c5223bca
commit 45ecd6a996
+20 -1
View File
@@ -1271,7 +1271,7 @@ pub fn HashMapUnmanaged(
// map, which is assumed to exist as key_ptr must be valid. This
// item must be at index 0.
const idx = if (@sizeOf(K) > 0)
(key_ptr - self.keys())
@as([*]K, @ptrCast(key_ptr)) - self.keys()
else
0;
@@ -2175,3 +2175,22 @@ test "rehash" {
}
}
}
test "removeByPtr, key is array" {
const gpa = testing.allocator;
var map: AutoHashMapUnmanaged([2]u32, u32) = .empty;
defer map.deinit(gpa);
const key: [2]u32 = .{ 1, 2 };
try map.put(gpa, key, 3);
try expectEqual(1, map.count());
try expectEqual(3, map.get(key));
const key_ptr = map.getKeyPtr(key).?;
map.removeByPtr(key_ptr);
try expectEqual(0, map.count());
try expectEqual(null, map.get(key));
}