Skip to content

Commit ae6f5dd

Browse files
[stdlib] Add consuming/owned annotations to Collection implementations (#19360)
* Add consuming/owned annotations to Collection implementations * Update SILOptimizer tests * Fix access_marker_verify test * XFAIL reconstruct_type_from_mangled_name
1 parent 5f46179 commit ae6f5dd

38 files changed

+150
-150
lines changed

stdlib/public/core/Algorithm.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ extension EnumeratedSequence.Iterator: IteratorProtocol, Sequence {
154154
extension EnumeratedSequence: Sequence {
155155
/// Returns an iterator over the elements of this sequence.
156156
@inlinable
157-
public func makeIterator() -> Iterator {
157+
public __consuming func makeIterator() -> Iterator {
158158
return Iterator(_base: _base.makeIterator())
159159
}
160160
}

stdlib/public/core/Array.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1084,7 +1084,7 @@ extension Array: RangeReplaceableCollection, ArrayProtocol {
10841084
@_semantics("array.mutate_unknown")
10851085
internal mutating func _appendElementAssumeUniqueAndCapacity(
10861086
_ oldCount: Int,
1087-
newElement: Element
1087+
newElement: __owned Element
10881088
) {
10891089
_sanityCheck(_buffer.isMutableAndUniquelyReferenced())
10901090
_sanityCheck(_buffer.capacity >= _buffer.count + 1)
@@ -1116,7 +1116,7 @@ extension Array: RangeReplaceableCollection, ArrayProtocol {
11161116
/// same array.
11171117
@inlinable
11181118
@_semantics("array.append_element")
1119-
public mutating func append(_ newElement: Element) {
1119+
public mutating func append(_ newElement: __owned Element) {
11201120
_makeUniqueAndReserveCapacityIfNotUnique()
11211121
let oldCount = _getCount()
11221122
_reserveCapacityAssumingUniqueBuffer(oldCount: oldCount)
@@ -1141,7 +1141,7 @@ extension Array: RangeReplaceableCollection, ArrayProtocol {
11411141
/// array.
11421142
@inlinable
11431143
@_semantics("array.append_contentsOf")
1144-
public mutating func append<S: Sequence>(contentsOf newElements: S)
1144+
public mutating func append<S: Sequence>(contentsOf newElements: __owned S)
11451145
where S.Element == Element {
11461146

11471147
let newElementsCount = newElements.underestimatedCount
@@ -1246,7 +1246,7 @@ extension Array: RangeReplaceableCollection, ArrayProtocol {
12461246
/// - Complexity: O(*n*), where *n* is the length of the array. If
12471247
/// `i == endIndex`, this method is equivalent to `append(_:)`.
12481248
@inlinable
1249-
public mutating func insert(_ newElement: Element, at i: Int) {
1249+
public mutating func insert(_ newElement: __owned Element, at i: Int) {
12501250
_checkIndex(i)
12511251
self.replaceSubrange(i..<i, with: CollectionOfOne(newElement))
12521252
}
@@ -1561,7 +1561,7 @@ extension Array {
15611561
@_semantics("array.mutate_unknown")
15621562
public mutating func replaceSubrange<C>(
15631563
_ subrange: Range<Int>,
1564-
with newElements: C
1564+
with newElements: __owned C
15651565
) where C: Collection, C.Element == Element {
15661566
_precondition(subrange.lowerBound >= self._buffer.startIndex,
15671567
"Array replace: subrange start is negative")

stdlib/public/core/ArrayBuffer.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ internal struct _ArrayBuffer<Element> : _ArrayBufferProtocol {
4343
/// - Precondition: The elements actually have dynamic type `U`, and `U`
4444
/// is a class or `@objc` existential.
4545
@inlinable
46-
internal func cast<U>(toBufferOf _: U.Type) -> _ArrayBuffer<U> {
46+
__consuming internal func cast<U>(toBufferOf _: U.Type) -> _ArrayBuffer<U> {
4747
_sanityCheck(_isClassOrObjCExistential(Element.self))
4848
_sanityCheck(_isClassOrObjCExistential(U.self))
4949
return _ArrayBuffer<U>(storage: _storage)
@@ -60,7 +60,7 @@ internal struct _ArrayBuffer<Element> : _ArrayBufferProtocol {
6060
/// - Precondition: `U` is a class or `@objc` existential derived from
6161
/// `Element`.
6262
@inlinable
63-
internal func downcast<U>(
63+
__consuming internal func downcast<U>(
6464
toBufferWithDeferredTypeCheckOf _: U.Type
6565
) -> _ArrayBuffer<U> {
6666
_sanityCheck(_isClassOrObjCExistential(Element.self))
@@ -214,7 +214,7 @@ extension _ArrayBuffer {
214214
/// just-initialized memory.
215215
@inlinable
216216
@discardableResult
217-
internal func _copyContents(
217+
__consuming internal func _copyContents(
218218
subRange bounds: Range<Int>,
219219
initializing target: UnsafeMutablePointer<Element>
220220
) -> UnsafeMutablePointer<Element> {

stdlib/public/core/ArrayBufferProtocol.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ internal protocol _ArrayBufferProtocol
3030
/// memory starting at `target`. Return a pointer "past the end" of the
3131
/// just-initialized memory.
3232
@discardableResult
33-
func _copyContents(
33+
__consuming func _copyContents(
3434
subRange bounds: Range<Int>,
3535
initializing target: UnsafeMutablePointer<Element>
3636
) -> UnsafeMutablePointer<Element>
@@ -74,7 +74,7 @@ internal protocol _ArrayBufferProtocol
7474
mutating func replaceSubrange<C>(
7575
_ subrange: Range<Int>,
7676
with newCount: Int,
77-
elementsOf newValues: C
77+
elementsOf newValues: __owned C
7878
) where C : Collection, C.Element == Element
7979

8080
/// Returns a `_SliceBuffer` containing the elements in `bounds`.
@@ -149,7 +149,7 @@ extension _ArrayBufferProtocol where Indices == Range<Int>{
149149
internal mutating func replaceSubrange<C>(
150150
_ subrange: Range<Int>,
151151
with newCount: Int,
152-
elementsOf newValues: C
152+
elementsOf newValues: __owned C
153153
) where C : Collection, C.Element == Element {
154154
_sanityCheck(startIndex == 0, "_SliceBuffer should override this function.")
155155
let oldCount = self.count

stdlib/public/core/ArrayShared.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ extension _ArrayBufferProtocol {
8888
@inline(never)
8989
internal mutating func _arrayOutOfPlaceReplace<C: Collection>(
9090
_ bounds: Range<Int>,
91-
with newValues: C,
91+
with newValues: __owned C,
9292
count insertCount: Int
9393
) where C.Element == Element {
9494

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

291291
// this function is only ever called from append(contentsOf:)

stdlib/public/core/ArraySlice.swift

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -903,7 +903,7 @@ extension ArraySlice: RangeReplaceableCollection, ArrayProtocol {
903903
@_semantics("array.mutate_unknown")
904904
internal mutating func _appendElementAssumeUniqueAndCapacity(
905905
_ oldCount: Int,
906-
newElement: Element
906+
newElement: __owned Element
907907
) {
908908
_sanityCheck(_buffer.isMutableAndUniquelyReferenced())
909909
_sanityCheck(_buffer.capacity >= _buffer.count + 1)
@@ -935,7 +935,7 @@ extension ArraySlice: RangeReplaceableCollection, ArrayProtocol {
935935
/// same array.
936936
@inlinable
937937
@_semantics("array.append_element")
938-
public mutating func append(_ newElement: Element) {
938+
public mutating func append(_ newElement: __owned Element) {
939939
_makeUniqueAndReserveCapacityIfNotUnique()
940940
let oldCount = _getCount()
941941
_reserveCapacityAssumingUniqueBuffer(oldCount: oldCount)
@@ -960,7 +960,7 @@ extension ArraySlice: RangeReplaceableCollection, ArrayProtocol {
960960
/// array.
961961
@inlinable
962962
@_semantics("array.append_contentsOf")
963-
public mutating func append<S: Sequence>(contentsOf newElements: S)
963+
public mutating func append<S: Sequence>(contentsOf newElements: __owned S)
964964
where S.Element == Element {
965965

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

11021102
@inlinable
1103-
public func _copyToContiguousArray() -> ContiguousArray<Element> {
1103+
public __consuming func _copyToContiguousArray() -> ContiguousArray<Element> {
11041104
if let n = _buffer.requestNativeBuffer() {
11051105
return ContiguousArray(_buffer: n)
11061106
}
@@ -1260,7 +1260,7 @@ extension ArraySlice {
12601260
}
12611261

12621262
@inlinable
1263-
public func _copyContents(
1263+
public __consuming func _copyContents(
12641264
initializing buffer: UnsafeMutableBufferPointer<Element>
12651265
) -> (Iterator,UnsafeMutableBufferPointer<Element>.Index) {
12661266

@@ -1330,7 +1330,7 @@ extension ArraySlice {
13301330
@_semantics("array.mutate_unknown")
13311331
public mutating func replaceSubrange<C>(
13321332
_ subrange: Range<Int>,
1333-
with newElements: C
1333+
with newElements: __owned C
13341334
) where C: Collection, C.Element == Element {
13351335
_precondition(subrange.lowerBound >= _buffer.startIndex,
13361336
"ArraySlice replace: subrange start is before the startIndex")

stdlib/public/core/ArrayType.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ internal protocol ArrayProtocol
5151
/// - Complexity: O(`self.count`).
5252
///
5353
/// - Precondition: `startIndex <= i`, `i <= endIndex`.
54-
mutating func insert(_ newElement: Element, at i: Int)
54+
mutating func insert(_ newElement: __owned Element, at i: Int)
5555

5656
/// Remove and return the element at the given index.
5757
///
@@ -77,7 +77,7 @@ extension ArrayProtocol {
7777
// efficient, we should make the default implementation coming from Sequence
7878
// preferred.
7979
@inlinable
80-
public func filter(
80+
public __consuming func filter(
8181
_ isIncluded: (Element) throws -> Bool
8282
) rethrows -> [Element] {
8383
return try _filter(isIncluded)

stdlib/public/core/BidirectionalCollection.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -382,7 +382,7 @@ extension BidirectionalCollection {
382382
/// `RandomAccessCollection`; otherwise, O(*k*), where *k* is the number of
383383
/// elements to drop.
384384
@inlinable // protocol-only
385-
public func dropLast(_ k: Int) -> SubSequence {
385+
public __consuming func dropLast(_ k: Int) -> SubSequence {
386386
_precondition(
387387
k >= 0, "Can't drop a negative number of elements from a collection")
388388
let end = index(
@@ -413,7 +413,7 @@ extension BidirectionalCollection {
413413
/// `RandomAccessCollection`; otherwise, O(*k*), where *k* is equal to
414414
/// `maxLength`.
415415
@inlinable // protocol-only
416-
public func suffix(_ maxLength: Int) -> SubSequence {
416+
public __consuming func suffix(_ maxLength: Int) -> SubSequence {
417417
_precondition(
418418
maxLength >= 0,
419419
"Can't take a suffix of negative length from a collection")

stdlib/public/core/CharacterUnicodeScalars.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ extension Character.UnicodeScalarView.Iterator : IteratorProtocol {
4949

5050
extension Character.UnicodeScalarView : Sequence {
5151
@inlinable // FIXME(sil-serialize-all)
52-
public func makeIterator() -> Iterator {
52+
public __consuming func makeIterator() -> Iterator {
5353
return Iterator(_base: IndexingIterator(_elements: self))
5454
}
5555
}

stdlib/public/core/Collection.swift

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1120,7 +1120,7 @@ extension Collection where Iterator == IndexingIterator<Self> {
11201120
/// Returns an iterator over the elements of the collection.
11211121
@inlinable // trivial-implementation
11221122
@inline(__always)
1123-
public func makeIterator() -> IndexingIterator<Self> {
1123+
public __consuming func makeIterator() -> IndexingIterator<Self> {
11241124
return IndexingIterator(_elements: self)
11251125
}
11261126
}
@@ -1356,7 +1356,7 @@ extension Collection {
13561356
/// `RandomAccessCollection`; otherwise, O(*k*), where *k* is the number of
13571357
/// elements to drop from the beginning of the collection.
13581358
@inlinable
1359-
public func dropFirst(_ k: Int) -> SubSequence {
1359+
public __consuming func dropFirst(_ k: Int) -> SubSequence {
13601360
_precondition(k >= 0, "Can't drop a negative number of elements from a collection")
13611361
let start = index(startIndex,
13621362
offsetBy: k, limitedBy: endIndex) ?? endIndex
@@ -1384,7 +1384,7 @@ extension Collection {
13841384
/// `RandomAccessCollection`; otherwise, O(*n*), where *n* is the length of
13851385
/// the collection.
13861386
@inlinable
1387-
public func dropLast(_ k: Int) -> SubSequence {
1387+
public __consuming func dropLast(_ k: Int) -> SubSequence {
13881388
_precondition(
13891389
k >= 0, "Can't drop a negative number of elements from a collection")
13901390
let amount = Swift.max(0, count - k)
@@ -1403,7 +1403,7 @@ extension Collection {
14031403
///
14041404
/// - Complexity: O(*n*), where *n* is the length of the collection.
14051405
@inlinable
1406-
public func drop(
1406+
public __consuming func drop(
14071407
while predicate: (Element) throws -> Bool
14081408
) rethrows -> SubSequence {
14091409
var start = startIndex
@@ -1434,7 +1434,7 @@ extension Collection {
14341434
/// `RandomAccessCollection`; otherwise, O(*k*), where *k* is the number of
14351435
/// elements to select from the beginning of the collection.
14361436
@inlinable
1437-
public func prefix(_ maxLength: Int) -> SubSequence {
1437+
public __consuming func prefix(_ maxLength: Int) -> SubSequence {
14381438
_precondition(
14391439
maxLength >= 0,
14401440
"Can't take a prefix of negative length from a collection")
@@ -1453,7 +1453,7 @@ extension Collection {
14531453
///
14541454
/// - Complexity: O(*n*), where *n* is the length of the collection.
14551455
@inlinable
1456-
public func prefix(
1456+
public __consuming func prefix(
14571457
while predicate: (Element) throws -> Bool
14581458
) rethrows -> SubSequence {
14591459
var end = startIndex
@@ -1484,7 +1484,7 @@ extension Collection {
14841484
/// `RandomAccessCollection`; otherwise, O(*n*), where *n* is the length of
14851485
/// the collection.
14861486
@inlinable
1487-
public func suffix(_ maxLength: Int) -> SubSequence {
1487+
public __consuming func suffix(_ maxLength: Int) -> SubSequence {
14881488
_precondition(
14891489
maxLength >= 0,
14901490
"Can't take a suffix of negative length from a collection")
@@ -1529,7 +1529,7 @@ extension Collection {
15291529
///
15301530
/// - Complexity: O(1)
15311531
@inlinable
1532-
public func prefix(upTo end: Index) -> SubSequence {
1532+
public __consuming func prefix(upTo end: Index) -> SubSequence {
15331533
return self[startIndex..<end]
15341534
}
15351535

@@ -1567,7 +1567,7 @@ extension Collection {
15671567
///
15681568
/// - Complexity: O(1)
15691569
@inlinable
1570-
public func suffix(from start: Index) -> SubSequence {
1570+
public __consuming func suffix(from start: Index) -> SubSequence {
15711571
return self[start..<endIndex]
15721572
}
15731573

@@ -1601,7 +1601,7 @@ extension Collection {
16011601
///
16021602
/// - Complexity: O(1)
16031603
@inlinable
1604-
public func prefix(through position: Index) -> SubSequence {
1604+
public __consuming func prefix(through position: Index) -> SubSequence {
16051605
return prefix(upTo: index(after: position))
16061606
}
16071607

@@ -1654,7 +1654,7 @@ extension Collection {
16541654
///
16551655
/// - Complexity: O(*n*), where *n* is the length of the collection.
16561656
@inlinable
1657-
public func split(
1657+
public __consuming func split(
16581658
maxSplits: Int = Int.max,
16591659
omittingEmptySubsequences: Bool = true,
16601660
whereSeparator isSeparator: (Element) throws -> Bool
@@ -1749,7 +1749,7 @@ extension Collection where Element : Equatable {
17491749
///
17501750
/// - Complexity: O(*n*), where *n* is the length of the collection.
17511751
@inlinable
1752-
public func split(
1752+
public __consuming func split(
17531753
separator: Element,
17541754
maxSplits: Int = Int.max,
17551755
omittingEmptySubsequences: Bool = true

stdlib/public/core/CollectionOfOne.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ extension CollectionOfOne: RandomAccessCollection, MutableCollection {
117117
///
118118
/// - Complexity: O(1)
119119
@inlinable // trivial-implementation
120-
public func makeIterator() -> Iterator {
120+
public __consuming func makeIterator() -> Iterator {
121121
return Iterator(_elements: _element)
122122
}
123123

stdlib/public/core/ContiguousArray.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -729,7 +729,7 @@ extension ContiguousArray: RangeReplaceableCollection, ArrayProtocol {
729729
@_semantics("array.mutate_unknown")
730730
internal mutating func _appendElementAssumeUniqueAndCapacity(
731731
_ oldCount: Int,
732-
newElement: Element
732+
newElement: __owned Element
733733
) {
734734
_sanityCheck(_buffer.isMutableAndUniquelyReferenced())
735735
_sanityCheck(_buffer.capacity >= _buffer.count + 1)
@@ -891,7 +891,7 @@ extension ContiguousArray: RangeReplaceableCollection, ArrayProtocol {
891891
/// - Complexity: O(*n*), where *n* is the length of the array. If
892892
/// `i == endIndex`, this method is equivalent to `append(_:)`.
893893
@inlinable
894-
public mutating func insert(_ newElement: Element, at i: Int) {
894+
public mutating func insert(_ newElement: __owned Element, at i: Int) {
895895
_checkIndex(i)
896896
self.replaceSubrange(i..<i, with: CollectionOfOne(newElement))
897897
}
@@ -926,7 +926,7 @@ extension ContiguousArray: RangeReplaceableCollection, ArrayProtocol {
926926
}
927927

928928
@inlinable
929-
public func _copyToContiguousArray() -> ContiguousArray<Element> {
929+
public __consuming func _copyToContiguousArray() -> ContiguousArray<Element> {
930930
if let n = _buffer.requestNativeBuffer() {
931931
return ContiguousArray(_buffer: n)
932932
}
@@ -1086,7 +1086,7 @@ extension ContiguousArray {
10861086
}
10871087

10881088
@inlinable
1089-
public func _copyContents(
1089+
public __consuming func _copyContents(
10901090
initializing buffer: UnsafeMutableBufferPointer<Element>
10911091
) -> (Iterator,UnsafeMutableBufferPointer<Element>.Index) {
10921092

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

0 commit comments

Comments
 (0)