diff --git a/Package.swift b/Package.swift index 263a4918..44656da0 100644 --- a/Package.swift +++ b/Package.swift @@ -116,7 +116,7 @@ let extraSettings: [SwiftSetting] = [ .enableExperimentalFeature("AddressableParameters"), .enableExperimentalFeature("AddressableTypes"), .enableExperimentalFeature("SuppressedAssociatedTypesWithDefaults"), // Requires Swift 6.4 - + .enableExperimentalFeature("BorrowAndMutateAccessors"), // Note: if you touch these, please make sure to also update the similar lists in // CMakeLists.txt and Xcode/Shared.xcconfig. ] diff --git a/Sources/BasicContainers/RigidArray/RigidArray+Container.swift b/Sources/BasicContainers/RigidArray/RigidArray+Container.swift index 3ae8cc59..c46aa70e 100644 --- a/Sources/BasicContainers/RigidArray/RigidArray+Container.swift +++ b/Sources/BasicContainers/RigidArray/RigidArray+Container.swift @@ -41,7 +41,7 @@ extension RigidArray: BidirectionalContainer where Element: ~Copyable {} extension RigidArray: RandomAccessContainer where Element: ~Copyable {} #if compiler(>=6.4) -@available(SwiftStdlib 6.4, *) +@available(SwiftStdlib 5.0, *) extension RigidArray: MutableContainer where Element: ~Copyable {} @available(SwiftStdlib 6.4, *) @@ -153,12 +153,14 @@ extension RigidArray where Element: ~Copyable { @inlinable public subscript(position: Int) -> Element { @inline(__always) - unsafeAddress { - _ptr(to: position) + @_unsafeSelfDependentResult + borrow { + _ptr(to: position).pointee } @inline(__always) - unsafeMutableAddress { - _mutablePtr(to: position) + @_unsafeSelfDependentResult + mutate { + &_mutablePtr(to: position).pointee } } } diff --git a/Sources/BasicContainers/RigidSet/RigidSet+Index.swift b/Sources/BasicContainers/RigidSet/RigidSet+Index.swift index 5fe511f1..2ee7675a 100644 --- a/Sources/BasicContainers/RigidSet/RigidSet+Index.swift +++ b/Sources/BasicContainers/RigidSet/RigidSet+Index.swift @@ -100,10 +100,10 @@ extension RigidSet where Element: ~Copyable { @inlinable public subscript(index: Index) -> Element { - // FIXME: Use borrow accessor here - unsafeAddress { + @_unsafeSelfDependentResult + borrow { _checkItemIndex(index) - return .init(_memberPtr(at: index._bucket)) + return _memberPtr(at: index._bucket).pointee } } } diff --git a/Sources/BasicContainers/UniqueArray/UniqueArray+Container.swift b/Sources/BasicContainers/UniqueArray/UniqueArray+Container.swift index c314324e..0483039b 100644 --- a/Sources/BasicContainers/UniqueArray/UniqueArray+Container.swift +++ b/Sources/BasicContainers/UniqueArray/UniqueArray+Container.swift @@ -114,12 +114,13 @@ extension UniqueArray where Element: ~Copyable { @inlinable public subscript(position: Int) -> Element { @inline(__always) - unsafeAddress { - _storage._ptr(to: position) + borrow { + _storage[position] } @inline(__always) - unsafeMutableAddress { - _storage._mutablePtr(to: position) + @_unsafeSelfDependentResult // FIXME: Why is this necessary? + mutate { + &_storage[position] } } } diff --git a/Sources/BasicContainers/UniqueSet/UniqueSet+Index.swift b/Sources/BasicContainers/UniqueSet/UniqueSet+Index.swift index 4dc1f451..2d48fb6d 100644 --- a/Sources/BasicContainers/UniqueSet/UniqueSet+Index.swift +++ b/Sources/BasicContainers/UniqueSet/UniqueSet+Index.swift @@ -59,10 +59,8 @@ extension UniqueSet where Element: ~Copyable { @inlinable public subscript(index: Index) -> Element { - // FIXME: Use borrow accessor here - unsafeAddress { - _checkItemIndex(index) - return .init(_storage._memberPtr(at: index._bucket)) + borrow { + _storage[index] } } } diff --git a/Sources/ContainersPreview/Protocols/Container/Container.swift b/Sources/ContainersPreview/Protocols/Container/Container.swift index 199aa591..e4d0e772 100644 --- a/Sources/ContainersPreview/Protocols/Container/Container.swift +++ b/Sources/ContainersPreview/Protocols/Container/Container.swift @@ -95,6 +95,18 @@ public protocol Container: /// - Returns: The distance between `start` and `end`. func distance(from start: Index, to end: Index) -> Int + /// Accesses the element at the specified position. + /// + /// You can subscript a collection with any valid index other than the + /// collection’s end index. The end index refers to the position one past the + /// last element of a collection, so it doesn’t correspond with an element. + /// + /// - Parameter position: The position of the element to access. + /// `position` must be a valid index of the container that is not equal + /// to the `endIndex` property. + /// - Complexity: O(1) + subscript(index: Index) -> Element { borrow } + /// Return a span over the container's storage that begins with the element at /// the given index, and extends to the end of the contiguous storage chunk /// that contains it, but no more than `maximumCount` items. @@ -165,8 +177,6 @@ public protocol Container: @_lifetime(borrow self) func nextSpan(after index: inout Index, maximumCount: Int) -> Span - // subscript(index: Index) -> Element { borrow } - /// Return the nearest valid index in this container less than or equal to /// the given index value, which must be valid in at least one view of self. /// diff --git a/Sources/ContainersPreview/Protocols/Container/MutableContainer.swift b/Sources/ContainersPreview/Protocols/Container/MutableContainer.swift index 06516f54..33bbc89c 100644 --- a/Sources/ContainersPreview/Protocols/Container/MutableContainer.swift +++ b/Sources/ContainersPreview/Protocols/Container/MutableContainer.swift @@ -19,7 +19,13 @@ public protocol MutableContainer: where Element: ~Copyable { - // subscript(index: Index) -> Element { borrow mutate } + /// Accesses the element at the specified position. + /// + /// - Parameter position: The position of the element to access. + /// `position` must be a valid index of the container that is not equal + /// to the `endIndex` property. + /// - Complexity: O(1) + subscript(index: Index) -> Element { borrow mutate } @_lifetime(&self) mutating func nextMutableSpan( diff --git a/Sources/ContainersPreview/Types/InputSpan.swift b/Sources/ContainersPreview/Types/InputSpan.swift index 5209b852..74afceb6 100644 --- a/Sources/ContainersPreview/Types/InputSpan.swift +++ b/Sources/ContainersPreview/Types/InputSpan.swift @@ -62,7 +62,7 @@ extension InputSpan where Element: ~Copyable { internal func _start() -> UnsafeMutableRawPointer { unsafe _pointer.unsafelyUnwrapped } - + @unsafe @_alwaysEmitIntoClient @_transparent @@ -177,7 +177,7 @@ extension InputSpan where Element: ~Copyable { capacity = buffer.count _count = initializedCount } - + @unsafe @_alwaysEmitIntoClient @_lifetime(borrow buffer) @@ -244,15 +244,17 @@ extension InputSpan where Element: ~Copyable { /// - Complexity: O(1) @_alwaysEmitIntoClient public subscript(_ index: Index) -> Element { - unsafeAddress { + @_unsafeSelfDependentResult + borrow { precondition(indices.contains(index), "Index out of bounds") - return unsafe UnsafePointer(_unsafeAddressOfElement(uncheckedOffset: index)) + return unsafe _unsafeAddressOfElement(uncheckedOffset: index).pointee } + @_unsafeSelfDependentResult @_lifetime(self: copy self) - unsafeMutableAddress { + mutate { precondition(indices.contains(index), "Index out of bounds") - return unsafe _unsafeAddressOfElement(uncheckedOffset: index) + return unsafe &_unsafeAddressOfElement(uncheckedOffset: index).pointee } } @@ -346,7 +348,7 @@ extension InputSpan where Element: ~Copyable { } _count &-= k } - + /// Removes and returns the first element of this input span, if it exists. /// /// - Returns: The first element of the original span if it wasn't empty; @@ -533,7 +535,7 @@ extension InputSpan where Element: ~Copyable { public mutating func consumePrefix(upTo n: Int) -> InputSpan { precondition(n >= 0, "Cannot consume a negative number of elements") let c = Swift.min(n, self.count) - + let buffer = unsafe _unsafeRawAddressOfSlot( uncheckedOffset: 0 ).withMemoryRebound(to: Element.self, capacity: c) { start in diff --git a/Sources/DequeModule/RigidDeque/RigidDeque.swift b/Sources/DequeModule/RigidDeque/RigidDeque.swift index 16c81eb3..a7e90e53 100644 --- a/Sources/DequeModule/RigidDeque/RigidDeque.swift +++ b/Sources/DequeModule/RigidDeque/RigidDeque.swift @@ -262,20 +262,21 @@ extension RigidDeque where Element: ~Copyable { @_alwaysEmitIntoClient public subscript(position: Int) -> Element { - // FIXME: Replace with borrow/mutate accessors @inline(__always) @_transparent - unsafeAddress { + @_unsafeSelfDependentResult + borrow { _checkItemIndex(position) let slot = _handle.slot(forOffset: position) - return _handle.ptr(at: slot) + return _handle.ptr(at: slot).pointee } @inline(__always) @_transparent - unsafeMutableAddress { + @_unsafeSelfDependentResult + mutate { _checkItemIndex(position) let slot = _handle.slot(forOffset: position) - return _handle.mutablePtr(at: slot) + return &_handle.mutablePtr(at: slot).pointee } } } diff --git a/Sources/DequeModule/UniqueDeque/UniqueDeque.swift b/Sources/DequeModule/UniqueDeque/UniqueDeque.swift index 9f5d8c19..5d26bc64 100644 --- a/Sources/DequeModule/UniqueDeque/UniqueDeque.swift +++ b/Sources/DequeModule/UniqueDeque/UniqueDeque.swift @@ -198,17 +198,14 @@ extension UniqueDeque where Element: ~Copyable { public subscript(position: Int) -> Element { @inline(__always) @_transparent - unsafeAddress { - precondition(position >= 0 && position < count, "Index out of bounds") - let slot = _storage._handle.slot(forOffset: position) - return _storage._handle.ptr(at: slot) + borrow { + _storage[position] } @inline(__always) @_transparent - unsafeMutableAddress { - precondition(position >= 0 && position < count, "Index out of bounds") - let slot = _storage._handle.slot(forOffset: position) - return _storage._handle.mutablePtr(at: slot) + @_unsafeSelfDependentResult // FIXME: Why is this necessary? + mutate { + &_storage[position] } } } diff --git a/Xcode/Shared.xcconfig b/Xcode/Shared.xcconfig index 51643e43..dd340ff7 100644 --- a/Xcode/Shared.xcconfig +++ b/Xcode/Shared.xcconfig @@ -22,6 +22,7 @@ OTHER_SWIFT_FLAGS = $(inherited) \ -enable-experimental-feature AddressableParameters \ -enable-experimental-feature AddressableTypes \ -enable-experimental-feature SuppressedAssociatedTypesWithDefaults \ + -enable-experimental-feature BorrowAndMutateAccessors \ -enable-experimental-feature "AvailabilityMacro=SwiftStdlib 5.0: macOS 10.14.4, iOS 12.2, watchOS 5.2, tvOS 12.2" \ -enable-experimental-feature "AvailabilityMacro=SwiftStdlib 5.1: macOS 10.15, iOS 13.0, watchOS 6.0, tvOS 13.0" \ -enable-experimental-feature "AvailabilityMacro=SwiftStdlib 5.6: macOS 12.3, iOS 15.4, watchOS 8.5, tvOS 15.4" \