Merge pull request #198 from lorentey/allow-building-with-swift-5.5

Restore support for building with Swift 5.5
This commit is contained in:
Karoy Lorentey authored and GitHub committed 2022-10-10 18:42:13 -07:00
commit dd224fc017
14 files changed
+89 -62

No files matched your search

@@ -120,8 +120,10 @@ extension BitSet._UnsafeHandle {
var buffer: (_Word, _Word) = (.empty, .empty)
return try withUnsafeMutablePointer(to: &buffer) { p in
// Homogeneous tuples are layout-compatible with their component type.
let words = UnsafeMutableRawPointer(p).assumingMemoryBound(to: _Word.self)
var bitset = Self(words: words, wordCount: wordCount, mutable: true)
let start = UnsafeMutableRawPointer(p)
.assumingMemoryBound(to: _Word.self)
let words = UnsafeMutableBufferPointer(start: start, count: wordCount)
var bitset = Self(words: words, mutable: true)
return try body(&bitset)
}
}
@@ -84,7 +84,7 @@ extension _Node {
itemMap: _Bitmap(bucket1, bucket2),
childMap: .empty,
count: 2
) { children, items in
) { children, items -> (_Slot, _Slot) in
assert(items.count == 2 && children.count == 0)
let i1 = bucket1 < bucket2 ? 1 : 0
let i2 = 1 &- i1
@@ -110,7 +110,7 @@ extension _Node {
) -> _Node {
defer { _invariantCheck() }
assert(!isCollisionNode)
let child = update {
let child: _Node = update {
assert($0.childMap.contains(bucket))
assert($0.childMap.slot(of: bucket) == slot)
let child = $0._removeChild(at: slot)
@@ -138,7 +138,7 @@ extension _Node {
@inlinable
internal mutating func removeSingletonChild() -> _Node {
defer { _invariantCheck() }
let child = update {
let child: _Node = update {
assert($0.itemCount == 0 && $0.childCount == 1)
let child = $0._removeChild(at: .zero)
$0.childMap = .empty
@@ -34,7 +34,7 @@ extension _Node {
internal mutating func replaceChild(
at bucket: _Bucket, _ slot: _Slot, with child: _Node
) -> Int {
let delta = update {
let delta: Int = update {
assert(!$0.isCollisionNode)
assert($0.childMap.contains(bucket))
assert($0.childMap.slot(of: bucket) == slot)
@@ -168,7 +168,7 @@ extension _Node {
let storage = Storage.allocate(
byteCapacity: occupiedBytes &+ extraBytes)
var node = _Node(storage: storage, count: count)
let result = node.update {
let result: R = node.update {
$0.itemMap = itemMap
$0.childMap = childMap
@@ -202,7 +202,7 @@ extension _Node {
assert(MemoryLayout<_Hash>.alignment <= MemoryLayout<_RawNode>.alignment)
let storage = Storage.allocate(byteCapacity: bytes &+ extraBytes)
var node = _Node(storage: storage, count: count)
let result = node.update {
let result: R = node.update {
$0.itemMap = _Bitmap(bitPattern: count)
$0.childMap = $0.itemMap
assert(bytes <= $0.bytesFree)
@@ -206,7 +206,7 @@ extension _Node {
let originalItemCount = self.count
for rs: _Slot in stride(from: .zero, to: r.itemsEndSlot, by: 1) {
let rp = r.itemPtr(at: rs)
let lslot = self.read { l in
let lslot: _Slot? = self.read { l in
let litems = l.reverseItems
return litems
.suffix(originalItemCount)
@@ -301,7 +301,7 @@ extension _Node {
}
if self.read({ $0.childMap.contains(bucket) }) {
self.ensureUnique(isUnique: isUnique)
let delta = try self.update { l in
let delta: Int = try self.update { l in
let lslot = l.childMap.slot(of: bucket)
let h = hashPrefix.appending(bucket, at: level)
let lchild = l.childPtr(at: lslot)
@@ -427,7 +427,7 @@ extension _Node {
itemSlot: _Slot
) {
let c = self.count
let node = update { src in
let node: _Node = update { src in
assert(!src.isCollisionNode)
assert(src.itemMap.contains(bucket))
assert(!src.childMap.contains(bucket))
@@ -147,7 +147,7 @@ extension _Node {
level, hash[level], path.currentItemSlot, by: { _ in })
} else {
let slot = path.childSlot(at: level)
let needsInlining = update {
let needsInlining: Bool = update {
let child = $0.childPtr(at: slot)
child.pointee._finalizeRemoval(level.descend(), hash, at: path)
return child.pointee.hasSingletonItem
@@ -33,7 +33,7 @@ extension _Node {
return _removeItemFromUniqueLeafNode(level, bucket, r.slot) { $0.move() }
}
let (old, needsInlining) = update {
let (old, needsInlining): (Element?, Bool) = update {
let child = $0.childPtr(at: r.slot)
let old = child.pointee.remove(level.descend(), key, hash)
guard old != nil else { return (old, false) }
@@ -98,7 +98,7 @@ extension _Node {
level, bucket, slot, by: { $0.move() })
}
let slot = path.childSlot(at: level)
let (item, needsInlining) = update {
let (item, needsInlining): (Element, Bool) = update {
let child = $0.childPtr(at: slot)
let item = child.pointee.remove(level.descend(), at: path)
return (item, child.pointee.hasSingletonItem)
@@ -46,15 +46,20 @@ where Key: Encodable, Value: Encodable
let codingKey = _DictionaryCodingKey(stringValue: key as! String)
try container.encode(value, forKey: codingKey)
}
} else if Key.self == Int.self {
return
}
if Key.self == Int.self {
// Since the keys are already Ints, we can use them as keys directly.
var container = encoder.container(keyedBy: _DictionaryCodingKey.self)
for (key, value) in self {
let codingKey = _DictionaryCodingKey(intValue: key as! Int)
try container.encode(value, forKey: codingKey)
}
} else if #available(macOS 12.3, iOS 15.4, watchOS 8.5, tvOS 15.4, *),
Key.self is CodingKeyRepresentable.Type {
return
}
#if swift(>=5.6)
if #available(macOS 12.3, iOS 15.4, watchOS 8.5, tvOS 15.4, *),
Key.self is CodingKeyRepresentable.Type {
// Since the keys are CodingKeyRepresentable, we can use the `codingKey`
// to create `_DictionaryCodingKey` instances.
var container = encoder.container(keyedBy: _DictionaryCodingKey.self)
@@ -63,15 +68,16 @@ where Key: Encodable, Value: Encodable
let dictionaryCodingKey = _DictionaryCodingKey(codingKey: codingKey)
try container.encode(value, forKey: dictionaryCodingKey)
}
} else {
// Keys are Encodable but not Strings or Ints, so we cannot arbitrarily
// convert to keys. We can encode as an array of alternating key-value
// pairs, though.
var container = encoder.unkeyedContainer()
for (key, value) in self {
try container.encode(key)
try container.encode(value)
}
return
}
#endif
// Keys are Encodable but not Strings or Ints, so we cannot arbitrarily
// convert to keys. We can encode as an array of alternating key-value
// pairs, though.
var container = encoder.unkeyedContainer()
for (key, value) in self {
try container.encode(key)
try container.encode(value)
}
}
}
@@ -95,7 +101,9 @@ where Key: Decodable, Value: Decodable
let value = try container.decode(Value.self, forKey: key)
self[key.stringValue as! Key] = value
}
} else if Key.self == Int.self {
return
}
if Key.self == Int.self {
// The keys are Ints, so we should be able to expect a keyed container.
let container = try decoder.container(keyedBy: _DictionaryCodingKey.self)
for key in container.allKeys {
@@ -119,8 +127,12 @@ where Key: Decodable, Value: Decodable
let value = try container.decode(Value.self, forKey: key)
self[key.intValue! as! Key] = value
}
} else if #available(macOS 12.3, iOS 15.4, watchOS 8.5, tvOS 15.4, *),
let keyType = Key.self as? CodingKeyRepresentable.Type {
return
}
#if swift(>=5.6)
if #available(macOS 12.3, iOS 15.4, watchOS 8.5, tvOS 15.4, *),
let keyType = Key.self as? CodingKeyRepresentable.Type {
// The keys are CodingKeyRepresentable, so we should be able to expect
// a keyed container.
let container = try decoder.container(keyedBy: _DictionaryCodingKey.self)
@@ -135,38 +147,40 @@ where Key: Decodable, Value: Decodable
let value: Value = try container.decode(Value.self, forKey: codingKey)
self[key] = value
}
} else {
// We should have encoded as an array of alternating key-value pairs.
var container = try decoder.unkeyedContainer()
return
}
#endif
// We're expecting to get pairs. If the container has a known count, it
// had better be even; no point in doing work if not.
if let count = container.count {
guard count % 2 == 0 else {
throw DecodingError.dataCorrupted(
DecodingError.Context(
codingPath: decoder.codingPath,
debugDescription: "Expected collection of key-value pairs; encountered odd-length array instead."
)
// We should have encoded as an array of alternating key-value pairs.
var container = try decoder.unkeyedContainer()
// We're expecting to get pairs. If the container has a known count, it
// had better be even; no point in doing work if not.
if let count = container.count {
guard count % 2 == 0 else {
throw DecodingError.dataCorrupted(
DecodingError.Context(
codingPath: decoder.codingPath,
debugDescription: "Expected collection of key-value pairs; encountered odd-length array instead."
)
}
)
}
}
while !container.isAtEnd {
let key = try container.decode(Key.self)
guard !container.isAtEnd else {
throw DecodingError.dataCorrupted(
DecodingError.Context(
codingPath: decoder.codingPath,
debugDescription: "Unkeyed container reached end before value in key-value pair."
)
)
}
while !container.isAtEnd {
let key = try container.decode(Key.self)
guard !container.isAtEnd else {
throw DecodingError.dataCorrupted(
DecodingError.Context(
codingPath: decoder.codingPath,
debugDescription: "Unkeyed container reached end before value in key-value pair."
)
)
}
let value = try container.decode(Value.self)
self[key] = value
}
let value = try container.decode(Value.self)
self[key] = value
}
}
}
@@ -14,7 +14,7 @@
///
/// To ensure maximum utility, this protocol doesn't refine `Collection`,
/// although it does share some of the same requirements.
public protocol DictionaryAPIChecker<Key, Value> {
public protocol DictionaryAPIChecker {
associatedtype Key
associatedtype Value
associatedtype Index
@@ -64,7 +64,7 @@ extension UnsafeMutableBufferPointer {
fromContentsOf source: C
) -> Index
where C.Element == Element {
let count = source._withContiguousStorageIfAvailable_SR14663 {
let count: Int? = source._withContiguousStorageIfAvailable_SR14663 {
guard let sourceAddress = $0.baseAddress, !$0.isEmpty else {
return 0
}
@@ -75,7 +75,7 @@ extension UnsafeMutableBufferPointer {
baseAddress?.initialize(from: sourceAddress, count: $0.count)
return $0.count
}
if let count {
if let count = count {
return startIndex.advanced(by: count)
}
@@ -390,12 +390,15 @@ extension LifetimeTracker {
map: PersistentDictionary<LifetimeTracked<Key>, LifetimeTracked<Value>>,
ref: [(key: LifetimeTracked<Key>, value: LifetimeTracked<Value>)]
) {
let ref = fixture.itemsInIterationOrder.map { item in
typealias K = LifetimeTracked<Key>
typealias V = LifetimeTracked<Value>
let ref: [(key: K, value: V)] = fixture.itemsInIterationOrder.map { item in
let key = keyTransform(item)
let value = valueTransform(item)
return (key: self.instance(for: key), value: self.instance(for: value))
}
let ref2 = fixture.itemsInInsertionOrder.map { item in
let ref2: [(key: K, value: V)] = fixture.itemsInInsertionOrder.map { item in
let key = keyTransform(item)
let value = valueTransform(item)
return (key: self.instance(for: key), value: self.instance(for: value))
@@ -288,6 +288,7 @@ class PersistentDictionaryTests: CollectionTestCase {
}
}
#if swift(>=5.6)
@available(macOS 12.3, iOS 15.4, watchOS 8.5, tvOS 15.4, *)
struct FancyDictionaryKey: CodingKeyRepresentable, Hashable, Codable {
var value: Int
@@ -303,6 +304,7 @@ class PersistentDictionaryTests: CollectionTestCase {
self.init(value)
}
}
#endif
struct BoringDictionaryKey: Hashable, Codable {
var value: Int
@@ -328,6 +330,7 @@ class PersistentDictionaryTests: CollectionTestCase {
])
expectEqual(try MinimalEncoder.encode(d2), v2)
#if swift(>=5.6)
if #available(macOS 12.3, iOS 15.4, watchOS 8.5, tvOS 15.4, *) {
let d3: PersistentDictionary<FancyDictionaryKey, Int16> = [
FancyDictionaryKey(1): 10, FancyDictionaryKey(2): 20
@@ -337,6 +340,7 @@ class PersistentDictionaryTests: CollectionTestCase {
])
expectEqual(try MinimalEncoder.encode(d3), v3)
}
#endif
let d4: PersistentDictionary<BoringDictionaryKey, UInt8> = [
// Note: we only have a single element to prevent ordering
@@ -372,6 +376,7 @@ class PersistentDictionaryTests: CollectionTestCase {
try MinimalDecoder.decode(v2, as: PD<Int, String>.self),
d2)
#if swift(>=5.6)
if #available(macOS 12.3, iOS 15.4, watchOS 8.5, tvOS 15.4, *) {
let d3: PersistentDictionary<FancyDictionaryKey, Int16> = [
FancyDictionaryKey(1): 10, FancyDictionaryKey(2): 20
@@ -383,6 +388,7 @@ class PersistentDictionaryTests: CollectionTestCase {
try MinimalDecoder.decode(v3, as: PD<FancyDictionaryKey, Int16>.self),
d3)
}
#endif
let d4: PersistentDictionary<BoringDictionaryKey, UInt8> = [
// Note: we only have a single element to prevent ordering
@@ -404,6 +410,7 @@ class PersistentDictionaryTests: CollectionTestCase {
expectTrue($0 is DecodingError)
}
#if swift(>=5.6)
if #available(macOS 12.3, iOS 15.4, watchOS 8.5, tvOS 15.4, *) {
let v6: MinimalEncoder.Value = .dictionary([
"This is not a number": .string("bus"),
@@ -426,6 +433,7 @@ class PersistentDictionaryTests: CollectionTestCase {
try MinimalDecoder.decode(v7, as: PD<FancyDictionaryKey, String>.self),
d7)
}
#endif
let v8: MinimalEncoder.Value = .array([
.int32(1), .string("bike"), .int32(2),