commit 45ecd6a99689dbb200342bb576f75a1930455e02 (tree)
parent 52c5223bca7c4e6269488288bc087a4cf94dd283
Author: Matthew Lugg <mlugg@mlugg.co.uk>
Date: Wed, 6 May 2026 10:04:54 +0100
std.hash_map: fix `removeByPtr` with array key type
Resolves: https://codeberg.org/ziglang/zig/issues/32086
Diffstat:
1 file changed, 20 insertions(+), 1 deletion(-)
diff --git a/lib/std/hash_map.zig b/lib/std/hash_map.zig
@@ -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));
+}