Skip to content

[stdlib] Add consuming/owned annotations to Collection implementations #19360

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Sep 21, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion stdlib/public/core/Algorithm.swift
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ extension EnumeratedSequence.Iterator: IteratorProtocol, Sequence {
extension EnumeratedSequence: Sequence {
/// Returns an iterator over the elements of this sequence.
@inlinable
public func makeIterator() -> Iterator {
public __consuming func makeIterator() -> Iterator {
return Iterator(_base: _base.makeIterator())
}
}
10 changes: 5 additions & 5 deletions stdlib/public/core/Array.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1084,7 +1084,7 @@ extension Array: RangeReplaceableCollection, ArrayProtocol {
@_semantics("array.mutate_unknown")
internal mutating func _appendElementAssumeUniqueAndCapacity(
_ oldCount: Int,
newElement: Element
newElement: __owned Element
) {
_sanityCheck(_buffer.isMutableAndUniquelyReferenced())
_sanityCheck(_buffer.capacity >= _buffer.count + 1)
Expand Down Expand Up @@ -1116,7 +1116,7 @@ extension Array: RangeReplaceableCollection, ArrayProtocol {
/// same array.
@inlinable
@_semantics("array.append_element")
public mutating func append(_ newElement: Element) {
public mutating func append(_ newElement: __owned Element) {
_makeUniqueAndReserveCapacityIfNotUnique()
let oldCount = _getCount()
_reserveCapacityAssumingUniqueBuffer(oldCount: oldCount)
Expand All @@ -1141,7 +1141,7 @@ extension Array: RangeReplaceableCollection, ArrayProtocol {
/// array.
@inlinable
@_semantics("array.append_contentsOf")
public mutating func append<S: Sequence>(contentsOf newElements: S)
public mutating func append<S: Sequence>(contentsOf newElements: __owned S)
where S.Element == Element {

let newElementsCount = newElements.underestimatedCount
Expand Down Expand Up @@ -1246,7 +1246,7 @@ extension Array: RangeReplaceableCollection, ArrayProtocol {
/// - Complexity: O(*n*), where *n* is the length of the array. If
/// `i == endIndex`, this method is equivalent to `append(_:)`.
@inlinable
public mutating func insert(_ newElement: Element, at i: Int) {
public mutating func insert(_ newElement: __owned Element, at i: Int) {
_checkIndex(i)
self.replaceSubrange(i..<i, with: CollectionOfOne(newElement))
}
Expand Down Expand Up @@ -1561,7 +1561,7 @@ extension Array {
@_semantics("array.mutate_unknown")
public mutating func replaceSubrange<C>(
_ subrange: Range<Int>,
with newElements: C
with newElements: __owned C
) where C: Collection, C.Element == Element {
_precondition(subrange.lowerBound >= self._buffer.startIndex,
"Array replace: subrange start is negative")
Expand Down
6 changes: 3 additions & 3 deletions stdlib/public/core/ArrayBuffer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ internal struct _ArrayBuffer<Element> : _ArrayBufferProtocol {
/// - Precondition: The elements actually have dynamic type `U`, and `U`
/// is a class or `@objc` existential.
@inlinable
internal func cast<U>(toBufferOf _: U.Type) -> _ArrayBuffer<U> {
__consuming internal func cast<U>(toBufferOf _: U.Type) -> _ArrayBuffer<U> {
_sanityCheck(_isClassOrObjCExistential(Element.self))
_sanityCheck(_isClassOrObjCExistential(U.self))
return _ArrayBuffer<U>(storage: _storage)
Expand All @@ -60,7 +60,7 @@ internal struct _ArrayBuffer<Element> : _ArrayBufferProtocol {
/// - Precondition: `U` is a class or `@objc` existential derived from
/// `Element`.
@inlinable
internal func downcast<U>(
__consuming internal func downcast<U>(
toBufferWithDeferredTypeCheckOf _: U.Type
) -> _ArrayBuffer<U> {
_sanityCheck(_isClassOrObjCExistential(Element.self))
Expand Down Expand Up @@ -214,7 +214,7 @@ extension _ArrayBuffer {
/// just-initialized memory.
@inlinable
@discardableResult
internal func _copyContents(
__consuming internal func _copyContents(
subRange bounds: Range<Int>,
initializing target: UnsafeMutablePointer<Element>
) -> UnsafeMutablePointer<Element> {
Expand Down
6 changes: 3 additions & 3 deletions stdlib/public/core/ArrayBufferProtocol.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ internal protocol _ArrayBufferProtocol
/// memory starting at `target`. Return a pointer "past the end" of the
/// just-initialized memory.
@discardableResult
func _copyContents(
__consuming func _copyContents(
subRange bounds: Range<Int>,
initializing target: UnsafeMutablePointer<Element>
) -> UnsafeMutablePointer<Element>
Expand Down Expand Up @@ -74,7 +74,7 @@ internal protocol _ArrayBufferProtocol
mutating func replaceSubrange<C>(
_ subrange: Range<Int>,
with newCount: Int,
elementsOf newValues: C
elementsOf newValues: __owned C
) where C : Collection, C.Element == Element

/// Returns a `_SliceBuffer` containing the elements in `bounds`.
Expand Down Expand Up @@ -149,7 +149,7 @@ extension _ArrayBufferProtocol where Indices == Range<Int>{
internal mutating func replaceSubrange<C>(
_ subrange: Range<Int>,
with newCount: Int,
elementsOf newValues: C
elementsOf newValues: __owned C
) where C : Collection, C.Element == Element {
_sanityCheck(startIndex == 0, "_SliceBuffer should override this function.")
let oldCount = self.count
Expand Down
4 changes: 2 additions & 2 deletions stdlib/public/core/ArrayShared.swift
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ extension _ArrayBufferProtocol {
@inline(never)
internal mutating func _arrayOutOfPlaceReplace<C: Collection>(
_ bounds: Range<Int>,
with newValues: C,
with newValues: __owned C,
count insertCount: Int
) where C.Element == Element {

Expand Down Expand Up @@ -285,7 +285,7 @@ extension _ArrayBufferProtocol {
/// Append items from `newItems` to a buffer.
@inlinable
internal mutating func _arrayAppendSequence<S: Sequence>(
_ newItems: S
_ newItems: __owned S
) where S.Element == Element {

// this function is only ever called from append(contentsOf:)
Expand Down
14 changes: 7 additions & 7 deletions stdlib/public/core/ArraySlice.swift
Original file line number Diff line number Diff line change
Expand Up @@ -903,7 +903,7 @@ extension ArraySlice: RangeReplaceableCollection, ArrayProtocol {
@_semantics("array.mutate_unknown")
internal mutating func _appendElementAssumeUniqueAndCapacity(
_ oldCount: Int,
newElement: Element
newElement: __owned Element
) {
_sanityCheck(_buffer.isMutableAndUniquelyReferenced())
_sanityCheck(_buffer.capacity >= _buffer.count + 1)
Expand Down Expand Up @@ -935,7 +935,7 @@ extension ArraySlice: RangeReplaceableCollection, ArrayProtocol {
/// same array.
@inlinable
@_semantics("array.append_element")
public mutating func append(_ newElement: Element) {
public mutating func append(_ newElement: __owned Element) {
_makeUniqueAndReserveCapacityIfNotUnique()
let oldCount = _getCount()
_reserveCapacityAssumingUniqueBuffer(oldCount: oldCount)
Expand All @@ -960,7 +960,7 @@ extension ArraySlice: RangeReplaceableCollection, ArrayProtocol {
/// array.
@inlinable
@_semantics("array.append_contentsOf")
public mutating func append<S: Sequence>(contentsOf newElements: S)
public mutating func append<S: Sequence>(contentsOf newElements: __owned S)
where S.Element == Element {

let newElementsCount = newElements.underestimatedCount
Expand Down Expand Up @@ -1065,7 +1065,7 @@ extension ArraySlice: RangeReplaceableCollection, ArrayProtocol {
/// - Complexity: O(*n*), where *n* is the length of the array. If
/// `i == endIndex`, this method is equivalent to `append(_:)`.
@inlinable
public mutating func insert(_ newElement: Element, at i: Int) {
public mutating func insert(_ newElement: __owned Element, at i: Int) {
_checkIndex(i)
self.replaceSubrange(i..<i, with: CollectionOfOne(newElement))
}
Expand Down Expand Up @@ -1100,7 +1100,7 @@ extension ArraySlice: RangeReplaceableCollection, ArrayProtocol {
}

@inlinable
public func _copyToContiguousArray() -> ContiguousArray<Element> {
public __consuming func _copyToContiguousArray() -> ContiguousArray<Element> {
if let n = _buffer.requestNativeBuffer() {
return ContiguousArray(_buffer: n)
}
Expand Down Expand Up @@ -1260,7 +1260,7 @@ extension ArraySlice {
}

@inlinable
public func _copyContents(
public __consuming func _copyContents(
initializing buffer: UnsafeMutableBufferPointer<Element>
) -> (Iterator,UnsafeMutableBufferPointer<Element>.Index) {

Expand Down Expand Up @@ -1330,7 +1330,7 @@ extension ArraySlice {
@_semantics("array.mutate_unknown")
public mutating func replaceSubrange<C>(
_ subrange: Range<Int>,
with newElements: C
with newElements: __owned C
) where C: Collection, C.Element == Element {
_precondition(subrange.lowerBound >= _buffer.startIndex,
"ArraySlice replace: subrange start is before the startIndex")
Expand Down
4 changes: 2 additions & 2 deletions stdlib/public/core/ArrayType.swift
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ internal protocol ArrayProtocol
/// - Complexity: O(`self.count`).
///
/// - Precondition: `startIndex <= i`, `i <= endIndex`.
mutating func insert(_ newElement: Element, at i: Int)
mutating func insert(_ newElement: __owned Element, at i: Int)

/// Remove and return the element at the given index.
///
Expand All @@ -77,7 +77,7 @@ extension ArrayProtocol {
// efficient, we should make the default implementation coming from Sequence
// preferred.
@inlinable
public func filter(
public __consuming func filter(
_ isIncluded: (Element) throws -> Bool
) rethrows -> [Element] {
return try _filter(isIncluded)
Expand Down
4 changes: 2 additions & 2 deletions stdlib/public/core/BidirectionalCollection.swift
Original file line number Diff line number Diff line change
Expand Up @@ -382,7 +382,7 @@ extension BidirectionalCollection {
/// `RandomAccessCollection`; otherwise, O(*k*), where *k* is the number of
/// elements to drop.
@inlinable // protocol-only
public func dropLast(_ k: Int) -> SubSequence {
public __consuming func dropLast(_ k: Int) -> SubSequence {
_precondition(
k >= 0, "Can't drop a negative number of elements from a collection")
let end = index(
Expand Down Expand Up @@ -413,7 +413,7 @@ extension BidirectionalCollection {
/// `RandomAccessCollection`; otherwise, O(*k*), where *k* is equal to
/// `maxLength`.
@inlinable // protocol-only
public func suffix(_ maxLength: Int) -> SubSequence {
public __consuming func suffix(_ maxLength: Int) -> SubSequence {
_precondition(
maxLength >= 0,
"Can't take a suffix of negative length from a collection")
Expand Down
2 changes: 1 addition & 1 deletion stdlib/public/core/CharacterUnicodeScalars.swift
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ extension Character.UnicodeScalarView.Iterator : IteratorProtocol {

extension Character.UnicodeScalarView : Sequence {
@inlinable // FIXME(sil-serialize-all)
public func makeIterator() -> Iterator {
public __consuming func makeIterator() -> Iterator {
return Iterator(_base: IndexingIterator(_elements: self))
}
}
Expand Down
24 changes: 12 additions & 12 deletions stdlib/public/core/Collection.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1120,7 +1120,7 @@ extension Collection where Iterator == IndexingIterator<Self> {
/// Returns an iterator over the elements of the collection.
@inlinable // trivial-implementation
@inline(__always)
public func makeIterator() -> IndexingIterator<Self> {
public __consuming func makeIterator() -> IndexingIterator<Self> {
return IndexingIterator(_elements: self)
}
}
Expand Down Expand Up @@ -1356,7 +1356,7 @@ extension Collection {
/// `RandomAccessCollection`; otherwise, O(*k*), where *k* is the number of
/// elements to drop from the beginning of the collection.
@inlinable
public func dropFirst(_ k: Int) -> SubSequence {
public __consuming func dropFirst(_ k: Int) -> SubSequence {
_precondition(k >= 0, "Can't drop a negative number of elements from a collection")
let start = index(startIndex,
offsetBy: k, limitedBy: endIndex) ?? endIndex
Expand Down Expand Up @@ -1384,7 +1384,7 @@ extension Collection {
/// `RandomAccessCollection`; otherwise, O(*n*), where *n* is the length of
/// the collection.
@inlinable
public func dropLast(_ k: Int) -> SubSequence {
public __consuming func dropLast(_ k: Int) -> SubSequence {
_precondition(
k >= 0, "Can't drop a negative number of elements from a collection")
let amount = Swift.max(0, count - k)
Expand All @@ -1403,7 +1403,7 @@ extension Collection {
///
/// - Complexity: O(*n*), where *n* is the length of the collection.
@inlinable
public func drop(
public __consuming func drop(
while predicate: (Element) throws -> Bool
) rethrows -> SubSequence {
var start = startIndex
Expand Down Expand Up @@ -1434,7 +1434,7 @@ extension Collection {
/// `RandomAccessCollection`; otherwise, O(*k*), where *k* is the number of
/// elements to select from the beginning of the collection.
@inlinable
public func prefix(_ maxLength: Int) -> SubSequence {
public __consuming func prefix(_ maxLength: Int) -> SubSequence {
_precondition(
maxLength >= 0,
"Can't take a prefix of negative length from a collection")
Expand All @@ -1453,7 +1453,7 @@ extension Collection {
///
/// - Complexity: O(*n*), where *n* is the length of the collection.
@inlinable
public func prefix(
public __consuming func prefix(
while predicate: (Element) throws -> Bool
) rethrows -> SubSequence {
var end = startIndex
Expand Down Expand Up @@ -1484,7 +1484,7 @@ extension Collection {
/// `RandomAccessCollection`; otherwise, O(*n*), where *n* is the length of
/// the collection.
@inlinable
public func suffix(_ maxLength: Int) -> SubSequence {
public __consuming func suffix(_ maxLength: Int) -> SubSequence {
_precondition(
maxLength >= 0,
"Can't take a suffix of negative length from a collection")
Expand Down Expand Up @@ -1529,7 +1529,7 @@ extension Collection {
///
/// - Complexity: O(1)
@inlinable
public func prefix(upTo end: Index) -> SubSequence {
public __consuming func prefix(upTo end: Index) -> SubSequence {
return self[startIndex..<end]
}

Expand Down Expand Up @@ -1567,7 +1567,7 @@ extension Collection {
///
/// - Complexity: O(1)
@inlinable
public func suffix(from start: Index) -> SubSequence {
public __consuming func suffix(from start: Index) -> SubSequence {
return self[start..<endIndex]
}

Expand Down Expand Up @@ -1601,7 +1601,7 @@ extension Collection {
///
/// - Complexity: O(1)
@inlinable
public func prefix(through position: Index) -> SubSequence {
public __consuming func prefix(through position: Index) -> SubSequence {
return prefix(upTo: index(after: position))
}

Expand Down Expand Up @@ -1654,7 +1654,7 @@ extension Collection {
///
/// - Complexity: O(*n*), where *n* is the length of the collection.
@inlinable
public func split(
public __consuming func split(
maxSplits: Int = Int.max,
omittingEmptySubsequences: Bool = true,
whereSeparator isSeparator: (Element) throws -> Bool
Expand Down Expand Up @@ -1749,7 +1749,7 @@ extension Collection where Element : Equatable {
///
/// - Complexity: O(*n*), where *n* is the length of the collection.
@inlinable
public func split(
public __consuming func split(
separator: Element,
maxSplits: Int = Int.max,
omittingEmptySubsequences: Bool = true
Expand Down
2 changes: 1 addition & 1 deletion stdlib/public/core/CollectionOfOne.swift
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ extension CollectionOfOne: RandomAccessCollection, MutableCollection {
///
/// - Complexity: O(1)
@inlinable // trivial-implementation
public func makeIterator() -> Iterator {
public __consuming func makeIterator() -> Iterator {
return Iterator(_elements: _element)
}

Expand Down
10 changes: 5 additions & 5 deletions stdlib/public/core/ContiguousArray.swift
Original file line number Diff line number Diff line change
Expand Up @@ -729,7 +729,7 @@ extension ContiguousArray: RangeReplaceableCollection, ArrayProtocol {
@_semantics("array.mutate_unknown")
internal mutating func _appendElementAssumeUniqueAndCapacity(
_ oldCount: Int,
newElement: Element
newElement: __owned Element
) {
_sanityCheck(_buffer.isMutableAndUniquelyReferenced())
_sanityCheck(_buffer.capacity >= _buffer.count + 1)
Expand Down Expand Up @@ -891,7 +891,7 @@ extension ContiguousArray: RangeReplaceableCollection, ArrayProtocol {
/// - Complexity: O(*n*), where *n* is the length of the array. If
/// `i == endIndex`, this method is equivalent to `append(_:)`.
@inlinable
public mutating func insert(_ newElement: Element, at i: Int) {
public mutating func insert(_ newElement: __owned Element, at i: Int) {
_checkIndex(i)
self.replaceSubrange(i..<i, with: CollectionOfOne(newElement))
}
Expand Down Expand Up @@ -926,7 +926,7 @@ extension ContiguousArray: RangeReplaceableCollection, ArrayProtocol {
}

@inlinable
public func _copyToContiguousArray() -> ContiguousArray<Element> {
public __consuming func _copyToContiguousArray() -> ContiguousArray<Element> {
if let n = _buffer.requestNativeBuffer() {
return ContiguousArray(_buffer: n)
}
Expand Down Expand Up @@ -1086,7 +1086,7 @@ extension ContiguousArray {
}

@inlinable
public func _copyContents(
public __consuming func _copyContents(
initializing buffer: UnsafeMutableBufferPointer<Element>
) -> (Iterator,UnsafeMutableBufferPointer<Element>.Index) {

Expand Down Expand Up @@ -1157,7 +1157,7 @@ extension ContiguousArray {
@_semantics("array.mutate_unknown")
public mutating func replaceSubrange<C>(
_ subrange: Range<Int>,
with newElements: C
with newElements: __owned C
) where C: Collection, C.Element == Element {
_precondition(subrange.lowerBound >= self._buffer.startIndex,
"ContiguousArray replace: subrange start is negative")
Expand Down
Loading