Add subscript requirements to [Mutable]Container with borrow/mutate accessors

This commit is contained in:
Karoy Lorentey committed 2026-06-22 20:10:22 -07:00
1 parent 22a94a9756
commit e8b09ade25
11 files changed
+59 -41

No files matched your search

+1 -1
View File
@@ -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.
]
@@ -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
}
}
}
@@ -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
}
}
}
@@ -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]
}
}
}
@@ -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]
}
}
}
@@ -95,6 +95,18 @@ public protocol Container<Element>:
/// - 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<Element>:
@_lifetime(borrow self)
func nextSpan(after index: inout Index, maximumCount: Int) -> Span<Element>
// 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.
///
@@ -19,7 +19,13 @@ public protocol MutableContainer<Element>:
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(
@@ -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<Element> {
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
@@ -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
}
}
}
@@ -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]
}
}
}
+1
View File
@@ -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" \