Skip to content

Commit fbd70fe

Browse files
committed
Fix build error for linux builds and add basic validation test
1 parent c6c0229 commit fbd70fe

File tree

3 files changed

+29
-14
lines changed

3 files changed

+29
-14
lines changed

stdlib/public/core/Dictionary.swift

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -608,9 +608,9 @@ extension Dictionary {
608608
public __consuming func filter(
609609
_ isIncluded: (Element) throws -> Bool
610610
) rethrows -> [Key: Value] {
611-
if _variant.isNative {
612-
return Dictionary(_native: try _variant.asNative.filter(isIncluded))
613-
} else {
611+
#if _runtime(_ObjC)
612+
guard _variant.isNative else {
613+
// Slow path for bridged dictionaries
614614
var result = _NativeDictionary<Key, Value>()
615615
for element in self {
616616
if try isIncluded(element) {
@@ -619,6 +619,8 @@ extension Dictionary {
619619
}
620620
return Dictionary(_native: result)
621621
}
622+
#endif
623+
return Dictionary(_native: try _variant.asNative.filter(isIncluded))
622624
}
623625
}
624626

stdlib/public/core/NativeDictionary.swift

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -798,18 +798,18 @@ extension _NativeDictionary { // High-level operations
798798
_ isIncluded: (Element) throws -> Bool
799799
) rethrows -> _NativeDictionary<Key, Value> {
800800
try _UnsafeBitset.withTemporaryBitset(
801-
capacity: _storage._bucketCount
802-
) { bitset in
803-
var count = 0
804-
for bucket in hashTable {
805-
if try isIncluded(
806-
(uncheckedKey(at: bucket), uncheckedValue(at: bucket))
807-
) {
808-
bitset.uncheckedInsert(bucket.offset)
809-
count += 1
810-
}
801+
capacity: _storage._bucketCount
802+
) { bitset in
803+
var count = 0
804+
for bucket in hashTable {
805+
if try isIncluded(
806+
(uncheckedKey(at: bucket), uncheckedValue(at: bucket))
807+
) {
808+
bitset.uncheckedInsert(bucket.offset)
809+
count += 1
811810
}
812-
return extractDictionary(using: bitset, count: count)
811+
}
812+
return extractDictionary(using: bitset, count: count)
813813
}
814814
}
815815
}

validation-test/stdlib/Dictionary.swift

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2013,6 +2013,19 @@ DictionaryTestSuite.test("mapValues(_:)") {
20132013
}
20142014
}
20152015

2016+
DictionaryTestSuite.test("filter(_:)") {
2017+
let d1 = [1: 1, 2: 2, 3: 100, 4: 4, 5: 100, 6: 6]
2018+
let d2 = d1.filter() {key, value in key == value}
2019+
2020+
expectEqual(d2.count, 4)
2021+
for (key, value) in d2 {
2022+
expectEqual(key, value)
2023+
}
2024+
2025+
expectNil(d2[3])
2026+
expectNil(d2[5])
2027+
}
2028+
20162029
DictionaryTestSuite.test("capacity/init(minimumCapacity:)") {
20172030
let d0 = Dictionary<String, Int>(minimumCapacity: 0)
20182031
expectGE(d0.capacity, 0)

0 commit comments

Comments
 (0)