Skip to content

Commit 4edd762

Browse files
committed
[Sequence] Make Sequence.SubSequence conform to Sequence.
Addressed ABI FIXME’s swiftlang#104 and swiftlang#105, making Sequence’s SubSequence conform to Sequence, with the same element type, and for which the SubSequence of a SubSequence is the same SubSequence.
1 parent 986df37 commit 4edd762

File tree

7 files changed

+15
-62
lines changed

7 files changed

+15
-62
lines changed

stdlib/private/StdlibCollectionUnittest/CheckSequenceType.swift

+1-7
Original file line numberDiff line numberDiff line change
@@ -1536,13 +1536,7 @@ extension TestSuite {
15361536

15371537
resiliencyChecks: CollectionMisuseResiliencyChecks = .all
15381538
) where
1539-
SequenceWithEquatableElement.Iterator.Element : Equatable,
1540-
SequenceWithEquatableElement.SubSequence : Sequence,
1541-
SequenceWithEquatableElement.SubSequence.Iterator.Element
1542-
== SequenceWithEquatableElement.Iterator.Element,
1543-
S.SubSequence : Sequence,
1544-
S.SubSequence.Iterator.Element == S.Iterator.Element,
1545-
S.SubSequence.SubSequence == S.SubSequence {
1539+
SequenceWithEquatableElement.Iterator.Element : Equatable {
15461540

15471541
var testNamePrefix = testNamePrefix
15481542

stdlib/private/StdlibUnittest/StdlibUnittest.swift.gyb

+2-12
Original file line numberDiff line numberDiff line change
@@ -378,11 +378,7 @@ public func expectTrapping<Bound>(
378378
public func expectType<T>(_: T.Type, _ x: inout T) {}
379379
public func expectEqualType<T>(_: T.Type, _: T.Type) {}
380380

381-
public func expectSequenceType<X : Sequence>(_ x: X) -> X
382-
where
383-
X.SubSequence : Sequence,
384-
X.SubSequence.Iterator.Element == X.Iterator.Element,
385-
X.SubSequence.SubSequence == X.SubSequence {
381+
public func expectSequenceType<X : Sequence>(_ x: X) -> X {
386382
return x
387383
}
388384

@@ -417,13 +413,7 @@ public func expectSequenceAssociatedTypes<X : Sequence>(
417413
sequenceType: X.Type,
418414
iteratorType: X.Iterator.Type,
419415
subSequenceType: X.SubSequence.Type
420-
) where
421-
// FIXME(ABI)#4 (Associated Types with where clauses): there should be no constraints in
422-
// the 'where' clause, all of these should be required by the protocol.
423-
X.SubSequence : Sequence,
424-
X.SubSequence.Iterator.Element == X.Iterator.Element,
425-
// X.SubSequence.Indices == X.Indices, // FIXME(ABI)#5 (Recursive Protocol Constraints): can't have this constraint now.
426-
X.SubSequence.SubSequence == X.SubSequence {}
416+
) {}
427417

428418
/// Check that all associated types of a `Collection` are what we expect them
429419
/// to be.

stdlib/public/core/Collection.swift

+2-5
Original file line numberDiff line numberDiff line change
@@ -651,12 +651,9 @@ public protocol Collection : _Indexable, Sequence
651651
/// collection, the subsequence should also conform to `Collection`.
652652
associatedtype SubSequence
653653
// FIXME(ABI) (Revert Where Clauses): remove these conformances:
654-
: _IndexableBase, Sequence
654+
: _IndexableBase
655655
= Slice<Self>
656-
where SubSequence.SubSequence == SubSequence
657-
// FIXME(ABI) (Revert Where Clauses): and this where clause:
658-
, Element == SubSequence.Element
659-
, SubSequence.Index == Index
656+
where SubSequence.Index == Index
660657

661658
// FIXME(ABI)#98 (Recursive Protocol Constraints):
662659
// FIXME(ABI)#99 (Associated Types with where clauses):

stdlib/public/core/ExistentialCollection.swift.gyb

+3-10
Original file line numberDiff line numberDiff line change
@@ -426,14 +426,10 @@ internal class _AnyRandomAccessCollectionBox<Element>
426426
@_fixed_layout
427427
@_versioned
428428
internal final class _${Kind}Box<S : ${Kind}> : _Any${Kind}Box<S.Iterator.Element>
429+
// FIXME(ABI) (Revert Where Clauses): apply all this only to Sequence:
430+
% if Kind != 'Sequence':
429431
where
430432
S.SubSequence : ${Kind},
431-
// FIXME(ABI) (Revert Where Clauses): apply all this only to Sequence:
432-
% if Kind == 'Sequence':
433-
S.SubSequence.Element == S.Element,
434-
S.SubSequence.SubSequence == S.SubSequence
435-
// FIXME(ABI) (Revert Where Clauses): remove this else clause:
436-
% else:
437433
S.SubSequence.Indices : ${Kind},
438434
S.Indices : ${Kind}
439435
% end
@@ -736,10 +732,7 @@ public struct AnySequence<Element> : Sequence {
736732
@_inlineable
737733
public init<S : Sequence>(_ base: S)
738734
where
739-
S.Element == Element,
740-
S.SubSequence : Sequence,
741-
S.SubSequence.Element == Element,
742-
S.SubSequence.SubSequence == S.SubSequence {
735+
S.Element == Element {
743736
self._box = _SequenceBox(_base: base)
744737
}
745738

stdlib/public/core/Sequence.swift

+4-17
Original file line numberDiff line numberDiff line change
@@ -331,19 +331,9 @@ public protocol Sequence {
331331
associatedtype Iterator : IteratorProtocol where Iterator.Element == Element
332332

333333
/// A type that represents a subsequence of some of the sequence's elements.
334-
associatedtype SubSequence
335-
// FIXME(ABI)#104 (Recursive Protocol Constraints):
336-
// FIXME(ABI)#105 (Associated Types with where clauses):
337-
// associatedtype SubSequence : Sequence
338-
// where
339-
// Element == SubSequence.Element,
340-
// SubSequence.SubSequence == SubSequence
341-
//
342-
// (<rdar://problem/20715009> Implement recursive protocol
343-
// constraints)
344-
//
345-
// These constraints allow processing collections in generic code by
346-
// repeatedly slicing them in a loop.
334+
associatedtype SubSequence : Sequence
335+
where Element == SubSequence.Element,
336+
SubSequence.SubSequence == SubSequence
347337

348338
/// Returns an iterator over the elements of this sequence.
349339
func makeIterator() -> Iterator
@@ -1194,10 +1184,7 @@ extension Sequence where Element : Equatable {
11941184
}
11951185
}
11961186

1197-
extension Sequence where
1198-
SubSequence : Sequence,
1199-
SubSequence.Element == Element,
1200-
SubSequence.SubSequence == SubSequence {
1187+
extension Sequence {
12011188

12021189
/// Returns a subsequence containing all but the given number of initial
12031190
/// elements.

stdlib/public/core/SequenceWrapper.swift

+2-10
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
@_show_in_interface
2020
public // @testable
2121
protocol _SequenceWrapper : Sequence {
22-
associatedtype Base : Sequence
22+
associatedtype Base : Sequence where Base.Element == Element
2323
associatedtype Iterator = Base.Iterator
2424
associatedtype SubSequence = Base.SubSequence
2525

@@ -51,7 +51,7 @@ extension _SequenceWrapper where Iterator == Base.Iterator {
5151
}
5252
}
5353

54-
extension _SequenceWrapper where Element == Base.Element {
54+
extension _SequenceWrapper {
5555
public func map<T>(
5656
_ transform: (Element) throws -> T
5757
) rethrows -> [T] {
@@ -93,10 +93,6 @@ extension _SequenceWrapper where SubSequence == Base.SubSequence {
9393
public func suffix(_ maxLength: Int) -> SubSequence {
9494
return _base.suffix(maxLength)
9595
}
96-
}
97-
98-
extension _SequenceWrapper
99-
where SubSequence == Base.SubSequence, Element == Base.Element {
10096

10197
public func drop(
10298
while predicate: (Element) throws -> Bool
@@ -110,10 +106,6 @@ extension _SequenceWrapper
110106
return try _base.prefix(while: predicate)
111107
}
112108

113-
public func suffix(_ maxLength: Int) -> SubSequence {
114-
return _base.suffix(maxLength)
115-
}
116-
117109
public func split(
118110
maxSplits: Int, omittingEmptySubsequences: Bool,
119111
whereSeparator isSeparator: (Element) throws -> Bool

stdlib/public/core/UnicodeEncoding.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public protocol _UnicodeEncoding_ {
4444
/// A type that can be used to parse `CodeUnits` into
4545
/// `EncodedScalar`s.
4646
associatedtype ForwardParser : Unicode.Parser
47-
// where ForwardParser.Encoding == Self
47+
where ForwardParser.Encoding == Self
4848

4949
/// A type that can be used to parse a reversed sequence of
5050
/// `CodeUnits` into `EncodedScalar`s.

0 commit comments

Comments
 (0)