[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
This commit is contained in:
Ben Cohen
2017-05-14 06:33:25 -07:00
committed by GitHub
parent 2e275e3a13
commit ea2f64cad2
78 changed files with 503 additions and 500 deletions

View File

@@ -12,10 +12,6 @@
%{
# We know we will eventually get a Sequence.Element type. Define
# a shorthand that we can use today.
GElement = "Iterator.Element"
orderingExplanation = """\
/// The predicate must be a *strict weak ordering* over the elements. That
/// is, for any elements `a`, `b`, and `c`, the following conditions must
@@ -110,7 +106,7 @@ extension Sequence {
% for preds in [True, False]:
% rethrows_ = "rethrows " if preds else ""
extension Sequence ${"" if preds else "where Iterator.Element : Comparable"} {
extension Sequence ${"" if preds else "where Element : Comparable"} {
% if preds:
/// Returns the minimum element in the sequence, using the given predicate as
@@ -152,9 +148,9 @@ ${orderingExplanation}
@warn_unqualified_access
public func min(
% if preds:
by areInIncreasingOrder: (${GElement}, ${GElement}) throws -> Bool
by areInIncreasingOrder: (Element, Element) throws -> Bool
% end
) ${rethrows_}-> ${GElement}? {
) ${rethrows_}-> Element? {
var it = makeIterator()
guard var result = it.next() else { return nil }
for e in IteratorSequence(it) {
@@ -206,9 +202,9 @@ ${orderingExplanation}
@warn_unqualified_access
public func max(
% if preds:
by areInIncreasingOrder: (${GElement}, ${GElement}) throws -> Bool
by areInIncreasingOrder: (Element, Element) throws -> Bool
% end
) ${rethrows_}-> ${GElement}? {
) ${rethrows_}-> Element? {
var it = makeIterator()
guard var result = it.next() else { return nil }
for e in IteratorSequence(it) {
@@ -233,7 +229,7 @@ ${orderingExplanation}
% for preds in [True, False]:
% rethrows_ = "rethrows " if preds else ""
extension Sequence ${"" if preds else "where Iterator.Element : Equatable"} {
extension Sequence ${"" if preds else "where Element : Equatable"} {
% if preds:
/// Returns a Boolean value indicating whether the initial elements of the
@@ -280,12 +276,12 @@ ${equivalenceExplanation}
public func starts<PossiblePrefix>(
with possiblePrefix: PossiblePrefix${"," if preds else ""}
% if preds:
by areEquivalent: (${GElement}, ${GElement}) throws -> Bool
by areEquivalent: (Element, Element) throws -> Bool
% end
) ${rethrows_}-> Bool
where
PossiblePrefix : Sequence,
PossiblePrefix.${GElement} == ${GElement} {
PossiblePrefix.Element == Element {
var possiblePrefixIterator = possiblePrefix.makeIterator()
for e0 in self {
@@ -313,7 +309,7 @@ ${equivalenceExplanation}
% for preds in [True, False]:
% rethrows_ = "rethrows " if preds else ""
extension Sequence ${"" if preds else "where Iterator.Element : Equatable"} {
extension Sequence ${"" if preds else "where Element : Equatable"} {
% if preds:
/// Returns a Boolean value indicating whether this sequence and another
@@ -358,12 +354,12 @@ ${equivalenceExplanation}
public func elementsEqual<OtherSequence>(
_ other: OtherSequence${"," if preds else ""}
% if preds:
by areEquivalent: (${GElement}, ${GElement}) throws -> Bool
by areEquivalent: (Element, Element) throws -> Bool
% end
) ${rethrows_}-> Bool
where
OtherSequence: Sequence,
OtherSequence.${GElement} == ${GElement} {
OtherSequence.Element == Element {
var iter1 = self.makeIterator()
var iter2 = other.makeIterator()
@@ -394,7 +390,7 @@ ${equivalenceExplanation}
% for preds in [True, False]:
% rethrows_ = "rethrows " if preds else ""
extension Sequence ${"" if preds else "where Iterator.Element : Comparable"} {
extension Sequence ${"" if preds else "where Element : Comparable"} {
% if preds:
/// Returns a Boolean value indicating whether the sequence precedes another
@@ -446,12 +442,12 @@ ${orderingExplanation}
_ other: OtherSequence${"," if preds else ""}
% if preds:
by areInIncreasingOrder:
(${GElement}, ${GElement}) throws -> Bool
(Element, Element) throws -> Bool
% end
) ${rethrows_}-> Bool
where
OtherSequence : Sequence,
OtherSequence.${GElement} == ${GElement} {
OtherSequence.Element == Element {
var iter1 = self.makeIterator()
var iter2 = other.makeIterator()
@@ -480,7 +476,7 @@ ${orderingExplanation}
// contains()
//===----------------------------------------------------------------------===//
extension Sequence where Iterator.Element : Equatable {
extension Sequence where Element : Equatable {
/// Returns a Boolean value indicating whether the sequence contains the
/// given element.
///
@@ -497,7 +493,7 @@ extension Sequence where Iterator.Element : Equatable {
/// - Returns: `true` if the element was found in the sequence; otherwise,
/// `false`.
@_inlineable
public func contains(_ element: ${GElement}) -> Bool {
public func contains(_ element: Element) -> Bool {
if let result = _customContainsEquatableElement(element) {
return result
}
@@ -549,7 +545,7 @@ extension Sequence {
/// `predicate`; otherwise, `false`.
@_inlineable
public func contains(
where predicate: (${GElement}) throws -> Bool
where predicate: (Element) throws -> Bool
) rethrows -> Bool {
for e in self {
if try predicate(e) {
@@ -610,7 +606,7 @@ extension Sequence {
public func reduce<Result>(
_ initialResult: Result,
_ nextPartialResult:
(_ partialResult: Result, ${GElement}) throws -> Result
(_ partialResult: Result, Element) throws -> Result
) rethrows -> Result {
var accumulator = initialResult
for element in self {
@@ -635,7 +631,7 @@ extension Sequence {
/// - Returns: An array containing the elements of this sequence in
/// reverse order.
@_inlineable
public func reversed() -> [${GElement}] {
public func reversed() -> [Element] {
// FIXME(performance): optimize to 1 pass? But Array(self) can be
// optimized to a memcpy() sometimes. Those cases are usually collections,
// though.
@@ -682,9 +678,9 @@ extension Sequence {
/// - SeeAlso: `joined()`, `map(_:)`
@_inlineable
public func flatMap<SegmentOfResult : Sequence>(
_ transform: (${GElement}) throws -> SegmentOfResult
) rethrows -> [SegmentOfResult.${GElement}] {
var result: [SegmentOfResult.${GElement}] = []
_ transform: (Element) throws -> SegmentOfResult
) rethrows -> [SegmentOfResult.Element] {
var result: [SegmentOfResult.Element] = []
for element in self {
result.append(contentsOf: try transform(element))
}
@@ -719,7 +715,7 @@ extension Sequence {
/// and *n* is the length of the result.
@_inlineable
public func flatMap<ElementOfResult>(
_ transform: (${GElement}) throws -> ElementOfResult?
_ transform: (Element) throws -> ElementOfResult?
) rethrows -> [ElementOfResult] {
return try _flatMap(transform)
}
@@ -729,7 +725,7 @@ extension Sequence {
// overloads.
@inline(__always)
public func _flatMap<ElementOfResult>(
_ transform: (${GElement}) throws -> ElementOfResult?
_ transform: (Element) throws -> ElementOfResult?
) rethrows -> [ElementOfResult] {
var result: [ElementOfResult] = []
for element in self {
@@ -749,42 +745,42 @@ extension Sequence {
@available(*, unavailable, renamed: "min(by:)")
public func minElement(
_ isOrderedBefore: (Iterator.Element, Iterator.Element) throws -> Bool
) rethrows -> Iterator.Element? {
_ isOrderedBefore: (Element, Element) throws -> Bool
) rethrows -> Element? {
Builtin.unreachable()
}
@available(*, unavailable, renamed: "max(by:)")
public func maxElement(
_ isOrderedBefore: (Iterator.Element, Iterator.Element) throws -> Bool
) rethrows -> Iterator.Element? {
_ isOrderedBefore: (Element, Element) throws -> Bool
) rethrows -> Element? {
Builtin.unreachable()
}
@available(*, unavailable, renamed: "reversed()")
public func reverse() -> [Iterator.Element] {
public func reverse() -> [Element] {
Builtin.unreachable()
}
@available(*, unavailable, renamed: "starts(with:by:)")
public func startsWith<PossiblePrefix>(
_ possiblePrefix: PossiblePrefix,
isEquivalent: (Iterator.Element, Iterator.Element) throws -> Bool
isEquivalent: (Element, Element) throws -> Bool
) rethrows-> Bool
where
PossiblePrefix : Sequence,
PossiblePrefix.Iterator.Element == Iterator.Element {
PossiblePrefix.Element == Element {
Builtin.unreachable()
}
@available(*, unavailable, renamed: "elementsEqual(_:by:)")
public func elementsEqual<OtherSequence>(
_ other: OtherSequence,
isEquivalent: (${GElement}, ${GElement}) throws -> Bool
isEquivalent: (Element, Element) throws -> Bool
) rethrows -> Bool
where
OtherSequence: Sequence,
OtherSequence.${GElement} == ${GElement} {
OtherSequence.Element == Element {
Builtin.unreachable()
}
@@ -793,17 +789,17 @@ extension Sequence {
OtherSequence
>(
_ other: OtherSequence,
isOrderedBefore: (${GElement}, ${GElement}) throws -> Bool
isOrderedBefore: (Element, Element) throws -> Bool
) rethrows -> Bool
where
OtherSequence : Sequence,
OtherSequence.${GElement} == ${GElement} {
OtherSequence.Element == Element {
Builtin.unreachable()
}
@available(*, unavailable, renamed: "contains(where:)")
public func contains(
_ predicate: (${GElement}) throws -> Bool
_ predicate: (Element) throws -> Bool
) rethrows -> Bool {
Builtin.unreachable()
}
@@ -811,20 +807,20 @@ extension Sequence {
@available(*, unavailable, renamed: "reduce(_:_:)")
public func reduce<Result>(
_ initial: Result,
combine: (_ partialResult: Result, ${GElement}) throws -> Result
combine: (_ partialResult: Result, Element) throws -> Result
) rethrows -> Result {
Builtin.unreachable()
}
}
extension Sequence where Iterator.Element : Comparable {
extension Sequence where Element : Comparable {
@available(*, unavailable, renamed: "min()")
public func minElement() -> Iterator.Element? {
public func minElement() -> Element? {
Builtin.unreachable()
}
@available(*, unavailable, renamed: "max()")
public func maxElement() -> Iterator.Element? {
public func maxElement() -> Element? {
Builtin.unreachable()
}
@@ -834,7 +830,7 @@ extension Sequence where Iterator.Element : Comparable {
) -> Bool
where
PossiblePrefix : Sequence,
PossiblePrefix.${GElement} == ${GElement} {
PossiblePrefix.Element == Element {
Builtin.unreachable()
}
@@ -844,7 +840,7 @@ extension Sequence where Iterator.Element : Comparable {
) -> Bool
where
OtherSequence : Sequence,
OtherSequence.${GElement} == ${GElement} {
OtherSequence.Element == Element {
Builtin.unreachable()
}
}