[stdlib] Adopt conditional conformance for Indices, Slice, ReversedCollection (#12913)

* Refactor Indices and Slice to use conditional conformance

* Replace ReversedRandomAccessCollection with a conditional extension

* Refactor some types into struct+extensions

* Revise Slice documentation

* Fix test cases for adoption of conditional conformances.

* [RangeReplaceableCollection] Eliminate unnecessary slicing subscript operator.

* Add -enable-experimental-conditional-conformances to test.

* Gruesome workaround for crasher in MutableSlice tests
This commit is contained in:
Ben Cohen
2017-11-30 09:10:22 -08:00
committed by GitHub
parent eea5e5db47
commit c4f0b5fe94
25 changed files with 347 additions and 680 deletions

View File

@@ -44,14 +44,20 @@ public typealias BidirectionalIndexable = BidirectionalCollection
/// `c.index(before: c.index(after: i)) == i`.
/// - If `i > c.startIndex && i <= c.endIndex`
/// `c.index(after: c.index(before: i)) == i`.
public protocol BidirectionalCollection : Collection
{
public protocol BidirectionalCollection: Collection
where SubSequence: BidirectionalCollection, Indices: BidirectionalCollection {
// FIXME(ABI): Associated type inference requires this.
associatedtype Element
// FIXME(ABI): Associated type inference requires this.
associatedtype Index
// FIXME(ABI): Associated type inference requires this.
associatedtype SubSequence = Slice<Self>
// FIXME(ABI): Associated type inference requires this.
associatedtype Indices = DefaultIndices<Self>
/// Returns the position immediately before the given index.
///
/// - Parameter i: A valid index of the collection. `i` must be greater than
@@ -65,16 +71,6 @@ public protocol BidirectionalCollection : Collection
/// `startIndex`.
func formIndex(before i: inout Index)
/// A sequence that can represent a contiguous subrange of the collection's
/// elements.
associatedtype SubSequence : BidirectionalCollection
= BidirectionalSlice<Self>
/// A type that represents the indices that are valid for subscripting the
/// collection, in ascending order.
associatedtype Indices : BidirectionalCollection
= DefaultBidirectionalIndices<Self>
/// The indices that are valid for subscripting the collection, in ascending
/// order.
///
@@ -201,17 +197,6 @@ extension BidirectionalCollection {
}
}
/// Supply the default "slicing" `subscript` for `BidirectionalCollection`
/// models that accept the default associated `SubSequence`,
/// `BidirectionalSlice<Self>`.
extension BidirectionalCollection where SubSequence == BidirectionalSlice<Self> {
@_inlineable // FIXME(sil-serialize-all)
public subscript(bounds: Range<Index>) -> BidirectionalSlice<Self> {
_failEarlyRangeCheck(bounds, bounds: startIndex..<endIndex)
return BidirectionalSlice(base: self, bounds: bounds)
}
}
extension BidirectionalCollection where SubSequence == Self {
/// Removes and returns the last element of the collection.
///