Skip to content

Commit 96d93a4

Browse files
committed
[stdlib] Set, Dictionary: Clarify assert message when a duplicate key is found
When a duplicate key is found during rehashing, it is usually either because a key was mutated after insertion, or because the dictionary itself was mutated from multiple threads at the same time. Both of these are serious programmer errors. Promote the sanity check for duplicate keys to a full precondition and improve the error message to point out the most probable cause of the failure.
1 parent 69099a8 commit 96d93a4

File tree

2 files changed

+4
-4
lines changed

2 files changed

+4
-4
lines changed

stdlib/public/core/Dictionary.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2538,8 +2538,8 @@ extension _NativeDictionaryBuffer where Key: Hashable
25382538
@_versioned // FIXME(sil-serialize-all)
25392539
internal func unsafeAddNew(key newKey: Key, value: Value) {
25402540
let (i, found) = _find(newKey, startBucket: _bucket(newKey))
2541-
_sanityCheck(
2542-
!found, "unsafeAddNew was called, but the key is already present")
2541+
_precondition(
2542+
!found, "Duplicate key found in Dictionary. Keys may have been mutated after insertion")
25432543
initializeKey(newKey, value: value, at: i.offset)
25442544
}
25452545

stdlib/public/core/Set.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2161,8 +2161,8 @@ extension _NativeSetBuffer where Element: Hashable
21612161
@_versioned // FIXME(sil-serialize-all)
21622162
internal func unsafeAddNew(key newKey: Element) {
21632163
let (i, found) = _find(newKey, startBucket: _bucket(newKey))
2164-
_sanityCheck(
2165-
!found, "unsafeAddNew was called, but the key is already present")
2164+
_precondition(
2165+
!found, "Duplicate element found in Set. Elements may have been mutated after insertion")
21662166
initializeKey(newKey, at: i.offset)
21672167
}
21682168

0 commit comments

Comments
 (0)