Fix error when HashMap.remove on uninitialized HashMap (#1629)

* HashMap: test removal on uninitialized

* HashMap.remove_entry_for_key: return false on unintialized

* test: switch to temp_init

* release note
This commit is contained in:
Walther Chen
2024-11-18 08:20:32 -05:00
committed by GitHub
parent 295b374b48
commit f39e339726
3 changed files with 13 additions and 1 deletions

View File

@@ -482,6 +482,7 @@ fn void HashMap.free_internal(&map, void* ptr) @inline @private
fn bool HashMap.remove_entry_for_key(&map, Key key) @private
{
if (!map.count) return false;
uint hash = rehash(key.hash());
uint i = index_for(hash, map.table.len);
Entry* prev = map.table[i];

View File

@@ -12,6 +12,7 @@
- Fix issue writing a single byte in the WriteBuffer
- A distinct inline pointer type can now participate in pointer arithmetics.
- Support &a[0] returning the distinct type when applying it to a distinct of a pointer.
- Fix error when calling `HashMap.remove` on uninitialized `HashMap`.
### Stdlib changes
- Add `io::MultiReader`, `io::MultiWriter`, and `io::TeeReader` structs.

View File

@@ -53,4 +53,14 @@ fn void map()
assert(tc.key == list[i].key);
assert(tc.value == list[i].value);
}
}
}
fn void map_remove()
{
Map m;
assert(!@ok(m.remove("A")));
m.temp_init();
assert(!@ok(m.remove("A")));
m.set("A", 0);
assert(@ok(m.remove("A")));
}