Skip to content

Commit 48e91c4

Browse files
authored
Merge pull request #11921 from DougGregor/revert-subsequence-as-sequence
2 parents 831a983 + 0793f6c commit 48e91c4

File tree

6 files changed

+61
-14
lines changed

6 files changed

+61
-14
lines changed

stdlib/private/StdlibCollectionUnittest/CheckSequenceType.swift

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1536,7 +1536,13 @@ extension TestSuite {
15361536

15371537
resiliencyChecks: CollectionMisuseResiliencyChecks = .all
15381538
) where
1539-
SequenceWithEquatableElement.Iterator.Element : Equatable {
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 {
15401546

15411547
var testNamePrefix = testNamePrefix
15421548

stdlib/private/StdlibUnittest/StdlibUnittest.swift.gyb

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -378,7 +378,11 @@ 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 {
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 {
382386
return x
383387
}
384388

@@ -413,7 +417,13 @@ public func expectSequenceAssociatedTypes<X : Sequence>(
413417
sequenceType: X.Type,
414418
iteratorType: X.Iterator.Type,
415419
subSequenceType: X.SubSequence.Type
416-
) {}
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 {}
417427

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

stdlib/public/core/Collection.swift

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -651,9 +651,12 @@ 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
654+
: _IndexableBase, Sequence
655655
= Slice<Self>
656-
where SubSequence.Index == Index
656+
where SubSequence.SubSequence == SubSequence
657+
// FIXME(ABI) (Revert Where Clauses): and this where clause:
658+
, Element == SubSequence.Element
659+
, SubSequence.Index == Index
657660

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

stdlib/public/core/ExistentialCollection.swift.gyb

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -426,10 +426,14 @@ 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':
431429
where
432430
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:
433437
S.SubSequence.Indices : ${Kind},
434438
S.Indices : ${Kind}
435439
% end
@@ -732,7 +736,10 @@ public struct AnySequence<Element> : Sequence {
732736
@_inlineable
733737
public init<S : Sequence>(_ base: S)
734738
where
735-
S.Element == Element {
739+
S.Element == Element,
740+
S.SubSequence : Sequence,
741+
S.SubSequence.Element == Element,
742+
S.SubSequence.SubSequence == S.SubSequence {
736743
self._box = _SequenceBox(_base: base)
737744
}
738745

stdlib/public/core/Sequence.swift

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -331,9 +331,19 @@ 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 : Sequence
335-
where Element == SubSequence.Element,
336-
SubSequence.SubSequence == SubSequence
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.
337347

338348
/// Returns an iterator over the elements of this sequence.
339349
func makeIterator() -> Iterator
@@ -1184,7 +1194,10 @@ extension Sequence where Element : Equatable {
11841194
}
11851195
}
11861196

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

11891202
/// Returns a subsequence containing all but the given number of initial
11901203
/// elements.

stdlib/public/core/SequenceWrapper.swift

Lines changed: 10 additions & 2 deletions
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 where Base.Element == Element
22+
associatedtype Base : Sequence
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 {
54+
extension _SequenceWrapper where Element == Base.Element {
5555
public func map<T>(
5656
_ transform: (Element) throws -> T
5757
) rethrows -> [T] {
@@ -93,6 +93,10 @@ 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 {
96100

97101
public func drop(
98102
while predicate: (Element) throws -> Bool
@@ -106,6 +110,10 @@ extension _SequenceWrapper where SubSequence == Base.SubSequence {
106110
return try _base.prefix(while: predicate)
107111
}
108112

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

0 commit comments

Comments
 (0)