Skip to content

[stdlib] Add Sequence.Element, change ExpressibleByArrayLiteral.Element to ArrayLiteralElement #8990

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 11 commits into from
May 14, 2017
Merged
2 changes: 0 additions & 2 deletions benchmark/single-source/PopFrontGeneric.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@ let arrayCount = 1024
// being really slow).
@_versioned
protocol MyArrayBufferProtocol : MutableCollection, RandomAccessCollection {
associatedtype Element

mutating func myReplace<C>(
_ subRange: Range<Int>,
with newValues: C
Expand Down
2 changes: 1 addition & 1 deletion lib/Sema/CSDiag.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6960,7 +6960,7 @@ bool FailureDiagnosis::visitArrayExpr(ArrayExpr *E) {
contextualElementType = ProtocolConformanceRef::getTypeWitnessByName(
contextualType,
*Conformance,
CS->getASTContext().Id_Element,
CS->getASTContext().getIdentifier("ArrayLiteralElement"),
&CS->TC)
->getDesugaredType();
elementTypePurpose = CTP_ArrayElement;
Expand Down
7 changes: 2 additions & 5 deletions stdlib/public/core/ArrayBufferProtocol.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,6 @@ internal protocol _ArrayBufferProtocol
: RandomAccessCollection
= CountableRange<Int>

/// The type of elements stored in the buffer.
associatedtype Element

/// Create an empty buffer.
init()

Expand Down Expand Up @@ -79,7 +76,7 @@ internal protocol _ArrayBufferProtocol
_ subrange: Range<Int>,
with newCount: Int,
elementsOf newValues: C
) where C : Collection, C.Iterator.Element == Element
) where C : Collection, C.Element == Element

/// Returns a `_SliceBuffer` containing the elements in `bounds`.
subscript(bounds: Range<Int>) -> _SliceBuffer<Element> { get }
Expand Down Expand Up @@ -144,7 +141,7 @@ extension _ArrayBufferProtocol {
_ subrange: Range<Int>,
with newCount: Int,
elementsOf newValues: C
) where C : Collection, C.Iterator.Element == Element {
) where C : Collection, C.Element == Element {
_sanityCheck(startIndex == 0, "_SliceBuffer should override this function.")
let oldCount = self.count
let eraseCount = subrange.count
Expand Down
6 changes: 3 additions & 3 deletions stdlib/public/core/ArrayType.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ internal protocol _ArrayProtocol
/// element. Otherwise, `nil`.
var _baseAddressIfContiguous: UnsafeMutablePointer<Element>? { get }

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

//===--- basic mutations ------------------------------------------------===//

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

/// Remove and return the element at the given index.
///
Expand All @@ -60,7 +60,7 @@ internal protocol _ArrayProtocol
///
/// - Precondition: `count > index`.
@discardableResult
mutating func remove(at index: Int) -> Iterator.Element
mutating func remove(at index: Int) -> Element

//===--- implementation detail -----------------------------------------===//

Expand Down
20 changes: 10 additions & 10 deletions stdlib/public/core/Arrays.swift.gyb
Original file line number Diff line number Diff line change
Expand Up @@ -1083,7 +1083,7 @@ extension ${Self} : RangeReplaceableCollection, _ArrayProtocol {
/// - Parameter s: The sequence of elements to turn into an array.
@_inlineable
public init<S : Sequence>(_ s: S)
where S.Iterator.Element == Element {
where S.Element == Element {

self = ${Self}(
_buffer: _Buffer(
Expand Down Expand Up @@ -1436,7 +1436,7 @@ extension ${Self} : RangeReplaceableCollection, _ArrayProtocol {
@_inlineable
@_semantics("array.append_contentsOf")
public mutating func append<S : Sequence>(contentsOf newElements: S)
where S.Iterator.Element == Element {
where S.Element == Element {

let newElementsCount = newElements.underestimatedCount
reserveCapacityForAppend(newElementsCount: newElementsCount)
Expand Down Expand Up @@ -1779,8 +1779,8 @@ extension ${Self} {

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

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

Expand Down Expand Up @@ -1817,7 +1817,7 @@ internal struct _InitializeMemoryFromCollection<
> : _PointerFunction {
@_inlineable
@_versioned
func call(_ rawMemory: UnsafeMutablePointer<C.Iterator.Element>, count: Int) {
func call(_ rawMemory: UnsafeMutablePointer<C.Element>, count: Int) {
var p = rawMemory
var q = newValues.startIndex
for _ in 0..<count {
Expand Down Expand Up @@ -1846,7 +1846,7 @@ extension _ArrayBufferProtocol {
_ bounds: Range<Int>,
with newValues: C,
count insertCount: Int
) where C.Iterator.Element == Element {
) where C.Element == Element {

let growth = insertCount - bounds.count
let newCount = self.count + growth
Expand Down Expand Up @@ -1918,7 +1918,7 @@ extension ${Self} {
public mutating func replaceSubrange<C>(
_ subrange: Range<Int>,
with newElements: C
) where C : Collection, C.Iterator.Element == Element {
) where C : Collection, C.Element == Element {
% if Self in ['Array', 'ContiguousArray']:
_precondition(subrange.lowerBound >= self._buffer.startIndex,
"${Self} replace: subrange start is negative")
Expand Down Expand Up @@ -2131,7 +2131,7 @@ extension _ArrayBufferProtocol {
@_versioned
internal mutating func _arrayAppendSequence<S : Sequence>(
_ newItems: S
) where S.Iterator.Element == Element {
) where S.Element == Element {

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

@available(*, unavailable, renamed: "append(contentsOf:)")
public mutating func appendContentsOf<S : Sequence>(_ newElements: S)
where S.Iterator.Element == Element {
where S.Element == Element {
Builtin.unreachable()
}
}
Expand Down
6 changes: 3 additions & 3 deletions stdlib/public/core/BidirectionalCollection.swift
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ public protocol BidirectionalCollection : _BidirectionalIndexable, Collection
/// // Prints "50"
///
/// - Complexity: O(1)
var last: Iterator.Element? { get }
var last: Element? { get }

/// Accesses a contiguous subrange of the collection's elements.
///
Expand Down Expand Up @@ -233,7 +233,7 @@ extension BidirectionalCollection where SubSequence == Self {
///
/// - Complexity: O(1).
/// - SeeAlso: `removeLast()`
public mutating func popLast() -> Iterator.Element? {
public mutating func popLast() -> Element? {
guard !isEmpty else { return nil }
let element = last!
self = self[startIndex..<index(before: endIndex)]
Expand All @@ -250,7 +250,7 @@ extension BidirectionalCollection where SubSequence == Self {
/// - Complexity: O(1)
/// - SeeAlso: `popLast()`
@discardableResult
public mutating func removeLast() -> Iterator.Element {
public mutating func removeLast() -> Element {
let element = last!
self = self[startIndex..<index(before: endIndex)]
return element
Expand Down
57 changes: 26 additions & 31 deletions stdlib/public/core/Collection.swift
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,11 @@ public protocol _IndexableBase {

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

Expand Down Expand Up @@ -652,7 +652,6 @@ public protocol Collection : _Indexable, Sequence
/// supplying `IndexingIterator` as its associated `Iterator`
/// type.
associatedtype Iterator = IndexingIterator<Self>
where Self.Iterator.Element == _Element

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


// FIXME(ABI)#98 (Recursive Protocol Constraints):
// FIXME(ABI)#99 (Associated Types with where clauses):
// associatedtype SubSequence : Collection
Expand Down Expand Up @@ -707,7 +705,7 @@ public protocol Collection : _Indexable, Sequence
/// `endIndex` property.
///
/// - Complexity: O(1)
subscript(position: Index) -> Iterator.Element { get }
subscript(position: Index) -> Element { get }

/// Accesses a contiguous subrange of the collection's elements.
///
Expand Down Expand Up @@ -740,7 +738,7 @@ public protocol Collection : _Indexable, Sequence
// FIXME(ABI) (Revert Where Clauses): Remove these two conformances
: _Indexable, Sequence
= DefaultIndices<Self>
where Indices.Iterator.Element == Index,
where Indices.Element == Index,
Indices.Index == Index
// FIXME(ABI) (Revert Where Clauses): Remove this where clause
, Indices.SubSequence == Indices
Expand Down Expand Up @@ -911,7 +909,7 @@ public protocol Collection : _Indexable, Sequence
/// otherwise, `nil`.
///
/// - Complexity: O(*n*)
func _customIndexOfEquatableElement(_ element: Iterator.Element) -> Index??
func _customIndexOfEquatableElement(_ element: Element) -> Index??

/// The first element of the collection.
///
Expand All @@ -922,7 +920,7 @@ public protocol Collection : _Indexable, Sequence
/// print(firstNumber)
/// }
/// // Prints "10"
var first: Iterator.Element? { get }
var first: Element? { get }

/// Returns an index that is the specified distance from the given index.
///
Expand Down Expand Up @@ -1317,7 +1315,7 @@ extension Collection where SubSequence == Self {
///
/// - Complexity: O(1)
@_inlineable
public mutating func popFirst() -> Iterator.Element? {
public mutating func popFirst() -> Element? {
// TODO: swift-3-indexing-model - review the following
guard !isEmpty else { return nil }
let element = first!
Expand Down Expand Up @@ -1360,23 +1358,20 @@ extension Collection {
/// }
/// // Prints "10"
@_inlineable
public var first: Iterator.Element? {
@inline(__always)
get {
// NB: Accessing `startIndex` may not be O(1) for some lazy collections,
// so instead of testing `isEmpty` and then returning the first element,
// we'll just rely on the fact that the iterator always yields the
// first element first.
var i = makeIterator()
return i.next()
}
public var first: Element? {
// NB: Accessing `startIndex` may not be O(1) for some lazy collections,
// so instead of testing `isEmpty` and then returning the first element,
// we'll just rely on the fact that the iterator always yields the
// first element first.
var i = makeIterator()
return i.next()
}

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

Expand Down Expand Up @@ -1448,7 +1443,7 @@ extension Collection {
/// sequence.
@_inlineable
public func map<T>(
_ transform: (Iterator.Element) throws -> T
_ transform: (Element) throws -> T
) rethrows -> [T] {
// TODO: swift-3-indexing-model - review the following
let count: Int = numericCast(self.count)
Expand Down Expand Up @@ -1536,7 +1531,7 @@ extension Collection {
/// - Complexity: O(*n*), where *n* is the length of the collection.
@_inlineable
public func drop(
while predicate: (Iterator.Element) throws -> Bool
while predicate: (Element) throws -> Bool
) rethrows -> SubSequence {
var start = startIndex
while try start != endIndex && predicate(self[start]) {
Expand Down Expand Up @@ -1582,7 +1577,7 @@ extension Collection {
/// - Complexity: O(*n*), where *n* is the length of the collection.
@_inlineable
public func prefix(
while predicate: (Iterator.Element) throws -> Bool
while predicate: (Element) throws -> Bool
) rethrows -> SubSequence {
var end = startIndex
while try end != endIndex && predicate(self[end]) {
Expand Down Expand Up @@ -1783,7 +1778,7 @@ extension Collection {
public func split(
maxSplits: Int = Int.max,
omittingEmptySubsequences: Bool = true,
whereSeparator isSeparator: (Iterator.Element) throws -> Bool
whereSeparator isSeparator: (Element) throws -> Bool
) rethrows -> [SubSequence] {
// TODO: swift-3-indexing-model - review the following
_precondition(maxSplits >= 0, "Must take zero or more splits")
Expand Down Expand Up @@ -1827,7 +1822,7 @@ extension Collection {
}
}

extension Collection where Iterator.Element : Equatable {
extension Collection where Element : Equatable {
/// Returns the longest possible subsequences of the collection, in order,
/// around elements equal to the given element.
///
Expand Down Expand Up @@ -1874,7 +1869,7 @@ extension Collection where Iterator.Element : Equatable {
/// elements.
@_inlineable
public func split(
separator: Iterator.Element,
separator: Element,
maxSplits: Int = Int.max,
omittingEmptySubsequences: Bool = true
) -> [SubSequence] {
Expand All @@ -1897,7 +1892,7 @@ extension Collection where SubSequence == Self {
/// - SeeAlso: `popFirst()`
@_inlineable
@discardableResult
public mutating func removeFirst() -> Iterator.Element {
public mutating func removeFirst() -> Element {
// TODO: swift-3-indexing-model - review the following
_precondition(!isEmpty, "can't remove items from an empty collection")
let element = first!
Expand Down Expand Up @@ -1960,16 +1955,16 @@ extension Collection {
public func split(
_ maxSplit: Int = Int.max,
allowEmptySlices: Bool = false,
whereSeparator isSeparator: (Iterator.Element) throws -> Bool
whereSeparator isSeparator: (Element) throws -> Bool
) rethrows -> [SubSequence] {
Builtin.unreachable()
}
}

extension Collection where Iterator.Element : Equatable {
extension Collection where Element : Equatable {
@available(*, unavailable, message: "Please use split(separator:maxSplits:omittingEmptySubsequences:) instead")
public func split(
_ separator: Iterator.Element,
_ separator: Element,
maxSplit: Int = Int.max,
allowEmptySlices: Bool = false
) -> [SubSequence] {
Expand Down
Loading