Skip to content

Commit 96ea67d

Browse files
authored
Merge pull request #9512 from airspeedswift/come-back-where-clauses-we-miss-you
[stdlib] Revert addition of some recursive where clauses on Collection protocols
2 parents 02dbc0f + d957041 commit 96ea67d

13 files changed

+130
-31
lines changed

stdlib/private/StdlibCollectionUnittest/CheckCollectionInstance.swift.gyb

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,8 @@ public func checkCollection<${genericParam}, C : Collection>(
257257
${TRACE},
258258
resiliencyChecks: CollectionMisuseResiliencyChecks = .all,
259259
sameValue: (${Element}, ${Element}) -> Bool
260-
) where C.Iterator.Element == ${Element} {
260+
) where C.Iterator.Element == ${Element},
261+
C.SubSequence : Collection {
261262

262263
checkForwardCollection(expected, collection, message(),
263264
stackTrace: stackTrace, showFrame: showFrame, file: file, line: line,
@@ -277,6 +278,7 @@ public func check${Traversal}Collection<
277278
resiliencyChecks: CollectionMisuseResiliencyChecks = .all
278279
) where
279280
C.Iterator.Element == ${Element},
281+
C.SubSequence : ${TraversalCollection},
280282
${Element} : Equatable {
281283

282284
check${Traversal}Collection(
@@ -296,7 +298,8 @@ public func check${Traversal}Collection<
296298
resiliencyChecks: CollectionMisuseResiliencyChecks = .all,
297299
sameValue: (${Element}, ${Element}) -> Bool
298300
) where
299-
C.Iterator.Element == ${Element} {
301+
C.Iterator.Element == ${Element},
302+
C.SubSequence : ${TraversalCollection} {
300303

301304
checkOneLevelOf${Traversal}Collection(expected, collection, ${trace},
302305
resiliencyChecks: resiliencyChecks, sameValue: sameValue)
@@ -501,7 +504,8 @@ ${genericParam}, S : ${TraversalCollection}
501504
resiliencyChecks: CollectionMisuseResiliencyChecks = .all,
502505
sameValue: (${Element}, ${Element}) -> Bool
503506
) where
504-
S.Iterator.Element == ${Element} {
507+
S.Iterator.Element == ${Element},
508+
S.SubSequence : ${TraversalCollection} {
505509

506510
let expectedArray = Array(expected)
507511

stdlib/private/StdlibCollectionUnittest/CheckCollectionType.swift.gyb

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -471,9 +471,16 @@ internal enum _SubSequenceSubscriptOnRangeMode {
471471
%{
472472
from gyb_stdlib_support import collectionForTraversal
473473
def testConstraints(protocol):
474+
if protocol == 'Collection':
475+
subseq_as_collection = 'CollectionWithEquatableElement.SubSequence : Collection,'
476+
else:
477+
subseq_as_collection=''
474478
return '''
475479
C : %(protocol)s,
476480
CollectionWithEquatableElement : %(protocol)s,
481+
%(subseq_as_collection)s
482+
C.SubSequence : %(protocol)s,
483+
C.Indices : %(protocol)s,
477484
CollectionWithEquatableElement.Iterator.Element : Equatable
478485
''' % locals()
479486

@@ -487,7 +494,6 @@ internal enum _SubSequenceSubscriptOnRangeMode {
487494
[CollectionWithEquatableElement.Iterator.Element]
488495
) -> CollectionWithEquatableElement,
489496

490-
491497
wrapValueIntoEquatable: @escaping (
492498
MinimalEquatableValue) -> CollectionWithEquatableElement.Iterator.Element,
493499

stdlib/private/StdlibCollectionUnittest/CheckMutableCollectionType.swift.gyb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,8 @@ extension TestSuite {
121121
isFixedLengthCollection: Bool,
122122
collectionIsBidirectional: Bool = false
123123
) where
124+
C.SubSequence : MutableCollection,
125+
C.Indices : Collection,
124126
CollectionWithEquatableElement.Iterator.Element : Equatable,
125127
CollectionWithComparableElement.Iterator.Element : Comparable {
126128

@@ -781,6 +783,8 @@ self.test("\(testNamePrefix).partition/InvalidOrderings") {
781783
withUnsafeMutableBufferPointerIsSupported: Bool,
782784
isFixedLengthCollection: Bool
783785
) where
786+
C.SubSequence : BidirectionalCollection & MutableCollection,
787+
C.Indices : BidirectionalCollection,
784788
CollectionWithEquatableElement.Iterator.Element : Equatable,
785789
CollectionWithComparableElement.Iterator.Element : Comparable {
786790

@@ -925,6 +929,8 @@ self.test("\(testNamePrefix).partition/DispatchesThrough_withUnsafeMutableBuffer
925929
withUnsafeMutableBufferPointerIsSupported: Bool,
926930
isFixedLengthCollection: Bool
927931
) where
932+
C.SubSequence : RandomAccessCollection & MutableCollection,
933+
C.Indices : RandomAccessCollection,
928934
CollectionWithEquatableElement.Iterator.Element : Equatable,
929935
CollectionWithComparableElement.Iterator.Element : Comparable {
930936

stdlib/private/StdlibCollectionUnittest/CheckRangeReplaceableCollectionType.swift

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -462,7 +462,10 @@ extension TestSuite {
462462
outOfBoundsIndexOffset: Int = 1,
463463
collectionIsBidirectional: Bool = false
464464
) where
465-
CollectionWithEquatableElement.Iterator.Element : Equatable {
465+
C.SubSequence : Collection,
466+
C.Indices : Collection,
467+
CollectionWithEquatableElement.Iterator.Element : Equatable,
468+
CollectionWithEquatableElement.SubSequence : Collection {
466469

467470
var testNamePrefix = testNamePrefix
468471

@@ -1177,6 +1180,8 @@ self.test("\(testNamePrefix).OperatorPlus") {
11771180
resiliencyChecks: CollectionMisuseResiliencyChecks = .all,
11781181
outOfBoundsIndexOffset: Int = 1
11791182
) where
1183+
C.SubSequence : BidirectionalCollection & RangeReplaceableCollection,
1184+
C.Indices : BidirectionalCollection,
11801185
CollectionWithEquatableElement.Iterator.Element : Equatable {
11811186

11821187
var testNamePrefix = testNamePrefix
@@ -1297,6 +1302,8 @@ self.test("\(testNamePrefix).removeLast(n: Int)/whereIndexIsBidirectional/remove
12971302
resiliencyChecks: CollectionMisuseResiliencyChecks = .all,
12981303
outOfBoundsIndexOffset: Int = 1
12991304
) where
1305+
C.SubSequence : RandomAccessCollection & RangeReplaceableCollection,
1306+
C.Indices : RandomAccessCollection,
13001307
CollectionWithEquatableElement.Iterator.Element : Equatable {
13011308

13021309
var testNamePrefix = testNamePrefix

stdlib/private/StdlibCollectionUnittest/CheckRangeReplaceableSliceType.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ extension TestSuite {
3333
collectionIsBidirectional: Bool = false
3434
) where
3535
C.SubSequence == C,
36+
C.Indices : Collection,
3637
CollectionWithEquatableElement.SubSequence == CollectionWithEquatableElement,
3738
CollectionWithEquatableElement.Iterator.Element : Equatable {
3839

@@ -164,6 +165,7 @@ extension TestSuite {
164165
outOfBoundsIndexOffset: Int = 1
165166
) where
166167
C.SubSequence == C,
168+
C.Indices : BidirectionalCollection,
167169
CollectionWithEquatableElement.SubSequence == CollectionWithEquatableElement,
168170
CollectionWithEquatableElement.Iterator.Element : Equatable {
169171

@@ -308,6 +310,7 @@ extension TestSuite {
308310
outOfBoundsIndexOffset: Int = 1
309311
) where
310312
C.SubSequence == C,
313+
C.Indices : RandomAccessCollection,
311314
CollectionWithEquatableElement.SubSequence == CollectionWithEquatableElement,
312315
CollectionWithEquatableElement.Iterator.Element : Equatable {
313316

stdlib/private/StdlibUnittest/StdlibUnittest.swift.gyb

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -376,7 +376,14 @@ public func expectSequenceType<X : Sequence>(_ x: X) -> X
376376
% for Mutable in ['', 'Mutable']:
377377
public func expect${Mutable}CollectionType<X : ${Mutable}Collection>(
378378
_ x: X.Type
379-
) { }
379+
) where
380+
// FIXME(ABI)#2 (Associated Types with where clauses): there should be no constraints in
381+
// the 'where' clause, all of these should be required by the protocol.
382+
% if Mutable == '':
383+
X.SubSequence : Collection,
384+
% end
385+
// X.SubSequence.Indices == X.Indices, // FIXME(ABI)#3 (Recursive Protocol Constraints): can't have this constraint now.
386+
X.Indices : Collection {}
380387
% end
381388

382389
/// A slice is a `Collection` that when sliced returns an instance of
@@ -414,7 +421,12 @@ public func expectCollectionAssociatedTypes<X : Collection>(
414421
indexType: X.Index.Type,
415422
indexDistanceType: X.IndexDistance.Type,
416423
indicesType: X.Indices.Type
417-
) { }
424+
) where
425+
// FIXME(ABI)#6 (Associated Types with where clauses): there should be no constraints in
426+
// the 'where' clause, all of these should be required by the protocol.
427+
X.SubSequence : Collection,
428+
// X.SubSequence.Indices == X.Indices, // FIXME(ABI)#7 (Recursive Protocol Constraints): can't have this constraint now.
429+
X.Indices : Collection {}
418430

419431
/// Check that all associated types of a `BidirectionalCollection` are what we
420432
/// expect them to be.
@@ -425,7 +437,12 @@ public func expectBidirectionalCollectionAssociatedTypes<X : BidirectionalCollec
425437
indexType: X.Index.Type,
426438
indexDistanceType: X.IndexDistance.Type,
427439
indicesType: X.Indices.Type
428-
) { }
440+
) where
441+
// FIXME(ABI)#8 (Associated Types with where clauses): there should be no constraints in
442+
// the 'where' clause, all of these should be required by the protocol.
443+
X.SubSequence : BidirectionalCollection,
444+
// X.SubSequence.Indices == X.Indices, // FIXME(ABI)#9 (Recursive Protocol Constraints): can't have this constraint now.
445+
X.Indices : BidirectionalCollection {}
429446

430447
/// Check that all associated types of a `RandomAccessCollection` are what we
431448
/// expect them to be.
@@ -436,7 +453,12 @@ public func expectRandomAccessCollectionAssociatedTypes<X : RandomAccessCollecti
436453
indexType: X.Index.Type,
437454
indexDistanceType: X.IndexDistance.Type,
438455
indicesType: X.Indices.Type
439-
) { }
456+
) where
457+
// FIXME(ABI)#10 (Associated Types with where clauses): there should be no constraints in
458+
// the 'where' clause, all of these should be required by the protocol.
459+
X.SubSequence : RandomAccessCollection,
460+
// X.SubSequence.Indices == X.Indices, // FIXME(ABI)#11 (Recursive Protocol Constraints): can't have this constraint now.
461+
X.Indices : RandomAccessCollection {}
440462

441463
public struct AssertionResult : CustomStringConvertible {
442464
init(isPass: Bool) {

stdlib/public/core/ArrayBufferProtocol.swift

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,10 @@
1616
internal protocol _ArrayBufferProtocol
1717
: MutableCollection, RandomAccessCollection {
1818

19-
associatedtype Indices = CountableRange<Int>
19+
associatedtype Indices
20+
// FIXME(ABI) (Revert Where Clauses): Remove this conformance
21+
: RandomAccessCollection
22+
= CountableRange<Int>
2023

2124
/// The type of elements stored in the buffer.
2225
associatedtype Element

stdlib/public/core/BidirectionalCollection.swift

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,9 @@ public protocol _BidirectionalIndexable : _Indexable {
6666
/// - If `i > c.startIndex && i <= c.endIndex`
6767
/// `c.index(after: c.index(before: i)) == i`.
6868
public protocol BidirectionalCollection : _BidirectionalIndexable, Collection
69-
where SubSequence: BidirectionalCollection, Indices: BidirectionalCollection {
69+
// FIXME(ABI) (Revert Where Clauses): Restore these
70+
// where SubSequence: BidirectionalCollection, Indices: BidirectionalCollection
71+
{
7072

7173
// TODO: swift-3-indexing-model - replaces functionality in BidirectionalIndex
7274
/// Returns the position immediately before the given index.
@@ -84,11 +86,17 @@ where SubSequence: BidirectionalCollection, Indices: BidirectionalCollection {
8486

8587
/// A sequence that can represent a contiguous subrange of the collection's
8688
/// elements.
87-
associatedtype SubSequence = BidirectionalSlice<Self>
89+
associatedtype SubSequence
90+
// FIXME(ABI) (Revert Where Clauses): Remove these conformances
91+
: _BidirectionalIndexable, Collection
92+
= BidirectionalSlice<Self>
8893

8994
/// A type that represents the indices that are valid for subscripting the
9095
/// collection, in ascending order.
91-
associatedtype Indices = DefaultBidirectionalIndices<Self>
96+
associatedtype Indices
97+
// FIXME(ABI) (Revert Where Clauses): Remove these conformances
98+
: _BidirectionalIndexable, Collection
99+
= DefaultBidirectionalIndices<Self>
92100

93101
/// The indices that are valid for subscripting the collection, in ascending
94102
/// order.

stdlib/public/core/Collection.swift

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -647,8 +647,8 @@ public struct IndexingIterator<
647647
/// count the number of contained elements, accessing its `count` property is
648648
/// an O(*n*) operation.
649649
public protocol Collection : _Indexable, Sequence
650-
where SubSequence: Collection, Indices: Collection,
651-
SubSequence.Index == Index
650+
// FIXME(ABI) (Revert Where Clauses): Restore these
651+
// where SubSequence: Collection, Indices: Collection,
652652
{
653653
/// A type that represents the number of steps between a pair of
654654
/// indices.
@@ -675,9 +675,15 @@ where SubSequence: Collection, Indices: Collection,
675675
/// This associated type appears as a requirement in the `Sequence`
676676
/// protocol, but it is restated here with stricter constraints. In a
677677
/// collection, the subsequence should also conform to `Collection`.
678-
associatedtype SubSequence = Slice<Self>
679-
where Iterator.Element == SubSequence.Iterator.Element,
680-
SubSequence.SubSequence == SubSequence
678+
associatedtype SubSequence
679+
// FIXME(ABI) (Revert Where Clauses): remove these conformances:
680+
: _IndexableBase, Sequence
681+
= Slice<Self>
682+
where SubSequence.SubSequence == SubSequence
683+
// FIXME(ABI) (Revert Where Clauses): and this where clause:
684+
, Iterator.Element == SubSequence.Iterator.Element
685+
, SubSequence.Index == Index
686+
681687

682688
// FIXME(ABI)#98 (Recursive Protocol Constraints):
683689
// FIXME(ABI)#99 (Associated Types with where clauses):
@@ -739,10 +745,15 @@ where SubSequence: Collection, Indices: Collection,
739745

740746
/// A type that represents the indices that are valid for subscripting the
741747
/// collection, in ascending order.
742-
associatedtype Indices = DefaultIndices<Self>
743-
where Indices.Iterator.Element == Index,
748+
associatedtype Indices
749+
// FIXME(ABI) (Revert Where Clauses): Remove these two conformances
750+
: _Indexable, Sequence
751+
= DefaultIndices<Self>
752+
where Indices.Iterator.Element == Index,
744753
Indices.Index == Index
745-
754+
// FIXME(ABI) (Revert Where Clauses): Remove this where clause
755+
, Indices.SubSequence == Indices
756+
746757
// FIXME(ABI)#100 (Recursive Protocol Constraints):
747758
// associatedtype Indices : Collection
748759
// where

stdlib/public/core/ExistentialCollection.swift.gyb

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -423,14 +423,21 @@ internal class _AnyRandomAccessCollectionBox<Element>
423423
% assert False, 'Unknown kind'
424424
% end
425425

426+
427+
426428
@_fixed_layout
427429
@_versioned
428430
internal final class _${Kind}Box<S : ${Kind}> : _Any${Kind}Box<S.Iterator.Element>
429-
% if Kind == 'Sequence':
430431
where
431432
S.SubSequence : ${Kind},
433+
// FIXME(ABI) (Revert Where Clauses): apply all this only to Sequence:
434+
% if Kind == 'Sequence':
432435
S.SubSequence.Iterator.Element == S.Iterator.Element,
433436
S.SubSequence.SubSequence == S.SubSequence
437+
// FIXME(ABI) (Revert Where Clauses): remove this else clause:
438+
% else:
439+
S.SubSequence.Indices : ${Kind},
440+
S.Indices : ${Kind}
434441
% end
435442
{
436443
internal typealias Element = S.Iterator.Element
@@ -1035,8 +1042,12 @@ public struct ${Self}<Element>
10351042
@_inlineable
10361043
public init<C : ${SubProtocol}>(_ base: C)
10371044
where
1038-
// FIXME(ABI)#101 (Associated Types with where clauses): these constraints should be applied to
1039-
// associated types of Collection.
1045+
// FIXME(ABI) (Revert Where Clauses): remove next 3 lines
1046+
C.SubSequence : ${SubProtocol},
1047+
C.SubSequence.Indices : ${SubProtocol},
1048+
C.Indices : ${SubProtocol},
1049+
// FIXME(ABI)#101 (Associated Types with where clauses): these constraints
1050+
// should be applied to associated types of Collection.
10401051
C.SubSequence.Iterator.Element == Element
10411052
{
10421053
// Traversal: ${Traversal}

stdlib/public/core/Mirror.swift

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,10 @@ public struct Mirror {
214214
children: C,
215215
displayStyle: DisplayStyle? = nil,
216216
ancestorRepresentation: AncestorRepresentation = .generated
217-
) where C.Iterator.Element == Child {
217+
) where C.Iterator.Element == Child
218+
// FIXME(ABI) (Revert Where Clauses): Remove these
219+
, C.SubSequence : Collection, C.SubSequence.Indices : Collection, C.Indices : Collection
220+
{
218221

219222
self.subjectType = Subject.self
220223
self._makeSuperclassMirror = Mirror._superclassIterator(
@@ -261,7 +264,10 @@ public struct Mirror {
261264
unlabeledChildren: C,
262265
displayStyle: DisplayStyle? = nil,
263266
ancestorRepresentation: AncestorRepresentation = .generated
264-
) {
267+
)
268+
// FIXME(ABI) (Revert Where Clauses): Remove these two clauses
269+
where C.SubSequence : Collection, C.Indices : Collection
270+
{
265271

266272
self.subjectType = Subject.self
267273
self._makeSuperclassMirror = Mirror._superclassIterator(

stdlib/public/core/MutableCollection.swift

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -197,8 +197,13 @@ public protocol _MutableIndexable : _Indexable {
197197
/// a[i] = x
198198
/// let y = x
199199
public protocol MutableCollection : _MutableIndexable, Collection
200-
where SubSequence: MutableCollection {
201-
associatedtype SubSequence = MutableSlice<Self>
200+
// FIXME(ABI) (Revert Where Clauses): restore this:
201+
// where SubSequence: MutableCollection
202+
{
203+
associatedtype SubSequence
204+
// FIXME(ABI) (Revert Where Clauses): remove this conformance:
205+
: Collection
206+
= MutableSlice<Self>
202207

203208
/// Accesses the element at the specified position.
204209
///

stdlib/public/core/RandomAccessCollection.swift

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,15 +48,22 @@ public protocol _RandomAccessIndexable : _BidirectionalIndexable {
4848
/// `distance(from:to:)` methods with O(1) efficiency.
4949
public protocol RandomAccessCollection :
5050
_RandomAccessIndexable, BidirectionalCollection
51-
where SubSequence: RandomAccessCollection, Indices: RandomAccessCollection
51+
// FIXME(ABI) (Revert Where Clauses): Restore this:
52+
// where SubSequence: RandomAccessCollection, Indices: RandomAccessCollection
5253
{
5354
/// A collection that represents a contiguous subrange of the collection's
5455
/// elements.
55-
associatedtype SubSequence = RandomAccessSlice<Self>
56+
associatedtype SubSequence
57+
// FIXME(ABI) (Revert Where Clauses): Remove these two constraints:
58+
: _RandomAccessIndexable, BidirectionalCollection
59+
= RandomAccessSlice<Self>
5660

5761
/// A type that represents the indices that are valid for subscripting the
5862
/// collection, in ascending order.
59-
associatedtype Indices = DefaultRandomAccessIndices<Self>
63+
associatedtype Indices
64+
// FIXME(ABI) (Revert Where Clauses): Remove these two constraints:
65+
: _RandomAccessIndexable, BidirectionalCollection
66+
= DefaultRandomAccessIndices<Self>
6067

6168
/// The indices that are valid for subscripting the collection, in ascending
6269
/// order.

0 commit comments

Comments
 (0)