From 93c7cd7e2c4d55c470875d4516266bea1cb22676 Mon Sep 17 00:00:00 2001 From: Karoy Lorentey Date: Fri, 2 May 2025 18:18:29 -0700 Subject: [PATCH 1/2] Add support for older 6.2 compiler builds --- Package.swift | 4 +- Sources/DequeModule/Deque+Container.swift | 4 +- Sources/DequeModule/DynamicDeque.swift | 4 +- Sources/DequeModule/RigidDeque.swift | 4 +- Sources/Future/Arrays/DynamicArray.swift | 60 ++-- Sources/Future/Arrays/NewArray.swift | 14 +- Sources/Future/Arrays/RigidArray.swift | 286 +++++++++++++++--- .../Containers/BidirectionalContainer.swift | 4 +- Sources/Future/Containers/Container.swift | 18 +- .../Containers/ContainerAlgorithms.swift | 24 +- .../Future/Containers/MutableContainer.swift | 86 +++++- .../Containers/RandomAccessContainer.swift | 6 +- .../Containers/RepeatingContainer.swift | 4 +- Sources/Future/Span/LifetimeOverride.swift | 4 + Sources/Future/Span/OutputSpan.swift | 15 + Sources/Future/Span/SpanExtensions.swift | 4 +- .../Span/UnsafeBufferPointer+Additions.swift | 6 +- Sources/Future/Tools/Box.swift | 25 +- Sources/Future/Tools/Inout.swift | 6 +- Sources/Future/Tools/Shared.swift | 23 +- .../MinimalTypeConformances.swift | 2 +- .../StaccatoContainerTests.swift | 1 + .../ArrayTests/DynamicArrayTests.swift | 1 + .../ArrayTests/RigidArrayTests.swift | 2 +- .../FutureTests/ContiguousStorageTests.swift | 1 + .../Assertions+Containers.swift | 8 +- .../ConformanceCheckers/CheckContainer.swift | 6 +- .../MinimalTypes/StaccatoContainer.swift | 2 +- 28 files changed, 483 insertions(+), 141 deletions(-) diff --git a/Package.swift b/Package.swift index 7b65673d..ece0e0f3 100644 --- a/Package.swift +++ b/Package.swift @@ -1,4 +1,4 @@ -// swift-tools-version:6.0 +// swift-tools-version:6.2 //===----------------------------------------------------------------------===// // // This source file is part of the Swift Collections open source project @@ -56,8 +56,6 @@ let availabilityMacros: KeyValuePairs = [ "SwiftStdlib 6.0": "macOS 15.0, iOS 18.0, watchOS 11.0, tvOS 18.0, visionOS 2.0", "SwiftStdlib 6.1": "macOS 15.4, iOS 18.4, watchOS 11.4, tvOS 18.4, visionOS 2.4", "SwiftStdlib 6.2": "macOS 9999, iOS 9999, watchOS 9999, tvOS 9999, visionOS 9999", - "SwiftCompatibilitySpan 5.0": "macOS 10.14.4, iOS 12.2, watchOS 5.2, tvOS 12.2, visionOS 1.1", - "SwiftCompatibilitySpan 6.2": "macOS 15.0, iOS 18.0, watchOS 11.0, tvOS 18.0, visionOS 2.0", ] let extraSettings: [SwiftSetting] = [ diff --git a/Sources/DequeModule/Deque+Container.swift b/Sources/DequeModule/Deque+Container.swift index 3fa7eba1..9cedc351 100644 --- a/Sources/DequeModule/Deque+Container.swift +++ b/Sources/DequeModule/Deque+Container.swift @@ -20,13 +20,13 @@ extension Deque: RandomAccessContainer { _storage.value.borrowElement(at: index) } - @available(SwiftCompatibilitySpan 5.0, *) + @available(SwiftStdlib 6.2, *) @lifetime(borrow self) public func nextSpan(after index: inout Int) -> Span { _storage.value.nextSpan(after: &index) } - @available(SwiftCompatibilitySpan 5.0, *) + @available(SwiftStdlib 6.2, *) @lifetime(borrow self) public func previousSpan(before index: inout Int) -> Span { _storage.value.previousSpan(before: &index) diff --git a/Sources/DequeModule/DynamicDeque.swift b/Sources/DequeModule/DynamicDeque.swift index b20719ae..beb8b99c 100644 --- a/Sources/DequeModule/DynamicDeque.swift +++ b/Sources/DequeModule/DynamicDeque.swift @@ -33,13 +33,13 @@ public struct DynamicDeque: ~Copyable { extension DynamicDeque: @unchecked Sendable where Element: Sendable & ~Copyable {} extension DynamicDeque: RandomAccessContainer where Element: ~Copyable { - @available(SwiftCompatibilitySpan 5.0, *) + @available(SwiftStdlib 6.2, *) @lifetime(borrow self) public func nextSpan(after index: inout Int) -> Span { _storage.nextSpan(after: &index) } - @available(SwiftCompatibilitySpan 5.0, *) + @available(SwiftStdlib 6.2, *) @lifetime(borrow self) public func previousSpan(before index: inout Int) -> Span { _storage.previousSpan(before: &index) diff --git a/Sources/DequeModule/RigidDeque.swift b/Sources/DequeModule/RigidDeque.swift index f342ff77..31b5d8a6 100644 --- a/Sources/DequeModule/RigidDeque.swift +++ b/Sources/DequeModule/RigidDeque.swift @@ -61,7 +61,7 @@ extension RigidDeque where Element: ~Copyable { } } -@available(SwiftCompatibilitySpan 5.0, *) +@available(SwiftStdlib 6.2, *) extension RigidDeque where Element: ~Copyable { @inlinable @lifetime(borrow self) @@ -78,7 +78,7 @@ extension RigidDeque where Element: ~Copyable { } } -@available(SwiftCompatibilitySpan 5.0, *) +@available(SwiftStdlib 6.2, *) extension RigidDeque: RandomAccessContainer, MutableContainer where Element: ~Copyable { @inlinable @lifetime(borrow self) diff --git a/Sources/Future/Arrays/DynamicArray.swift b/Sources/Future/Arrays/DynamicArray.swift index e7e3000a..bf91308a 100644 --- a/Sources/Future/Arrays/DynamicArray.swift +++ b/Sources/Future/Arrays/DynamicArray.swift @@ -74,7 +74,7 @@ extension DynamicArray /*where Element: Copyable*/ { self.append(copying: contents) } - @available(SwiftCompatibilitySpan 5.0, *) + @available(SwiftStdlib 6.2, *) @_alwaysEmitIntoClient @inline(__always) public init & ~Copyable & ~Escapable>( @@ -84,7 +84,7 @@ extension DynamicArray /*where Element: Copyable*/ { self.init(consuming: RigidArray(capacity: capacity, copying: contents)) } - @available(SwiftCompatibilitySpan 5.0, *) + @available(SwiftStdlib 6.2, *) @_alwaysEmitIntoClient @inline(__always) public init & Sequence>( @@ -110,7 +110,7 @@ extension DynamicArray where Element: ~Copyable { //MARK: - Span creation extension DynamicArray where Element: ~Copyable { - @available(SwiftCompatibilitySpan 5.0, *) + @available(SwiftStdlib 6.2, *) public var span: Span { @lifetime(borrow self) @inlinable @@ -118,8 +118,9 @@ extension DynamicArray where Element: ~Copyable { _storage.span } } - - @available(SwiftCompatibilitySpan 5.0, *) + +#if compiler(>=6.2) && $InoutLifetimeDependence + @available(SwiftStdlib 6.2, *) public var mutableSpan: MutableSpan { @lifetime(&self) @inlinable @@ -127,6 +128,16 @@ extension DynamicArray where Element: ~Copyable { _storage.mutableSpan } } +#else + @available(SwiftStdlib 6.2, *) + public var mutableSpan: MutableSpan { + @lifetime(borrow self) + @inlinable + mutating get { + _storage.mutableSpan + } + } +#endif } //MARK: RandomAccessContainer conformance @@ -162,7 +173,7 @@ extension DynamicArray where Element: ~Copyable { } } -@available(SwiftCompatibilitySpan 5.0, *) +@available(SwiftStdlib 6.2, *) extension DynamicArray: RandomAccessContainer where Element: ~Copyable { @inlinable @lifetime(borrow self) @@ -181,21 +192,28 @@ extension DynamicArray: RandomAccessContainer where Element: ~Copyable { extension DynamicArray where Element: ~Copyable { @inlinable +#if compiler(>=6.2) && $InoutLifetimeDependence @lifetime(&self) +#else + @lifetime(borrow self) +#endif public mutating func mutateElement(at index: Int) -> Inout { _storage.mutateElement(at: index) } @inlinable - @lifetime(&self) public mutating func swapAt(_ i: Int, _ j: Int) { _storage.swapAt(i, j) } } extension DynamicArray: MutableContainer where Element: ~Copyable { - @available(SwiftCompatibilitySpan 5.0, *) + @available(SwiftStdlib 6.2, *) +#if compiler(>=6.2) && $InoutLifetimeDependence @lifetime(&self) +#else + @lifetime(borrow self) +#endif public mutating func nextMutableSpan(after index: inout Int) -> MutableSpan { _storage.nextMutableSpan(after: &index) } @@ -348,7 +366,7 @@ extension DynamicArray where Element: ~Copyable { /// whether the element should be removed from the array. /// /// - Complexity: O(`count`) - @available(SwiftCompatibilitySpan 5.0, *) + @available(SwiftStdlib 6.2, *) @_alwaysEmitIntoClient public mutating func removeAll( where shouldBeRemoved: (borrowing Element) throws(E) -> Bool @@ -440,7 +458,7 @@ extension DynamicArray { /// /// - Complexity: O(`newElements.count`) when amortized over many /// invocations on the same array. - @available(SwiftCompatibilitySpan 5.0, *) + @available(SwiftStdlib 6.2, *) @_alwaysEmitIntoClient public mutating func append(copying newElements: Span) { _ensureFreeCapacity(newElements.count) @@ -475,7 +493,7 @@ extension DynamicArray { } } - @available(SwiftCompatibilitySpan 5.0, *) + @available(SwiftStdlib 6.2, *) public mutating func _appendContainer< C: Container & ~Copyable & ~Escapable >( @@ -499,7 +517,7 @@ extension DynamicArray { /// /// - Complexity: O(`newElements.count`), when amortized over many invocations /// over the same array. - @available(SwiftCompatibilitySpan 5.0, *) + @available(SwiftStdlib 6.2, *) @_alwaysEmitIntoClient public mutating func append< C: Container & ~Copyable & ~Escapable @@ -519,7 +537,7 @@ extension DynamicArray { /// /// - Complexity: O(`newElements.count`), when amortized over many invocations /// over the same array. - @available(SwiftCompatibilitySpan 5.0, *) + @available(SwiftStdlib 6.2, *) @_alwaysEmitIntoClient public mutating func append< C: Container & Sequence @@ -636,7 +654,7 @@ extension DynamicArray { /// /// - Complexity: O(*n* + *m*), where *n* is count of this array and /// *m* is the count of `newElements`. - @available(SwiftCompatibilitySpan 5.0, *) + @available(SwiftStdlib 6.2, *) @inlinable public mutating func insert( copying newElements: Span, at index: Int @@ -677,7 +695,7 @@ extension DynamicArray { at: index, copying: newElements, newCount: newCount) } - @available(SwiftCompatibilitySpan 5.0, *) + @available(SwiftStdlib 6.2, *) @inlinable internal mutating func _insertContainer< C: Container & ~Copyable & ~Escapable @@ -711,7 +729,7 @@ extension DynamicArray { /// /// - Complexity: O(*n* + *m*), where *n* is count of this array and /// *m* is the count of `newElements`. - @available(SwiftCompatibilitySpan 5.0, *) + @available(SwiftStdlib 6.2, *) @_alwaysEmitIntoClient @inline(__always) public mutating func insert< @@ -742,7 +760,7 @@ extension DynamicArray { /// /// - Complexity: O(*n* + *m*), where *n* is count of this array and /// *m* is the count of `newElements`. - @available(SwiftCompatibilitySpan 5.0, *) + @available(SwiftStdlib 6.2, *) @_alwaysEmitIntoClient @inline(__always) public mutating func insert< @@ -850,7 +868,7 @@ extension DynamicArray { /// /// - Complexity: O(*n* + *m*), where *n* is count of this array and /// *m* is the count of `newElements`. - @available(SwiftCompatibilitySpan 5.0, *) + @available(SwiftStdlib 6.2, *) @inlinable public mutating func replaceSubrange( _ subrange: Range, @@ -898,7 +916,7 @@ extension DynamicArray { subrange, copyingCollection: newElements, newCount: c) } - @available(SwiftCompatibilitySpan 5.0, *) + @available(SwiftStdlib 6.2, *) @inlinable public mutating func _replaceSubrange< C: Container & ~Copyable & ~Escapable @@ -937,7 +955,7 @@ extension DynamicArray { /// /// - Complexity: O(*n* + *m*), where *n* is count of this array and /// *m* is the count of `newElements`. - @available(SwiftCompatibilitySpan 5.0, *) + @available(SwiftStdlib 6.2, *) @inlinable @inline(__always) public mutating func replaceSubrange< @@ -973,7 +991,7 @@ extension DynamicArray { /// /// - Complexity: O(*n* + *m*), where *n* is count of this array and /// *m* is the count of `newElements`. - @available(SwiftCompatibilitySpan 5.0, *) + @available(SwiftStdlib 6.2, *) @inlinable @inline(__always) public mutating func replaceSubrange< diff --git a/Sources/Future/Arrays/NewArray.swift b/Sources/Future/Arrays/NewArray.swift index fbbc7791..b5db4e90 100644 --- a/Sources/Future/Arrays/NewArray.swift +++ b/Sources/Future/Arrays/NewArray.swift @@ -59,7 +59,7 @@ extension NewArray: RandomAccessCollection, MutableCollection { } } -@available(SwiftCompatibilitySpan 5.0, *) +@available(SwiftStdlib 6.2, *) extension NewArray: RandomAccessContainer, MutableContainer { @lifetime(borrow self) public func borrowElement(at index: Int) -> Borrow { @@ -76,13 +76,21 @@ extension NewArray: RandomAccessContainer, MutableContainer { return _storage.value.previousSpan(before: &index) } +#if compiler(>=6.2) && $InoutLifetimeDependence @lifetime(&self) +#else + @lifetime(borrow self) +#endif public mutating func mutateElement(at index: Int) -> Inout { _ensureUnique() return _storage.value.mutateElement(at: index) } +#if compiler(>=6.2) && $InoutLifetimeDependence @lifetime(&self) +#else + @lifetime(borrow self) +#endif public mutating func nextMutableSpan(after index: inout Int) -> MutableSpan { _ensureUnique() return _storage.value.nextMutableSpan(after: &index) @@ -172,7 +180,7 @@ extension NewArray { self._storage.value.append(newElement) } - @available(SwiftCompatibilitySpan 5.0, *) + @available(SwiftStdlib 6.2, *) @inlinable public mutating func insert(_ item: consuming Element, at index: Int) { precondition(index >= 0 && index <= count) @@ -211,7 +219,7 @@ extension NewArray { } - @available(SwiftCompatibilitySpan 5.0, *) + @available(SwiftStdlib 6.2, *) @inlinable public mutating func removeLast() -> Element { precondition(count > 0, "Cannot remove last element from empty array") diff --git a/Sources/Future/Arrays/RigidArray.swift b/Sources/Future/Arrays/RigidArray.swift index edf045a6..f10ed9e0 100644 --- a/Sources/Future/Arrays/RigidArray.swift +++ b/Sources/Future/Arrays/RigidArray.swift @@ -13,8 +13,42 @@ import InternalCollectionsUtilities #endif -/// A manually resizable, heap allocated, noncopyable array of -/// potentially noncopyable elements. +/// A fixed capacity, heap allocated, noncopyable array of potentially +/// noncopyable elements. +/// +/// `RigidArray` instances are created with a certain maximum capacity. Elements +/// can be added to the array up to that capacity, but no more: trying to add an +/// item to a full array results in a runtime trap. +/// +/// var items = RigidArray(capacity: 2) +/// items.append(1) +/// items.append(2) +/// items.append(3) // Runtime error: RigidArray capacity overflow +/// +/// Rigid arrays provide convenience properties to help verify that it has +/// enough available capacity: `isFull` and `freeCapacity`. +/// +/// guard items.freeCapacity >= 4 else { throw CapacityOverflow() } +/// items.append(copying: newItems) +/// +/// It is possible to extend or shrink the capacity of a rigid array instance, +/// but this needs to be done explicitly, with operations dedicated to this +/// purpose (such as ``reserveCapacity`` and ``reallocate(capacity:)``). +/// The array never resizes itself automatically. +/// +/// It therefore requires careful manual analysis or up front runtime capacity +/// checks to prevent the array from overflowing its storage. This makes +/// this type more difficult to use than a dynamic array. However, it allows +/// this construct to provide predictably stable performance. +/// +/// This trading of usability in favor of stable performance limits `RigidArray` +/// to the most resource-constrained of use cases, such as space-constrained +/// environments that require carefully accounting of every heap allocation, or +/// time-constrained applications that cannot accommodate unexpected latency +/// spikes due to a reallocation getting triggered at an inopportune moment. +/// +/// For use cases outside of these narrow domains, we generally recommmend +/// the use of ``DynamicArray`` rather than `RigidArray`. @safe @frozen public struct RigidArray: ~Copyable { @@ -95,7 +129,7 @@ extension RigidArray /*where Element: Copyable*/ { self.append(copying: contents) } - @available(SwiftCompatibilitySpan 5.0, *) + @available(SwiftStdlib 6.2, *) @_alwaysEmitIntoClient @inline(__always) public init & Sequence>( @@ -116,7 +150,7 @@ extension RigidArray /*where Element: Copyable*/ { self.append(copying: contents) } - @available(SwiftCompatibilitySpan 5.0, *) + @available(SwiftStdlib 6.2, *) @_alwaysEmitIntoClient @inline(__always) public init & ~Copyable & ~Escapable>( @@ -127,7 +161,7 @@ extension RigidArray /*where Element: Copyable*/ { self.append(copying: contents) } - @available(SwiftCompatibilitySpan 5.0, *) + @available(SwiftStdlib 6.2, *) @_alwaysEmitIntoClient @inline(__always) public init & Collection>( @@ -139,6 +173,8 @@ extension RigidArray /*where Element: Copyable*/ { } } +// FIXME: init(moving:), init(consuming:) + //MARK: - Basics extension RigidArray where Element: ~Copyable { @@ -170,7 +206,7 @@ extension RigidArray where Element: ~Copyable { //MARK: - Span creation extension RigidArray where Element: ~Copyable { - @available(SwiftCompatibilitySpan 5.0, *) + @available(SwiftStdlib 6.2, *) public var span: Span { @lifetime(borrow self) @inlinable @@ -180,7 +216,8 @@ extension RigidArray where Element: ~Copyable { } } - @available(SwiftCompatibilitySpan 5.0, *) +#if compiler(>=6.2) && $InoutLifetimeDependence + @available(SwiftStdlib 6.2, *) public var mutableSpan: MutableSpan { @lifetime(&self) @inlinable @@ -189,17 +226,32 @@ extension RigidArray where Element: ~Copyable { return unsafe _overrideLifetime(result, mutating: &self) } } +#else + @available(SwiftStdlib 6.2, *) + public var mutableSpan: MutableSpan { + @lifetime(borrow self) + @inlinable + mutating get { + let result = unsafe MutableSpan(_unsafeElements: _items) + return unsafe _overrideLifetime(result, mutating: &self) + } + } +#endif - @available(SwiftCompatibilitySpan 5.0, *) + @available(SwiftStdlib 6.2, *) @inlinable @lifetime(borrow self) internal func _span(in range: Range) -> Span { span._extracting(range) } - @available(SwiftCompatibilitySpan 5.0, *) + @available(SwiftStdlib 6.2, *) @inlinable +#if compiler(>=6.2) && $InoutLifetimeDependence @lifetime(&self) +#else + @lifetime(borrow self) +#endif internal mutating func _mutableSpan( in range: Range ) -> MutableSpan { @@ -260,7 +312,7 @@ extension RigidArray where Element: ~Copyable { } } -@available(SwiftCompatibilitySpan 5.0, *) +@available(SwiftStdlib 6.2, *) extension RigidArray: RandomAccessContainer where Element: ~Copyable { @inlinable @lifetime(borrow self) @@ -277,7 +329,11 @@ extension RigidArray: RandomAccessContainer where Element: ~Copyable { extension RigidArray where Element: ~Copyable { @inlinable +#if compiler(>=6.2) && $InoutLifetimeDependence @lifetime(&self) +#else + @lifetime(borrow self) +#endif public mutating func mutateElement(at index: Int) -> Inout { precondition(index >= 0 && index < _count) return unsafe Inout( @@ -295,9 +351,13 @@ extension RigidArray where Element: ~Copyable { } } -@available(SwiftCompatibilitySpan 5.0, *) +@available(SwiftStdlib 6.2, *) extension RigidArray: MutableContainer where Element: ~Copyable { +#if compiler(>=6.2) && $InoutLifetimeDependence @lifetime(&self) +#else + @lifetime(borrow self) +#endif public mutating func nextMutableSpan( after index: inout Int ) -> MutableSpan { @@ -508,7 +568,7 @@ extension RigidArray where Element: ~Copyable { /// whether the element should be removed from the array. /// /// - Complexity: O(`count`) - @available(SwiftCompatibilitySpan 5.0, *) + @available(SwiftStdlib 6.2, *) @_alwaysEmitIntoClient public mutating func removeAll( where shouldBeRemoved: (borrowing Element) throws(E) -> Bool @@ -551,6 +611,24 @@ extension RigidArray where Element: ~Copyable { unsafe _storage.initializeElement(at: _count, to: item) _count &+= 1 } + + /// Adds an element to the end of the array, if possible. + /// + /// If the array does not have sufficient capacity to hold any more elements, + /// then this returns the given item without appending it; otherwise it + /// returns nil. + /// + /// - Parameter item: The element to append to the collection. + /// - Returns: `item` if the array is full; otherwise nil. + /// + /// - Complexity: O(1) + @inlinable + public mutating func pushLast(_ item: consuming Element) -> Element? { + // FIXME: Remove this in favor of a standard algorithm. + if isFull { return item } + append(item) + return nil + } } extension RigidArray where Element: ~Copyable { @@ -576,6 +654,18 @@ extension RigidArray where Element: ~Copyable { _count &+= items.count } + /// Appends the elements of a given array to the end of this array by moving + /// them between the containers. On return, the input array becomes empty, but + /// it is not destroyed, and it preserves its original storage capacity. + /// + /// If the target array does not have sufficient capacity to hold all items + /// in the source array, then this triggers a runtime error. + /// + /// - Parameters + /// - items: A fully initialized buffer whose contents to move into + /// the array. + /// + /// - Complexity: O(`items.count`) @_alwaysEmitIntoClient public mutating func append( moving items: inout RigidArray @@ -588,6 +678,17 @@ extension RigidArray where Element: ~Copyable { } } + /// Appends the elements of a given container to the end of this array by + /// consuming the source container. + /// + /// If the target array does not have sufficient capacity to hold all items + /// in the source array, then this triggers a runtime error. + /// + /// - Parameters + /// - items: A fully initialized buffer whose contents to move into + /// the array. + /// + /// - Complexity: O(`items.count`) @_alwaysEmitIntoClient public mutating func append( consuming items: consuming RigidArray @@ -647,7 +748,7 @@ extension RigidArray { /// - newElements: A span whose contents to copy into the array. /// /// - Complexity: O(`newElements.count`) - @available(SwiftCompatibilitySpan 5.0, *) + @available(SwiftStdlib 6.2, *) @_alwaysEmitIntoClient public mutating func append(copying items: Span) { unsafe items.withUnsafeBufferPointer { source in @@ -686,7 +787,7 @@ extension RigidArray { precondition(it.next() == nil, "RigidArray capacity overflow") } - @available(SwiftCompatibilitySpan 5.0, *) + @available(SwiftStdlib 6.2, *) @inlinable internal mutating func _appendContainer< C: Container & ~Copyable & ~Escapable @@ -708,7 +809,7 @@ extension RigidArray { /// - newElements: A container whose contents to copy into the array. /// /// - Complexity: O(`newElements.count`) - @available(SwiftCompatibilitySpan 5.0, *) + @available(SwiftStdlib 6.2, *) @_alwaysEmitIntoClient @inline(__always) public mutating func append & ~Copyable & ~Escapable>( @@ -726,7 +827,7 @@ extension RigidArray { /// - newElements: The new elements to copy into the array. /// /// - Complexity: O(*m*), where *m* is the length of `newElements`. - @available(SwiftCompatibilitySpan 5.0, *) + @available(SwiftStdlib 6.2, *) @_alwaysEmitIntoClient @inline(__always) public mutating func append< @@ -795,6 +896,18 @@ extension RigidArray where Element: ~Copyable { _count &+= items.count } + /// Inserts the elements of a given array into the given position in this + /// array by moving them between the containers. On return, the input array + /// becomes empty, but it is not destroyed, and it preserves its original + /// storage capacity. + /// + /// If the target array does not have sufficient capacity to hold all items + /// in the source array, then this triggers a runtime error. + /// + /// - Parameters + /// - items: An array whose contents to move into `self`. + /// + /// - Complexity: O(`items.count`) @_alwaysEmitIntoClient public mutating func insert( moving items: inout RigidArray, @@ -809,6 +922,17 @@ extension RigidArray where Element: ~Copyable { } } + /// Inserts the elements of a given array into the given position in this + /// array by consuming the source container. + /// + /// If the target array does not have sufficient capacity to hold all items + /// in the source array, then this triggers a runtime error. + /// + /// - Parameters + /// - items: A fully initialized buffer whose contents to move into + /// the array. + /// + /// - Complexity: O(`items.count`) @_alwaysEmitIntoClient public mutating func insert( consuming items: consuming RigidArray, @@ -817,7 +941,6 @@ extension RigidArray where Element: ~Copyable { var items = items self.insert(moving: &items, at: index) } - } extension RigidArray { @@ -904,7 +1027,7 @@ extension RigidArray { /// /// - Complexity: O(*n* + *m*), where *n* is count of this array and /// *m* is the count of `newElements`. - @available(SwiftCompatibilitySpan 5.0, *) + @available(SwiftStdlib 6.2, *) @inlinable public mutating func insert( copying newElements: Span, at index: Int @@ -943,7 +1066,7 @@ extension RigidArray { _count += newCount } - @available(SwiftCompatibilitySpan 5.0, *) + @available(SwiftStdlib 6.2, *) @inlinable internal mutating func _insertContainer< C: Container & ~Copyable & ~Escapable @@ -1011,7 +1134,7 @@ extension RigidArray { /// /// - Complexity: O(*n* + *m*), where *n* is count of this array and /// *m* is the count of `newElements`. - @available(SwiftCompatibilitySpan 5.0, *) + @available(SwiftStdlib 6.2, *) @_alwaysEmitIntoClient @inline(__always) public mutating func insert< @@ -1043,7 +1166,7 @@ extension RigidArray { /// /// - Complexity: O(*n* + *m*), where *n* is count of this array and /// *m* is the count of `newElements`. - @available(SwiftCompatibilitySpan 5.0, *) + @available(SwiftStdlib 6.2, *) @_alwaysEmitIntoClient @inline(__always) public mutating func insert< @@ -1093,34 +1216,116 @@ extension RigidArray where Element: ~Copyable { } extension RigidArray where Element: ~Copyable { + /// Replaces the specified range of elements by moving the elements of a + /// fully initialized buffer into their place. On return, the buffer is left + /// in an uninitialized state. + /// + /// This method has the effect of removing the specified range of elements + /// from the array and inserting the new elements starting at the same + /// location. The number of new elements need not match the number of elements + /// being removed. + /// + /// If the capacity of the array isn't sufficient to accommodate the new + /// elements, then this method triggers a runtime error. + /// + /// If you pass a zero-length range as the `subrange` parameter, this method + /// inserts the elements of `newElements` at `subrange.lowerBound`. Calling + /// the `insert(copying:at:)` method instead is preferred in this case. + /// + /// Likewise, if you pass a zero-length buffer as the `newElements` + /// parameter, this method removes the elements in the given subrange + /// without replacement. Calling the `removeSubrange(_:)` method instead is + /// preferred in this case. + /// + /// - Parameters + /// - subrange: The subrange of the array to replace. The bounds of + /// the range must be valid indices in the array. + /// - newElements: A fully initialized buffer whose contents to move into + /// the array. + /// + /// - Complexity: O(`self.count` + `newElements.count`) @_alwaysEmitIntoClient public mutating func replaceSubrange( _ subrange: Range, - moving items: UnsafeMutableBufferPointer, + moving newElements: UnsafeMutableBufferPointer, ) { let gap = unsafe _gapForReplacement( - of: subrange, withNewCount: items.count) - let c = unsafe gap._moveInitializePrefix(from: items) - assert(c == items.count) + of: subrange, withNewCount: newElements.count) + let c = unsafe gap._moveInitializePrefix(from: newElements) + assert(c == newElements.count) } + /// Replaces the specified range of elements by moving the elements of a + /// another array into their place. On return, the source array + /// becomes empty, but it is not destroyed, and it preserves its original + /// storage capacity. + /// + /// This method has the effect of removing the specified range of elements + /// from the array and inserting the new elements starting at the same + /// location. The number of new elements need not match the number of elements + /// being removed. + /// + /// If the capacity of the array isn't sufficient to accommodate the new + /// elements, then this method triggers a runtime error. + /// + /// If you pass a zero-length range as the `subrange` parameter, this method + /// inserts the elements of `newElements` at `subrange.lowerBound`. Calling + /// the `insert(copying:at:)` method instead is preferred in this case. + /// + /// Likewise, if you pass a zero-length buffer as the `newElements` + /// parameter, this method removes the elements in the given subrange + /// without replacement. Calling the `removeSubrange(_:)` method instead is + /// preferred in this case. + /// + /// - Parameters + /// - subrange: The subrange of the array to replace. The bounds of + /// the range must be valid indices in the array. + /// - newElements: An array whose contents to move into `self`. + /// + /// - Complexity: O(`self.count` + `newElements.count`) @_alwaysEmitIntoClient public mutating func replaceSubrange( _ subrange: Range, - moving items: inout RigidArray, + moving newElements: inout RigidArray, ) { - unsafe items.withUnsafeMutableBufferPointer { buffer, count in + unsafe newElements.withUnsafeMutableBufferPointer { buffer, count in unsafe self.replaceSubrange(subrange, moving: buffer) count = 0 } } + /// Replaces the specified range of elements by moving the elements of a + /// given array into their place, consuming it in the process. + /// + /// This method has the effect of removing the specified range of elements + /// from the array and inserting the new elements starting at the same + /// location. The number of new elements need not match the number of elements + /// being removed. + /// + /// If the capacity of the array isn't sufficient to accommodate the new + /// elements, then this method triggers a runtime error. + /// + /// If you pass a zero-length range as the `subrange` parameter, this method + /// inserts the elements of `newElements` at `subrange.lowerBound`. Calling + /// the `insert(copying:at:)` method instead is preferred in this case. + /// + /// Likewise, if you pass a zero-length buffer as the `newElements` + /// parameter, this method removes the elements in the given subrange + /// without replacement. Calling the `removeSubrange(_:)` method instead is + /// preferred in this case. + /// + /// - Parameters + /// - subrange: The subrange of the array to replace. The bounds of + /// the range must be valid indices in the array. + /// - newElements: An array whose contents to move into `self`. + /// + /// - Complexity: O(`self.count` + `newElements.count`) @_alwaysEmitIntoClient public mutating func replaceSubrange( _ subrange: Range, - consuming items: consuming RigidArray, + consuming newElements: consuming RigidArray, ) { - replaceSubrange(subrange, moving: &items) + replaceSubrange(subrange, moving: &newElements) } } @@ -1150,8 +1355,7 @@ extension RigidArray { /// the range must be valid indices in the array. /// - newElements: The new elements to copy into the collection. /// - /// - Complexity: O(*n* + *m*), where *n* is count of this array and - /// *m* is the count of `newElements`. + /// - Complexity: O(`self.count` + `newElements.count`) @inlinable public mutating func replaceSubrange( _ subrange: Range, @@ -1188,8 +1392,7 @@ extension RigidArray { /// the range must be valid indices in the array. /// - newElements: The new elements to copy into the collection. /// - /// - Complexity: O(*n* + *m*), where *n* is count of this array and - /// *m* is the count of `newElements`. + /// - Complexity: O(`self.count` + `newElements.count`) @inlinable public mutating func replaceSubrange( _ subrange: Range, @@ -1225,9 +1428,8 @@ extension RigidArray { /// the range must be valid indices in the array. /// - newElements: The new elements to copy into the collection. /// - /// - Complexity: O(*n* + *m*), where *n* is count of this array and - /// *m* is the count of `newElements`. - @available(SwiftCompatibilitySpan 5.0, *) + /// - Complexity: O(`self.count` + `newElements.count`) + @available(SwiftStdlib 6.2, *) @inlinable public mutating func replaceSubrange( _ subrange: Range, @@ -1285,8 +1487,7 @@ extension RigidArray { /// the range must be valid indices in the array. /// - newElements: The new elements to copy into the collection. /// - /// - Complexity: O(*n* + *m*), where *n* is count of this array and - /// *m* is the count of `newElements`. + /// - Complexity: O(`self.count` + `newElements.count`) @inlinable @inline(__always) public mutating func replaceSubrange( @@ -1297,7 +1498,7 @@ extension RigidArray { subrange, copyingCollection: newElements, newCount: newElements.count) } - @available(SwiftCompatibilitySpan 5.0, *) + @available(SwiftStdlib 6.2, *) @inlinable public mutating func _replaceSubrange< C: Container & ~Copyable & ~Escapable @@ -1338,9 +1539,8 @@ extension RigidArray { /// the range must be valid indices in the array. /// - newElements: The new elements to copy into the collection. /// - /// - Complexity: O(*n* + *m*), where *n* is count of this array and - /// *m* is the count of `newElements`. - @available(SwiftCompatibilitySpan 5.0, *) + /// - Complexity: O(`self.count` + `newElements.count`) + @available(SwiftStdlib 6.2, *) @inlinable @inline(__always) public mutating func replaceSubrange< @@ -1380,7 +1580,7 @@ extension RigidArray { /// /// - Complexity: O(*n* + *m*), where *n* is count of this array and /// *m* is the count of `newElements`. - @available(SwiftCompatibilitySpan 5.0, *) + @available(SwiftStdlib 6.2, *) @inlinable @inline(__always) public mutating func replaceSubrange< diff --git a/Sources/Future/Containers/BidirectionalContainer.swift b/Sources/Future/Containers/BidirectionalContainer.swift index f22fbefa..401658a9 100644 --- a/Sources/Future/Containers/BidirectionalContainer.swift +++ b/Sources/Future/Containers/BidirectionalContainer.swift @@ -9,7 +9,7 @@ // //===----------------------------------------------------------------------===// -@available(SwiftCompatibilitySpan 5.0, *) +@available(SwiftStdlib 6.2, *) public protocol BidirectionalContainer: Container, ~Copyable, ~Escapable { func index(before i: Index) -> Index func formIndex(before i: inout Index) @@ -18,7 +18,7 @@ public protocol BidirectionalContainer: Container, ~Copyable, ~Escapabl func previousSpan(before index: inout Index) -> Span } -@available(SwiftCompatibilitySpan 5.0, *) +@available(SwiftStdlib 6.2, *) extension BidirectionalContainer where Self: ~Copyable & ~Escapable { @inlinable @lifetime(borrow self) diff --git a/Sources/Future/Containers/Container.swift b/Sources/Future/Containers/Container.swift index f8b997fa..948292b9 100644 --- a/Sources/Future/Containers/Container.swift +++ b/Sources/Future/Containers/Container.swift @@ -9,7 +9,7 @@ // //===----------------------------------------------------------------------===// -@available(SwiftCompatibilitySpan 5.0, *) +@available(SwiftStdlib 6.2, *) public protocol Container: ~Copyable, ~Escapable { associatedtype Element: ~Copyable/* & ~Escapable*/ associatedtype Index: Comparable @@ -30,7 +30,7 @@ public protocol Container: ~Copyable, ~Escapable { limitedBy limit: Index ) - #if compiler(>=9999) // FIXME: We can't do this yet + #if compiler(>=9999) // FIXME: Needs borrow accessors subscript(index: Index) -> Element { borrow } #else @lifetime(borrow self) @@ -83,7 +83,7 @@ public protocol Container: ~Copyable, ~Escapable { func _customLastIndexOfEquatableElement(_ element: borrowing Element) -> Index?? } -@available(SwiftCompatibilitySpan 5.0, *) +@available(SwiftStdlib 6.2, *) extension Container where Self: ~Copyable & ~Escapable { @inlinable public func _defaultIndex(_ i: Index, advancedBy distance: Int) -> Index { @@ -171,7 +171,7 @@ extension Container where Self: ~Copyable & ~Escapable { } } -@available(SwiftCompatibilitySpan 5.0, *) +@available(SwiftStdlib 6.2, *) extension Container where Self: ~Copyable & ~Escapable { @inlinable public var isEmpty: Bool { @@ -215,13 +215,13 @@ extension Container where Self: ~Copyable & ~Escapable { public func _customLastIndexOfEquatableElement(_ element: borrowing Element) -> Index?? { nil } } -@available(SwiftCompatibilitySpan 5.0, *) +@available(SwiftStdlib 6.2, *) extension Container where Self: Sequence { @inlinable public var underestimatedCount: Int { count } } -@available(SwiftCompatibilitySpan 5.0, *) +@available(SwiftStdlib 6.2, *) extension Container where Self: Collection { // Resolve ambiguities between default implementations between Collection // and Container. @@ -256,7 +256,7 @@ extension Container where Self: Collection { public func _customLastIndexOfEquatableElement(_ element: borrowing Element) -> Index?? { nil } } -@available(SwiftCompatibilitySpan 5.0, *) +@available(SwiftStdlib 6.2, *) extension Container where Self: ~Copyable & ~Escapable { @inlinable public subscript(index: Index) -> Element { @@ -269,7 +269,7 @@ extension Container where Self: ~Copyable & ~Escapable { } } -@available(SwiftCompatibilitySpan 5.0, *) +@available(SwiftStdlib 6.2, *) extension Container where Self: ~Copyable & ~Escapable { @inlinable @lifetime(borrow self) @@ -287,7 +287,7 @@ extension Container where Self: ~Copyable & ~Escapable { #if false // DEMO -@available(SwiftCompatibilitySpan 5.0, *) +@available(SwiftStdlib 6.2, *) extension Container where Self: ~Copyable & ~Escapable { // This is just to demo the bulk iteration model func forEachSpan(_ body: (Span) throws(E) -> Void) throws(E) { diff --git a/Sources/Future/Containers/ContainerAlgorithms.swift b/Sources/Future/Containers/ContainerAlgorithms.swift index 14543889..d1c97467 100644 --- a/Sources/Future/Containers/ContainerAlgorithms.swift +++ b/Sources/Future/Containers/ContainerAlgorithms.swift @@ -11,7 +11,7 @@ //MARK: - firstIndex(where:) -@available(SwiftCompatibilitySpan 5.0, *) +@available(SwiftStdlib 6.2, *) extension Container where Self: ~Copyable & ~Escapable { @inlinable internal func _containerFirstIndex( @@ -55,7 +55,7 @@ extension Container where Self: ~Copyable & ~Escapable { } } -@available(SwiftCompatibilitySpan 5.0, *) +@available(SwiftStdlib 6.2, *) extension Container where Self: Collection { /// Returns the first index that addresses an element of the container that /// satisfies the given predicate. @@ -83,7 +83,7 @@ extension Container where Self: Collection { //MARK: - firstIndex(of:) -@available(SwiftCompatibilitySpan 5.0, *) +@available(SwiftStdlib 6.2, *) extension Container where Element: Equatable { @inlinable internal func _containerFirstIndex(of element: Element) -> Index? { @@ -110,7 +110,7 @@ extension Container where Element: Equatable { } } -@available(SwiftCompatibilitySpan 5.0, *) +@available(SwiftStdlib 6.2, *) extension Container where Self: Collection, Element: Equatable { /// Returns the first index where the specified value appears in the container. /// @@ -131,7 +131,7 @@ extension Container where Self: Collection, Element: Equatable { //MARK: - lastIndex(where:) -@available(SwiftCompatibilitySpan 5.0, *) +@available(SwiftStdlib 6.2, *) extension BidirectionalContainer where Self: ~Copyable & ~Escapable { @inlinable internal func _containerLastIndex( @@ -175,7 +175,7 @@ extension BidirectionalContainer where Self: ~Copyable & ~Escapable { } } -@available(SwiftCompatibilitySpan 5.0, *) +@available(SwiftStdlib 6.2, *) extension BidirectionalContainer where Self: BidirectionalCollection { /// Returns the first index that addresses an element of the container that /// satisfies the given predicate. @@ -203,7 +203,7 @@ extension BidirectionalContainer where Self: BidirectionalCollection { //MARK: - lastIndex(of:) -@available(SwiftCompatibilitySpan 5.0, *) +@available(SwiftStdlib 6.2, *) extension BidirectionalContainer where Element: Equatable { @inlinable internal func _containerLastIndex(of element: Element) -> Index? { @@ -230,7 +230,7 @@ extension BidirectionalContainer where Element: Equatable { } } -@available(SwiftCompatibilitySpan 5.0, *) +@available(SwiftStdlib 6.2, *) extension BidirectionalContainer where Self: BidirectionalCollection, Element: Equatable { /// Returns the first index where the specified value appears in the container. /// @@ -251,7 +251,7 @@ extension BidirectionalContainer where Self: BidirectionalCollection, Element: E //MARK: - elementsEqual(_:,by:) -@available(SwiftCompatibilitySpan 5.0, *) +@available(SwiftStdlib 6.2, *) extension Container where Self: ~Copyable & ~Escapable { @inlinable internal func _spanwiseZip( @@ -345,7 +345,7 @@ extension Container where Self: ~Copyable & ~Escapable { } } -@available(SwiftCompatibilitySpan 5.0, *) +@available(SwiftStdlib 6.2, *) extension Container where Self: Sequence { /// Returns a Boolean value indicating whether this container and another /// container contain equivalent elements in the same order, using the given @@ -379,7 +379,7 @@ extension Container where Self: Sequence { //MARK: - elementsEqual(_:) -@available(SwiftCompatibilitySpan 5.0, *) +@available(SwiftStdlib 6.2, *) extension Container where Self: ~Copyable & ~Escapable, Element: Equatable { /// Returns a Boolean value indicating whether this container and another /// container contain the same elements in the same order. @@ -398,7 +398,7 @@ extension Container where Self: ~Copyable & ~Escapable, Element: Equatable { } } -@available(SwiftCompatibilitySpan 5.0, *) +@available(SwiftStdlib 6.2, *) extension Container where Self: Sequence, Element: Equatable { /// Returns a Boolean value indicating whether this container and another /// container contain the same elements in the same order. diff --git a/Sources/Future/Containers/MutableContainer.swift b/Sources/Future/Containers/MutableContainer.swift index 05197d4b..368f7f53 100644 --- a/Sources/Future/Containers/MutableContainer.swift +++ b/Sources/Future/Containers/MutableContainer.swift @@ -9,17 +9,26 @@ // //===----------------------------------------------------------------------===// -@available(SwiftCompatibilitySpan 5.0, *) +@available(SwiftStdlib 6.2, *) public protocol MutableContainer: Container, ~Copyable, ~Escapable { -#if compiler(>=9999) // FIXME: We can't do this yet +#if compiler(>=9999) // FIXME: Needs borrow accessors subscript(index: Index) -> Element { borrow mutate } #else + +#if compiler(>=6.2) && $InoutLifetimeDependence @lifetime(&self) +#else + @lifetime(borrow self) +#endif @lifetime(self: copy self) mutating func mutateElement(at index: Index) -> Inout #endif +#if compiler(>=6.2) && $InoutLifetimeDependence @lifetime(&self) +#else + @lifetime(borrow self) +#endif @lifetime(self: copy self) mutating func nextMutableSpan(after index: inout Index) -> MutableSpan @@ -29,39 +38,86 @@ public protocol MutableContainer: Container, ~Copyable, ~Escapable { mutating func swapAt(_ i: Index, _ j: Index) } -@available(SwiftCompatibilitySpan 5.0, *) +@available(SwiftStdlib 6.2, *) extension MutableContainer where Self: ~Copyable & ~Escapable { - #if false // Hm... +#if false // FIXME: This has flagrant exclusivity violations. +#if compiler(>=6.2) && $InoutLifetimeDependence @lifetime(&self) - mutating func nextMutableSpan( +#else + @lifetime(borrow self) +#endif + public mutating func nextMutableSpan( after index: inout Index, maximumCount: Int ) -> MutableSpan { - let original = index - var span = self.nextMutableSpan(after: &index) - if span.count > maximumCount { - span = span.extracting(first: maximumCount) - // Index remains within the same span, so offseting it is expected to be quick - index = self.index(original, offsetBy: maximumCount) + let start = index + do { + let span = self.nextMutableSpan(after: &index) + guard span.count > maximumCount else { return span } } - return span + // Index remains within the same span, so offseting it is expected to be quick + let end = self.index(start, offsetBy: maximumCount) + index = start + var span = self.nextMutableSpan(after: &index) + let extract = span.extracting(first: maximumCount) // FIXME: Oops + index = end + return extract } - #endif +#endif + @lifetime(self: copy self) + public mutating func withNextMutableSpan< + E: Error, R: ~Copyable + >( + after index: inout Index, maximumCount: Int, + body: (inout MutableSpan) throws(E) -> R + ) throws(E) -> R { + // FIXME: We don't want this to be closure-based, but MutableSpan cannot be sliced "in place". + let start = index + do { + var span = self.nextMutableSpan(after: &index) + if span.count <= maximumCount { + return try body(&span) + } + } + // Try again, but figure out what our target index is first. + // Index remains within the same span, so offseting it is expected to be quick + index = self.index(start, offsetBy: maximumCount) + var i = start + var span = self.nextMutableSpan(after: &i) + var extract = span.extracting(first: maximumCount) + return try body(&extract) + } + +#if compiler(>=6.2) && $InoutLifetimeDependence @inlinable public subscript(index: Index) -> Element { @lifetime(borrow self) unsafeAddress { unsafe borrowElement(at: index)._pointer } - + @lifetime(&self) unsafeMutableAddress { unsafe mutateElement(at: index)._pointer } } +#else + @inlinable + public subscript(index: Index) -> Element { + @lifetime(borrow self) + unsafeAddress { + unsafe borrowElement(at: index)._pointer + } + + @lifetime(borrow self) + unsafeMutableAddress { + unsafe mutateElement(at: index)._pointer + } + } +#endif } -@available(SwiftCompatibilitySpan 5.0, *) +@available(SwiftStdlib 6.2, *) extension MutableContainer where Self: ~Copyable & ~Escapable { /// Moves all elements satisfying `isSuffixElement` into a suffix of the /// container, returning the start position of the resulting suffix. diff --git a/Sources/Future/Containers/RandomAccessContainer.swift b/Sources/Future/Containers/RandomAccessContainer.swift index 7caf2094..7da0a0c7 100644 --- a/Sources/Future/Containers/RandomAccessContainer.swift +++ b/Sources/Future/Containers/RandomAccessContainer.swift @@ -9,11 +9,11 @@ // //===----------------------------------------------------------------------===// -@available(SwiftCompatibilitySpan 5.0, *) +@available(SwiftStdlib 6.2, *) public protocol RandomAccessContainer : BidirectionalContainer, ~Copyable, ~Escapable {} -@available(SwiftCompatibilitySpan 5.0, *) +@available(SwiftStdlib 6.2, *) extension RandomAccessContainer where Self: ~Copyable & ~Escapable, Index: Strideable, Index.Stride == Int { @@ -83,7 +83,7 @@ where Self: ~Copyable & ~Escapable, Index: Strideable, Index.Stride == Int // Disambiguate parallel extensions on RandomAccessContainer and RandomAccessCollection -@available(SwiftCompatibilitySpan 5.0, *) +@available(SwiftStdlib 6.2, *) extension RandomAccessCollection where Self: RandomAccessContainer, diff --git a/Sources/Future/Containers/RepeatingContainer.swift b/Sources/Future/Containers/RepeatingContainer.swift index 9285f90e..712dbdca 100644 --- a/Sources/Future/Containers/RepeatingContainer.swift +++ b/Sources/Future/Containers/RepeatingContainer.swift @@ -53,7 +53,7 @@ extension RepeatingContainer where Element: ~Copyable { } #if false // FIXME: This is what we'd want -@available(SwiftCompatibilitySpan 5.0, *) +@available(SwiftStdlib 6.2, *) extension RepeatingContainer: RandomAccessContainer where Element: ~Copyable { @lifetime(borrow self) public func borrowElement(at index: Int) -> Borrow { @@ -85,7 +85,7 @@ extension RepeatingContainer: RandomAccessContainer where Element: ~Copyable { } } #else -@available(SwiftCompatibilitySpan 5.0, *) +@available(SwiftStdlib 6.2, *) extension RepeatingContainer: RandomAccessContainer where Element: ~Copyable { @lifetime(borrow self) public func borrowElement(at index: Int) -> Borrow { diff --git a/Sources/Future/Span/LifetimeOverride.swift b/Sources/Future/Span/LifetimeOverride.swift index e6fc96ab..8faa9e6c 100644 --- a/Sources/Future/Span/LifetimeOverride.swift +++ b/Sources/Future/Span/LifetimeOverride.swift @@ -53,7 +53,11 @@ public func _overrideLifetime< @_unsafeNonescapableResult @_alwaysEmitIntoClient @_transparent +#if compiler(>=6.2) && $InoutLifetimeDependence @lifetime(&source) +#else +@lifetime(borrow source) +#endif public func _overrideLifetime< T: ~Copyable & ~Escapable, U: ~Copyable & ~Escapable diff --git a/Sources/Future/Span/OutputSpan.swift b/Sources/Future/Span/OutputSpan.swift index b54ea24f..bf4fe657 100644 --- a/Sources/Future/Span/OutputSpan.swift +++ b/Sources/Future/Span/OutputSpan.swift @@ -363,6 +363,7 @@ extension OutputSpan where Element: ~Copyable { } } +#if compiler(>=6.2) && $InoutLifetimeDependence @_alwaysEmitIntoClient public var mutableSpan: MutableSpan { @lifetime(&self) @@ -375,6 +376,20 @@ extension OutputSpan where Element: ~Copyable { return unsafe _overrideLifetime(span, mutating: &self) } } +#else + @_alwaysEmitIntoClient + public var mutableSpan: MutableSpan { + @lifetime(borrow self) + mutating get { + let pointer = unsafe _pointer?.assumingMemoryBound(to: Element.self) + let buffer = unsafe UnsafeMutableBufferPointer( + start: pointer, count: _initialized + ) + let span = unsafe MutableSpan(_unsafeElements: buffer) + return unsafe _overrideLifetime(span, mutating: &self) + } + } +#endif } @available(macOS 9999, *) diff --git a/Sources/Future/Span/SpanExtensions.swift b/Sources/Future/Span/SpanExtensions.swift index f8ea9fb5..a1af1045 100644 --- a/Sources/Future/Span/SpanExtensions.swift +++ b/Sources/Future/Span/SpanExtensions.swift @@ -13,7 +13,7 @@ @usableFromInline internal let immortalThing: [Void] = [] -@available(SwiftCompatibilitySpan 5.0, *) +@available(SwiftStdlib 6.2, *) extension Span where Element: ~Copyable { public static var empty: Span { @lifetime(immortal) @@ -32,7 +32,7 @@ extension Span where Element: ~Copyable { } } -@available(SwiftCompatibilitySpan 5.0, *) +@available(SwiftStdlib 6.2, *) extension Span where Element: Equatable { /// Returns a Boolean value indicating whether this and another span /// contain equal elements in the same order. diff --git a/Sources/Future/Span/UnsafeBufferPointer+Additions.swift b/Sources/Future/Span/UnsafeBufferPointer+Additions.swift index 860400d5..020eb6ee 100644 --- a/Sources/Future/Span/UnsafeBufferPointer+Additions.swift +++ b/Sources/Future/Span/UnsafeBufferPointer+Additions.swift @@ -96,7 +96,7 @@ extension UnsafeMutableBufferPointer { /// The `source` span must fit entirely in `self`. /// /// - Returns: The index after the last item that was initialized in this buffer. - @available(SwiftCompatibilitySpan 5.0, *) + @available(SwiftStdlib 6.2, *) @inlinable internal func _initializePrefix(copying source: Span) -> Int { unsafe source.withUnsafeBufferPointer { unsafe self._initializePrefix(copying: $0) } @@ -114,7 +114,7 @@ extension UnsafeMutableBufferPointer { /// /// - Returns: A pair of values `(count, end)`, where `count` is the number of items that were /// successfully initialized, and `end` is the index into `items` after the last copied item. - @available(SwiftCompatibilitySpan 5.0, *) + @available(SwiftStdlib 6.2, *) @inlinable internal func _initializePrefix< C: Container & ~Copyable & ~Escapable @@ -157,7 +157,7 @@ extension UnsafeMutableBufferPointer { /// entirely uninitialized. /// /// The count of `span` must not be greater than `self.count`. - @available(SwiftCompatibilitySpan 5.0, *) + @available(SwiftStdlib 6.2, *) @inlinable internal mutating func _initializeAndDropPrefix(copying span: Span) { unsafe span.withUnsafeBufferPointer { buffer in diff --git a/Sources/Future/Tools/Box.swift b/Sources/Future/Tools/Box.swift index 5f636c86..5b94e655 100644 --- a/Sources/Future/Tools/Box.swift +++ b/Sources/Future/Tools/Box.swift @@ -40,9 +40,9 @@ extension Box where T: ~Copyable { return result } - @lifetime(immortal) @_alwaysEmitIntoClient @_transparent + @lifetime(immortal) public consuming func leak() -> Inout { let result = unsafe Inout(unsafeImmortalAddress: _pointer) discard self @@ -62,16 +62,20 @@ extension Box where T: ~Copyable { } } - @lifetime(borrow self) @_alwaysEmitIntoClient @_transparent + @lifetime(borrow self) public func borrow() -> Borrow { unsafe Borrow(unsafeAddress: UnsafePointer(_pointer), borrowing: self) } - @lifetime(&self) @_alwaysEmitIntoClient @_transparent +#if compiler(>=6.2) && $InoutLifetimeDependence + @lifetime(&self) +#else + @lifetime(borrow self) +#endif public mutating func mutate() -> Inout { unsafe Inout(unsafeAddress: _pointer, mutating: &self) } @@ -86,7 +90,7 @@ extension Box where T: Copyable { } extension Box where T: ~Copyable { - @available(SwiftCompatibilitySpan 5.0, *) + @available(SwiftStdlib 6.2, *) public var span: Span { @_alwaysEmitIntoClient @lifetime(borrow self) @@ -95,7 +99,8 @@ extension Box where T: ~Copyable { } } - @available(SwiftCompatibilitySpan 5.0, *) +#if compiler(>=6.2) && $InoutLifetimeDependence + @available(SwiftStdlib 6.2, *) public var mutableSpan: MutableSpan { @_alwaysEmitIntoClient @lifetime(&self) @@ -103,4 +108,14 @@ extension Box where T: ~Copyable { unsafe MutableSpan(_unsafeStart: _pointer, count: 1) } } +#else + @available(SwiftStdlib 6.2, *) + public var mutableSpan: MutableSpan { + @_alwaysEmitIntoClient + @lifetime(borrow self) + mutating get { + unsafe MutableSpan(_unsafeStart: _pointer, count: 1) + } + } +#endif } diff --git a/Sources/Future/Tools/Inout.swift b/Sources/Future/Tools/Inout.swift index b4e7a578..9c06f9aa 100644 --- a/Sources/Future/Tools/Inout.swift +++ b/Sources/Future/Tools/Inout.swift @@ -41,10 +41,14 @@ public struct Inout: ~Copyable, ~Escapable { /// instance of type 'T'. /// - Parameter owner: The owning instance that this 'Inout' instance's /// lifetime is based on. - @lifetime(&owner) @unsafe @_alwaysEmitIntoClient @_transparent +#if compiler(>=6.2) && $InoutLifetimeDependence + @lifetime(&owner) +#else + @lifetime(borrow owner) +#endif public init( unsafeAddress: UnsafeMutablePointer, mutating owner: inout Owner diff --git a/Sources/Future/Tools/Shared.swift b/Sources/Future/Tools/Shared.swift index 7eaaee1c..1baf0d3e 100644 --- a/Sources/Future/Tools/Shared.swift +++ b/Sources/Future/Tools/Shared.swift @@ -109,6 +109,7 @@ extension Shared where Storage: ~Copyable { } extension Shared where Storage: ~Copyable { +#if compiler(>=6.2) && $InoutLifetimeDependence @inlinable @inline(__always) public var value: Storage { @@ -122,6 +123,21 @@ extension Shared where Storage: ~Copyable { return unsafe _mutableAddress } } +#else + @inlinable + @inline(__always) + public var value: Storage { + @lifetime(borrow self) + unsafeAddress { + unsafe _address + } + @lifetime(borrow self) + unsafeMutableAddress { + precondition(isUnique()) + return unsafe _mutableAddress + } + } +#endif } extension Shared where Storage: ~Copyable { @@ -133,9 +149,14 @@ extension Shared where Storage: ~Copyable { // lifetime(self) == lifetime(_box.storage). unsafe Borrow(unsafeAddress: _address, borrowing: self) } - + + @inlinable +#if compiler(>=6.2) && $InoutLifetimeDependence @lifetime(&self) +#else + @lifetime(borrow self) +#endif public mutating func mutate() -> Inout { // This is gloriously (and very explicitly) unsafe, as it should be. // `Shared` is carefully constructed to guarantee that diff --git a/Tests/CollectionsTestSupportTests/MinimalTypeConformances.swift b/Tests/CollectionsTestSupportTests/MinimalTypeConformances.swift index f088532b..6251d463 100644 --- a/Tests/CollectionsTestSupportTests/MinimalTypeConformances.swift +++ b/Tests/CollectionsTestSupportTests/MinimalTypeConformances.swift @@ -47,7 +47,7 @@ final class MinimalTypeTests: CollectionTestCase { #if false // FIXME: Track down compiler crash func testTestContainer() { - guard #available(SwiftCompatibilitySpan 5.0, *) else { return } + guard #available(SwiftStdlib 6.2, *) else { return } let a = TestContainer( contents: RigidArray(count: 100, initializedWith: { $0 }), spanCounts: [3, 1, 7]) diff --git a/Tests/CollectionsTestSupportTests/StaccatoContainerTests.swift b/Tests/CollectionsTestSupportTests/StaccatoContainerTests.swift index f121d41a..2e8fa873 100644 --- a/Tests/CollectionsTestSupportTests/StaccatoContainerTests.swift +++ b/Tests/CollectionsTestSupportTests/StaccatoContainerTests.swift @@ -13,6 +13,7 @@ import XCTest import _CollectionsTestSupport import Future +@available(SwiftStdlib 6.2, *) class StaccatoContainerTests: CollectionTestCase { func checkStriding( _ container: borrowing C, diff --git a/Tests/FutureTests/ArrayTests/DynamicArrayTests.swift b/Tests/FutureTests/ArrayTests/DynamicArrayTests.swift index e45f14c6..2610f534 100644 --- a/Tests/FutureTests/ArrayTests/DynamicArrayTests.swift +++ b/Tests/FutureTests/ArrayTests/DynamicArrayTests.swift @@ -14,6 +14,7 @@ import _CollectionsTestSupport import Future import Synchronization +@available(SwiftStdlib 6.2, *) class DynamicArrayTests: CollectionTestCase { func test_validate_Container() { withSomeArrayLayouts("layout", ofCapacities: [0, 10, 100]) { layout in diff --git a/Tests/FutureTests/ArrayTests/RigidArrayTests.swift b/Tests/FutureTests/ArrayTests/RigidArrayTests.swift index 171f6305..056d5ddf 100644 --- a/Tests/FutureTests/ArrayTests/RigidArrayTests.swift +++ b/Tests/FutureTests/ArrayTests/RigidArrayTests.swift @@ -13,7 +13,7 @@ import XCTest import _CollectionsTestSupport import Future -@available(SwiftStdlib 6.0, *) +@available(SwiftStdlib 6.2, *) class RigidArrayTests: CollectionTestCase { func test_validate_Container() { withSomeArrayLayouts("layout", ofCapacities: [0, 10, 100]) { layout in diff --git a/Tests/FutureTests/ContiguousStorageTests.swift b/Tests/FutureTests/ContiguousStorageTests.swift index 1862794f..7d517469 100644 --- a/Tests/FutureTests/ContiguousStorageTests.swift +++ b/Tests/FutureTests/ContiguousStorageTests.swift @@ -12,6 +12,7 @@ import XCTest import Future +@available(SwiftStdlib 6.2, *) final class ContiguousStorageTests: XCTestCase { @available(SwiftStdlib 6.2, *) diff --git a/Tests/_CollectionsTestSupport/AssertionContexts/Assertions+Containers.swift b/Tests/_CollectionsTestSupport/AssertionContexts/Assertions+Containers.swift index 6b66bac8..81816702 100644 --- a/Tests/_CollectionsTestSupport/AssertionContexts/Assertions+Containers.swift +++ b/Tests/_CollectionsTestSupport/AssertionContexts/Assertions+Containers.swift @@ -12,7 +12,7 @@ import Future -@available(SwiftCompatibilitySpan 5.0, *) +@available(SwiftStdlib 6.2, *) public func expectContainersWithEquivalentElements< C1: Container & ~Copyable & ~Escapable, C2: Container & ~Copyable & ~Escapable @@ -32,7 +32,7 @@ public func expectContainersWithEquivalentElements< } /// Check if `left` and `right` contain equal elements in the same order. -@available(SwiftCompatibilitySpan 5.0, *) +@available(SwiftStdlib 6.2, *) public func expectContainersWithEqualElements< Element: Equatable, C1: Container & ~Copyable & ~Escapable, @@ -52,7 +52,7 @@ public func expectContainersWithEqualElements< } /// Check if `left` and `right` contain equal elements in the same order. -@available(SwiftCompatibilitySpan 5.0, *) +@available(SwiftStdlib 6.2, *) public func expectContainerContents< Element: Equatable, C1: Container & ~Copyable & ~Escapable, @@ -92,7 +92,7 @@ public func expectContainerContents< } /// Check if `left` and `right` contain equal elements in the same order. -@available(SwiftCompatibilitySpan 5.0, *) +@available(SwiftStdlib 6.2, *) public func expectContainerContents< C1: Container & ~Copyable & ~Escapable, C2: Collection, diff --git a/Tests/_CollectionsTestSupport/ConformanceCheckers/CheckContainer.swift b/Tests/_CollectionsTestSupport/ConformanceCheckers/CheckContainer.swift index 1e4b58b2..335bd4fb 100644 --- a/Tests/_CollectionsTestSupport/ConformanceCheckers/CheckContainer.swift +++ b/Tests/_CollectionsTestSupport/ConformanceCheckers/CheckContainer.swift @@ -12,7 +12,7 @@ import XCTest import Future -@available(SwiftCompatibilitySpan 5.0, *) +@available(SwiftStdlib 6.2, *) extension Container where Self: ~Copyable & ~Escapable { @inlinable internal func _indicesByIndexAfter() -> [Index] { @@ -37,7 +37,7 @@ extension Container where Self: ~Copyable & ~Escapable { } } -@available(SwiftCompatibilitySpan 5.0, *) +@available(SwiftStdlib 6.2, *) @inlinable public func checkContainer< C: Container & ~Copyable & ~Escapable, @@ -55,7 +55,7 @@ public func checkContainer< file: file, line: line) } -@available(SwiftCompatibilitySpan 5.0, *) +@available(SwiftStdlib 6.2, *) @inlinable public func checkContainer< C: Container & ~Copyable & ~Escapable, diff --git a/Tests/_CollectionsTestSupport/MinimalTypes/StaccatoContainer.swift b/Tests/_CollectionsTestSupport/MinimalTypes/StaccatoContainer.swift index eb2f3ca1..ae3517be 100644 --- a/Tests/_CollectionsTestSupport/MinimalTypes/StaccatoContainer.swift +++ b/Tests/_CollectionsTestSupport/MinimalTypes/StaccatoContainer.swift @@ -80,7 +80,7 @@ extension StaccatoContainer where Element: ~Copyable { } } -@available(SwiftCompatibilitySpan 5.0, *) +@available(SwiftStdlib 6.2, *) extension StaccatoContainer: Container where Element: ~Copyable { public func formIndex(_ i: inout Index, offsetBy distance: inout Int, limitedBy limit: Index) { precondition(i >= startIndex && i <= endIndex) From 6385ebbe4d5fd7235dca833a4b041b14f06d46cf Mon Sep 17 00:00:00 2001 From: Karoy Lorentey Date: Fri, 2 May 2025 18:23:22 -0700 Subject: [PATCH 2/2] More workarounds for crashes in older 6.2 compiler builds --- Sources/Future/Containers/ContiguousContainer.swift | 2 ++ Sources/Future/Containers/MutableContainer.swift | 2 ++ 2 files changed, 4 insertions(+) diff --git a/Sources/Future/Containers/ContiguousContainer.swift b/Sources/Future/Containers/ContiguousContainer.swift index 7f966a16..9f25b2b3 100644 --- a/Sources/Future/Containers/ContiguousContainer.swift +++ b/Sources/Future/Containers/ContiguousContainer.swift @@ -48,8 +48,10 @@ where Self: ~Copyable & ~Escapable, Index == Int { @available(SwiftStdlib 6.2, *) extension Array: ContiguousContainer {} +#if compiler(>=6.2) && $InoutLifetimeDependence // FIXME: Crashes older 6.2 compilers. @available(SwiftStdlib 6.2, *) extension ContiguousArray: ContiguousContainer {} +#endif @available(SwiftStdlib 6.2, *) extension CollectionOfOne: ContiguousContainer {} diff --git a/Sources/Future/Containers/MutableContainer.swift b/Sources/Future/Containers/MutableContainer.swift index 368f7f53..ee846f24 100644 --- a/Sources/Future/Containers/MutableContainer.swift +++ b/Sources/Future/Containers/MutableContainer.swift @@ -64,6 +64,7 @@ extension MutableContainer where Self: ~Copyable & ~Escapable { } #endif +#if compiler(>=6.2) && $InoutLifetimeDependence // FIXME: Crashes older 6.2 compilers. @lifetime(self: copy self) public mutating func withNextMutableSpan< E: Error, R: ~Copyable @@ -87,6 +88,7 @@ extension MutableContainer where Self: ~Copyable & ~Escapable { var extract = span.extracting(first: maximumCount) return try body(&extract) } +#endif #if compiler(>=6.2) && $InoutLifetimeDependence @inlinable