authorgravatar for mlugg@mlugg.co.ukMatthew Lugg <mlugg@mlugg.co.uk> 2026-05-06 10:04:54+01:00
committergravatar for mlugg@mlugg.co.ukMatthew Lugg <mlugg@mlugg.co.uk> 2026-05-07 06:20:58+02:00
log45ecd6a99689dbb200342bb576f75a1930455e02
tree0ed480874bd7ebb6f13b41f1fc74755f5417ff16
parent52c5223bca7c4e6269488288bc087a4cf94dd283

std.hash_map: fix `removeByPtr` with array key type

Resolves: https://codeberg.org/ziglang/zig/issues/32086

1 files changed, 20 insertions(+), 1 deletions(-)

lib/std/hash_map.zig+20-1
...@@ -1271,7 +1271,7 @@ pub fn HashMapUnmanaged(...@@ -1271,7 +1271,7 @@ pub fn HashMapUnmanaged(
1271 // map, which is assumed to exist as key_ptr must be valid. This1271 // map, which is assumed to exist as key_ptr must be valid. This
1272 // item must be at index 0.1272 // item must be at index 0.
1273 const idx = if (@sizeOf(K) > 0)1273 const idx = if (@sizeOf(K) > 0)
1274 (key_ptr - self.keys())1274 @as([*]K, @ptrCast(key_ptr)) - self.keys()
1275 else1275 else
1276 0;1276 0;
12771277
...@@ -2175,3 +2175,22 @@ test "rehash" {...@@ -2175,3 +2175,22 @@ test "rehash" {
2175 }2175 }
2176 }2176 }
2177}2177}
2178
2179test "removeByPtr, key is array" {
2180 const gpa = testing.allocator;
2181
2182 var map: AutoHashMapUnmanaged([2]u32, u32) = .empty;
2183 defer map.deinit(gpa);
2184
2185 const key: [2]u32 = .{ 1, 2 };
2186 try map.put(gpa, key, 3);
2187
2188 try expectEqual(1, map.count());
2189 try expectEqual(3, map.get(key));
2190
2191 const key_ptr = map.getKeyPtr(key).?;
2192 map.removeByPtr(key_ptr);
2193
2194 try expectEqual(0, map.count());
2195 try expectEqual(null, map.get(key));
2196}