Skip to content

Commit e5f893b

Browse files
committed
[Sequence] Make Sequence.SubSequence conform to Sequence.
Addressed ABI FIXME’s #4, swiftlang#5, 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. Fixes SR-318 / rdar://problem/31418206.
1 parent 2323991 commit e5f893b

File tree

6 files changed

+14
-62
lines changed

6 files changed

+14
-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
@@ -450,14 +450,10 @@ internal class _AnyRandomAccessCollectionBox<Element>
450450
@_fixed_layout
451451
@_versioned
452452
internal final class _${Kind}Box<S : ${Kind}> : _Any${Kind}Box<S.Iterator.Element>
453+
// FIXME(ABI) (Revert Where Clauses): apply all this only to Sequence:
454+
% if Kind != 'Sequence':
453455
where
454456
S.SubSequence : ${Kind},
455-
// FIXME(ABI) (Revert Where Clauses): apply all this only to Sequence:
456-
% if Kind == 'Sequence':
457-
S.SubSequence.Element == S.Element,
458-
S.SubSequence.SubSequence == S.SubSequence
459-
// FIXME(ABI) (Revert Where Clauses): remove this else clause:
460-
% else:
461457
S.SubSequence.Indices : ${Kind},
462458
S.Indices : ${Kind}
463459
% end
@@ -764,10 +760,7 @@ public struct AnySequence<Element> : Sequence {
764760
@_inlineable
765761
public init<S : Sequence>(_ base: S)
766762
where
767-
S.Element == Element,
768-
S.SubSequence : Sequence,
769-
S.SubSequence.Element == Element,
770-
S.SubSequence.SubSequence == S.SubSequence {
763+
S.Element == Element {
771764
self._box = _SequenceBox(_base: base)
772765
}
773766

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-11
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

@@ -55,7 +55,7 @@ extension _SequenceWrapper where Iterator == Base.Iterator {
5555
}
5656
}
5757

58-
extension _SequenceWrapper where Element == Base.Element {
58+
extension _SequenceWrapper {
5959
@_inlineable // FIXME(sil-serialize-all)
6060
public func map<T>(
6161
_ transform: (Element) throws -> T
@@ -106,10 +106,6 @@ extension _SequenceWrapper where SubSequence == Base.SubSequence {
106106
public func suffix(_ maxLength: Int) -> SubSequence {
107107
return _base.suffix(maxLength)
108108
}
109-
}
110-
111-
extension _SequenceWrapper
112-
where SubSequence == Base.SubSequence, Element == Base.Element {
113109

114110
@_inlineable // FIXME(sil-serialize-all)
115111
public func drop(
@@ -125,11 +121,6 @@ extension _SequenceWrapper
125121
return try _base.prefix(while: predicate)
126122
}
127123

128-
@_inlineable // FIXME(sil-serialize-all)
129-
public func suffix(_ maxLength: Int) -> SubSequence {
130-
return _base.suffix(maxLength)
131-
}
132-
133124
@_inlineable // FIXME(sil-serialize-all)
134125
public func split(
135126
maxSplits: Int, omittingEmptySubsequences: Bool,

0 commit comments

Comments
 (0)