Skip to content

Commit ea2f64c

Browse files
[stdlib] Add Sequence.Element, change ExpressibleByArrayLiteral.Element to ArrayLiteralElement (#8990)
* Give Sequence a top-level Element, constrain Iterator to match * Remove many instances of Iterator. * Fixed various hard-coded tests * XFAIL a few tests that need further investigation * Change assoc type for arrayLiteralConvertible * Mop up remaining "better expressed as a where clause" warnings * Fix UnicodeDecoders prototype test * Fix UIntBuffer * Fix hard-coded Element identifier in CSDiag * Fix up more tests * Account for flatMap changes
1 parent 2e275e3 commit ea2f64c

File tree

78 files changed

+503
-500
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

78 files changed

+503
-500
lines changed

benchmark/single-source/PopFrontGeneric.swift

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,6 @@ let arrayCount = 1024
1919
// being really slow).
2020
@_versioned
2121
protocol MyArrayBufferProtocol : MutableCollection, RandomAccessCollection {
22-
associatedtype Element
23-
2422
mutating func myReplace<C>(
2523
_ subRange: Range<Int>,
2624
with newValues: C

lib/Sema/CSDiag.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6971,7 +6971,7 @@ bool FailureDiagnosis::visitArrayExpr(ArrayExpr *E) {
69716971
contextualElementType = ProtocolConformanceRef::getTypeWitnessByName(
69726972
contextualType,
69736973
*Conformance,
6974-
CS->getASTContext().Id_Element,
6974+
CS->getASTContext().getIdentifier("ArrayLiteralElement"),
69756975
&CS->TC)
69766976
->getDesugaredType();
69776977
elementTypePurpose = CTP_ArrayElement;

stdlib/public/core/ArrayBufferProtocol.swift

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,6 @@ internal protocol _ArrayBufferProtocol
2121
: RandomAccessCollection
2222
= CountableRange<Int>
2323

24-
/// The type of elements stored in the buffer.
25-
associatedtype Element
26-
2724
/// Create an empty buffer.
2825
init()
2926

@@ -79,7 +76,7 @@ internal protocol _ArrayBufferProtocol
7976
_ subrange: Range<Int>,
8077
with newCount: Int,
8178
elementsOf newValues: C
82-
) where C : Collection, C.Iterator.Element == Element
79+
) where C : Collection, C.Element == Element
8380

8481
/// Returns a `_SliceBuffer` containing the elements in `bounds`.
8582
subscript(bounds: Range<Int>) -> _SliceBuffer<Element> { get }
@@ -144,7 +141,7 @@ extension _ArrayBufferProtocol {
144141
_ subrange: Range<Int>,
145142
with newCount: Int,
146143
elementsOf newValues: C
147-
) where C : Collection, C.Iterator.Element == Element {
144+
) where C : Collection, C.Element == Element {
148145
_sanityCheck(startIndex == 0, "_SliceBuffer should override this function.")
149146
let oldCount = self.count
150147
let eraseCount = subrange.count

stdlib/public/core/ArrayType.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ internal protocol _ArrayProtocol
3131
/// element. Otherwise, `nil`.
3232
var _baseAddressIfContiguous: UnsafeMutablePointer<Element>? { get }
3333

34-
subscript(index: Int) -> Iterator.Element { get set }
34+
subscript(index: Int) -> Element { get set }
3535

3636
//===--- basic mutations ------------------------------------------------===//
3737

@@ -50,7 +50,7 @@ internal protocol _ArrayProtocol
5050
/// - Complexity: O(`self.count`).
5151
///
5252
/// - Precondition: `startIndex <= i`, `i <= endIndex`.
53-
mutating func insert(_ newElement: Iterator.Element, at i: Int)
53+
mutating func insert(_ newElement: Element, at i: Int)
5454

5555
/// Remove and return the element at the given index.
5656
///
@@ -60,7 +60,7 @@ internal protocol _ArrayProtocol
6060
///
6161
/// - Precondition: `count > index`.
6262
@discardableResult
63-
mutating func remove(at index: Int) -> Iterator.Element
63+
mutating func remove(at index: Int) -> Element
6464

6565
//===--- implementation detail -----------------------------------------===//
6666

stdlib/public/core/Arrays.swift.gyb

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1083,7 +1083,7 @@ extension ${Self} : RangeReplaceableCollection, _ArrayProtocol {
10831083
/// - Parameter s: The sequence of elements to turn into an array.
10841084
@_inlineable
10851085
public init<S : Sequence>(_ s: S)
1086-
where S.Iterator.Element == Element {
1086+
where S.Element == Element {
10871087

10881088
self = ${Self}(
10891089
_buffer: _Buffer(
@@ -1436,7 +1436,7 @@ extension ${Self} : RangeReplaceableCollection, _ArrayProtocol {
14361436
@_inlineable
14371437
@_semantics("array.append_contentsOf")
14381438
public mutating func append<S : Sequence>(contentsOf newElements: S)
1439-
where S.Iterator.Element == Element {
1439+
where S.Element == Element {
14401440

14411441
let newElementsCount = newElements.underestimatedCount
14421442
reserveCapacityForAppend(newElementsCount: newElementsCount)
@@ -1779,8 +1779,8 @@ extension ${Self} {
17791779

17801780
@_inlineable
17811781
public func _copyContents(
1782-
initializing buffer: UnsafeMutableBufferPointer<Iterator.Element>
1783-
) -> (Iterator,UnsafeMutableBufferPointer<Iterator.Element>.Index) {
1782+
initializing buffer: UnsafeMutableBufferPointer<Element>
1783+
) -> (Iterator,UnsafeMutableBufferPointer<Element>.Index) {
17841784

17851785
guard !self.isEmpty else { return (makeIterator(),buffer.startIndex) }
17861786

@@ -1817,7 +1817,7 @@ internal struct _InitializeMemoryFromCollection<
18171817
> : _PointerFunction {
18181818
@_inlineable
18191819
@_versioned
1820-
func call(_ rawMemory: UnsafeMutablePointer<C.Iterator.Element>, count: Int) {
1820+
func call(_ rawMemory: UnsafeMutablePointer<C.Element>, count: Int) {
18211821
var p = rawMemory
18221822
var q = newValues.startIndex
18231823
for _ in 0..<count {
@@ -1846,7 +1846,7 @@ extension _ArrayBufferProtocol {
18461846
_ bounds: Range<Int>,
18471847
with newValues: C,
18481848
count insertCount: Int
1849-
) where C.Iterator.Element == Element {
1849+
) where C.Element == Element {
18501850

18511851
let growth = insertCount - bounds.count
18521852
let newCount = self.count + growth
@@ -1918,7 +1918,7 @@ extension ${Self} {
19181918
public mutating func replaceSubrange<C>(
19191919
_ subrange: Range<Int>,
19201920
with newElements: C
1921-
) where C : Collection, C.Iterator.Element == Element {
1921+
) where C : Collection, C.Element == Element {
19221922
% if Self in ['Array', 'ContiguousArray']:
19231923
_precondition(subrange.lowerBound >= self._buffer.startIndex,
19241924
"${Self} replace: subrange start is negative")
@@ -2131,7 +2131,7 @@ extension _ArrayBufferProtocol {
21312131
@_versioned
21322132
internal mutating func _arrayAppendSequence<S : Sequence>(
21332133
_ newItems: S
2134-
) where S.Iterator.Element == Element {
2134+
) where S.Element == Element {
21352135

21362136
// this function is only ever called from append(contentsOf:)
21372137
// which should always have exhausted its capacity before calling
@@ -2410,13 +2410,13 @@ extension ${Self} {
24102410
public mutating func replaceRange<C>(
24112411
_ subRange: Range<Int>,
24122412
with newElements: C
2413-
) where C : Collection, C.Iterator.Element == Element {
2413+
) where C : Collection, C.Element == Element {
24142414
Builtin.unreachable()
24152415
}
24162416

24172417
@available(*, unavailable, renamed: "append(contentsOf:)")
24182418
public mutating func appendContentsOf<S : Sequence>(_ newElements: S)
2419-
where S.Iterator.Element == Element {
2419+
where S.Element == Element {
24202420
Builtin.unreachable()
24212421
}
24222422
}

stdlib/public/core/BidirectionalCollection.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ public protocol BidirectionalCollection : _BidirectionalIndexable, Collection
129129
/// // Prints "50"
130130
///
131131
/// - Complexity: O(1)
132-
var last: Iterator.Element? { get }
132+
var last: Element? { get }
133133

134134
/// Accesses a contiguous subrange of the collection's elements.
135135
///
@@ -233,7 +233,7 @@ extension BidirectionalCollection where SubSequence == Self {
233233
///
234234
/// - Complexity: O(1).
235235
/// - SeeAlso: `removeLast()`
236-
public mutating func popLast() -> Iterator.Element? {
236+
public mutating func popLast() -> Element? {
237237
guard !isEmpty else { return nil }
238238
let element = last!
239239
self = self[startIndex..<index(before: endIndex)]
@@ -250,7 +250,7 @@ extension BidirectionalCollection where SubSequence == Self {
250250
/// - Complexity: O(1)
251251
/// - SeeAlso: `popLast()`
252252
@discardableResult
253-
public mutating func removeLast() -> Iterator.Element {
253+
public mutating func removeLast() -> Element {
254254
let element = last!
255255
self = self[startIndex..<index(before: endIndex)]
256256
return element

stdlib/public/core/Collection.swift

Lines changed: 26 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -60,11 +60,11 @@ public protocol _IndexableBase {
6060

6161
// The declaration of _Element and subscript here is a trick used to
6262
// break a cyclic conformance/deduction that Swift can't handle. We
63-
// need something other than a Collection.Iterator.Element that can
63+
// need something other than a Collection.Element that can
6464
// be used as IndexingIterator<T>'s Element. Here we arrange for
6565
// the Collection itself to have an Element type that's deducible from
6666
// its subscript. Ideally we'd like to constrain this Element to be the same
67-
// as Collection.Iterator.Element (see below), but we have no way of
67+
// as Collection.Element (see below), but we have no way of
6868
// expressing it today.
6969
associatedtype _Element
7070

@@ -652,7 +652,6 @@ public protocol Collection : _Indexable, Sequence
652652
/// supplying `IndexingIterator` as its associated `Iterator`
653653
/// type.
654654
associatedtype Iterator = IndexingIterator<Self>
655-
where Self.Iterator.Element == _Element
656655

657656
// FIXME(ABI)#179 (Type checker): Needed here so that the `Iterator` is properly deduced from
658657
// a custom `makeIterator()` function. Otherwise we get an
@@ -672,10 +671,9 @@ public protocol Collection : _Indexable, Sequence
672671
= Slice<Self>
673672
where SubSequence.SubSequence == SubSequence
674673
// FIXME(ABI) (Revert Where Clauses): and this where clause:
675-
, Iterator.Element == SubSequence.Iterator.Element
674+
, Element == SubSequence.Element
676675
, SubSequence.Index == Index
677676

678-
679677
// FIXME(ABI)#98 (Recursive Protocol Constraints):
680678
// FIXME(ABI)#99 (Associated Types with where clauses):
681679
// associatedtype SubSequence : Collection
@@ -707,7 +705,7 @@ public protocol Collection : _Indexable, Sequence
707705
/// `endIndex` property.
708706
///
709707
/// - Complexity: O(1)
710-
subscript(position: Index) -> Iterator.Element { get }
708+
subscript(position: Index) -> Element { get }
711709

712710
/// Accesses a contiguous subrange of the collection's elements.
713711
///
@@ -740,7 +738,7 @@ public protocol Collection : _Indexable, Sequence
740738
// FIXME(ABI) (Revert Where Clauses): Remove these two conformances
741739
: _Indexable, Sequence
742740
= DefaultIndices<Self>
743-
where Indices.Iterator.Element == Index,
741+
where Indices.Element == Index,
744742
Indices.Index == Index
745743
// FIXME(ABI) (Revert Where Clauses): Remove this where clause
746744
, Indices.SubSequence == Indices
@@ -911,7 +909,7 @@ public protocol Collection : _Indexable, Sequence
911909
/// otherwise, `nil`.
912910
///
913911
/// - Complexity: O(*n*)
914-
func _customIndexOfEquatableElement(_ element: Iterator.Element) -> Index??
912+
func _customIndexOfEquatableElement(_ element: Element) -> Index??
915913

916914
/// The first element of the collection.
917915
///
@@ -922,7 +920,7 @@ public protocol Collection : _Indexable, Sequence
922920
/// print(firstNumber)
923921
/// }
924922
/// // Prints "10"
925-
var first: Iterator.Element? { get }
923+
var first: Element? { get }
926924

927925
/// Returns an index that is the specified distance from the given index.
928926
///
@@ -1317,7 +1315,7 @@ extension Collection where SubSequence == Self {
13171315
///
13181316
/// - Complexity: O(1)
13191317
@_inlineable
1320-
public mutating func popFirst() -> Iterator.Element? {
1318+
public mutating func popFirst() -> Element? {
13211319
// TODO: swift-3-indexing-model - review the following
13221320
guard !isEmpty else { return nil }
13231321
let element = first!
@@ -1360,23 +1358,20 @@ extension Collection {
13601358
/// }
13611359
/// // Prints "10"
13621360
@_inlineable
1363-
public var first: Iterator.Element? {
1364-
@inline(__always)
1365-
get {
1366-
// NB: Accessing `startIndex` may not be O(1) for some lazy collections,
1367-
// so instead of testing `isEmpty` and then returning the first element,
1368-
// we'll just rely on the fact that the iterator always yields the
1369-
// first element first.
1370-
var i = makeIterator()
1371-
return i.next()
1372-
}
1361+
public var first: Element? {
1362+
// NB: Accessing `startIndex` may not be O(1) for some lazy collections,
1363+
// so instead of testing `isEmpty` and then returning the first element,
1364+
// we'll just rely on the fact that the iterator always yields the
1365+
// first element first.
1366+
var i = makeIterator()
1367+
return i.next()
13731368
}
13741369

13751370
// TODO: swift-3-indexing-model - uncomment and replace above ready (or should we still use the iterator one?)
13761371
/// Returns the first element of `self`, or `nil` if `self` is empty.
13771372
///
13781373
/// - Complexity: O(1)
1379-
// public var first: Iterator.Element? {
1374+
// public var first: Element? {
13801375
// return isEmpty ? nil : self[startIndex]
13811376
// }
13821377

@@ -1448,7 +1443,7 @@ extension Collection {
14481443
/// sequence.
14491444
@_inlineable
14501445
public func map<T>(
1451-
_ transform: (Iterator.Element) throws -> T
1446+
_ transform: (Element) throws -> T
14521447
) rethrows -> [T] {
14531448
// TODO: swift-3-indexing-model - review the following
14541449
let count: Int = numericCast(self.count)
@@ -1536,7 +1531,7 @@ extension Collection {
15361531
/// - Complexity: O(*n*), where *n* is the length of the collection.
15371532
@_inlineable
15381533
public func drop(
1539-
while predicate: (Iterator.Element) throws -> Bool
1534+
while predicate: (Element) throws -> Bool
15401535
) rethrows -> SubSequence {
15411536
var start = startIndex
15421537
while try start != endIndex && predicate(self[start]) {
@@ -1582,7 +1577,7 @@ extension Collection {
15821577
/// - Complexity: O(*n*), where *n* is the length of the collection.
15831578
@_inlineable
15841579
public func prefix(
1585-
while predicate: (Iterator.Element) throws -> Bool
1580+
while predicate: (Element) throws -> Bool
15861581
) rethrows -> SubSequence {
15871582
var end = startIndex
15881583
while try end != endIndex && predicate(self[end]) {
@@ -1783,7 +1778,7 @@ extension Collection {
17831778
public func split(
17841779
maxSplits: Int = Int.max,
17851780
omittingEmptySubsequences: Bool = true,
1786-
whereSeparator isSeparator: (Iterator.Element) throws -> Bool
1781+
whereSeparator isSeparator: (Element) throws -> Bool
17871782
) rethrows -> [SubSequence] {
17881783
// TODO: swift-3-indexing-model - review the following
17891784
_precondition(maxSplits >= 0, "Must take zero or more splits")
@@ -1827,7 +1822,7 @@ extension Collection {
18271822
}
18281823
}
18291824

1830-
extension Collection where Iterator.Element : Equatable {
1825+
extension Collection where Element : Equatable {
18311826
/// Returns the longest possible subsequences of the collection, in order,
18321827
/// around elements equal to the given element.
18331828
///
@@ -1874,7 +1869,7 @@ extension Collection where Iterator.Element : Equatable {
18741869
/// elements.
18751870
@_inlineable
18761871
public func split(
1877-
separator: Iterator.Element,
1872+
separator: Element,
18781873
maxSplits: Int = Int.max,
18791874
omittingEmptySubsequences: Bool = true
18801875
) -> [SubSequence] {
@@ -1897,7 +1892,7 @@ extension Collection where SubSequence == Self {
18971892
/// - SeeAlso: `popFirst()`
18981893
@_inlineable
18991894
@discardableResult
1900-
public mutating func removeFirst() -> Iterator.Element {
1895+
public mutating func removeFirst() -> Element {
19011896
// TODO: swift-3-indexing-model - review the following
19021897
_precondition(!isEmpty, "can't remove items from an empty collection")
19031898
let element = first!
@@ -1960,16 +1955,16 @@ extension Collection {
19601955
public func split(
19611956
_ maxSplit: Int = Int.max,
19621957
allowEmptySlices: Bool = false,
1963-
whereSeparator isSeparator: (Iterator.Element) throws -> Bool
1958+
whereSeparator isSeparator: (Element) throws -> Bool
19641959
) rethrows -> [SubSequence] {
19651960
Builtin.unreachable()
19661961
}
19671962
}
19681963

1969-
extension Collection where Iterator.Element : Equatable {
1964+
extension Collection where Element : Equatable {
19701965
@available(*, unavailable, message: "Please use split(separator:maxSplits:omittingEmptySubsequences:) instead")
19711966
public func split(
1972-
_ separator: Iterator.Element,
1967+
_ separator: Element,
19731968
maxSplit: Int = Int.max,
19741969
allowEmptySlices: Bool = false
19751970
) -> [SubSequence] {

0 commit comments

Comments
 (0)