Skip to content

Commit f6f3ed0

Browse files
Add Collection constraints via protocol where clauses (#9374)
1 parent 8d888b5 commit f6f3ed0

File tree

8 files changed

+26
-55
lines changed

8 files changed

+26
-55
lines changed

stdlib/public/core/ArrayBufferProtocol.swift

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

19-
associatedtype Indices : RandomAccessCollection = CountableRange<Int>
19+
associatedtype Indices = CountableRange<Int>
2020

2121
/// The type of elements stored in the buffer.
2222
associatedtype Element

stdlib/public/core/BidirectionalCollection.swift

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,8 @@ public protocol _BidirectionalIndexable : _Indexable {
6565
/// `c.index(before: c.index(after: i)) == i`.
6666
/// - If `i > c.startIndex && i <= c.endIndex`
6767
/// `c.index(after: c.index(before: i)) == i`.
68-
public protocol BidirectionalCollection
69-
: _BidirectionalIndexable, Collection {
68+
public protocol BidirectionalCollection : _BidirectionalIndexable, Collection
69+
where SubSequence: BidirectionalCollection, Indices: BidirectionalCollection {
7070

7171
// TODO: swift-3-indexing-model - replaces functionality in BidirectionalIndex
7272
/// Returns the position immediately before the given index.
@@ -84,17 +84,11 @@ public protocol BidirectionalCollection
8484

8585
/// A sequence that can represent a contiguous subrange of the collection's
8686
/// elements.
87-
associatedtype SubSequence : _BidirectionalIndexable, Collection
88-
= BidirectionalSlice<Self>
89-
// FIXME(ABI)#93 (Recursive Protocol Constraints):
90-
// associatedtype SubSequence : BidirectionalCollection
87+
associatedtype SubSequence = BidirectionalSlice<Self>
9188

9289
/// A type that represents the indices that are valid for subscripting the
9390
/// collection, in ascending order.
94-
associatedtype Indices : _BidirectionalIndexable, Collection
95-
= DefaultBidirectionalIndices<Self>
96-
// FIXME(ABI)#95 (Recursive Protocol Constraints):
97-
// associatedtype Indices : BidirectionalCollection
91+
associatedtype Indices = DefaultBidirectionalIndices<Self>
9892

9993
/// The indices that are valid for subscripting the collection, in ascending
10094
/// order.

stdlib/public/core/Collection.swift

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -646,7 +646,11 @@ public struct IndexingIterator<
646646
/// forward or bidirectional collection must traverse the entire collection to
647647
/// count the number of contained elements, accessing its `count` property is
648648
/// an O(*n*) operation.
649-
public protocol Collection : _Indexable, Sequence {
649+
public protocol Collection : _Indexable, Sequence
650+
where SubSequence: Collection, Indices: Collection,
651+
SubSequence.Index == Index,
652+
SubSequence.Iterator.Element == Iterator.Element
653+
{
650654
/// A type that represents the number of steps between a pair of
651655
/// indices.
652656
associatedtype IndexDistance = Int
@@ -672,10 +676,10 @@ public protocol Collection : _Indexable, Sequence {
672676
/// This associated type appears as a requirement in the `Sequence`
673677
/// protocol, but it is restated here with stricter constraints. In a
674678
/// collection, the subsequence should also conform to `Collection`.
675-
associatedtype SubSequence : _IndexableBase, Sequence = Slice<Self>
676-
where Self.SubSequence.Index == Index,
677-
Self.Iterator.Element == Self.SubSequence.Iterator.Element,
679+
associatedtype SubSequence = Slice<Self>
680+
where Iterator.Element == SubSequence.Iterator.Element,
678681
SubSequence.SubSequence == SubSequence
682+
679683
// FIXME(ABI)#98 (Recursive Protocol Constraints):
680684
// FIXME(ABI)#99 (Associated Types with where clauses):
681685
// associatedtype SubSequence : Collection
@@ -736,10 +740,9 @@ public protocol Collection : _Indexable, Sequence {
736740

737741
/// A type that represents the indices that are valid for subscripting the
738742
/// collection, in ascending order.
739-
associatedtype Indices : _Indexable, Sequence = DefaultIndices<Self>
743+
associatedtype Indices = DefaultIndices<Self>
740744
where Indices.Iterator.Element == Index,
741-
Indices.Index == Index,
742-
Indices.SubSequence == Indices
745+
Indices.Index == Index
743746

744747
// FIXME(ABI)#100 (Recursive Protocol Constraints):
745748
// associatedtype Indices : Collection

stdlib/public/core/ExistentialCollection.swift.gyb

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -426,14 +426,11 @@ internal class _AnyRandomAccessCollectionBox<Element>
426426
@_fixed_layout
427427
@_versioned
428428
internal final class _${Kind}Box<S : ${Kind}> : _Any${Kind}Box<S.Iterator.Element>
429+
% if Kind == 'Sequence':
429430
where
430431
S.SubSequence : ${Kind},
431-
% if Kind == 'Sequence':
432432
S.SubSequence.Iterator.Element == S.Iterator.Element,
433433
S.SubSequence.SubSequence == S.SubSequence
434-
% else:
435-
S.SubSequence.Indices : ${Kind},
436-
S.Indices : ${Kind}
437434
% end
438435
{
439436
internal typealias Element = S.Iterator.Element
@@ -1040,10 +1037,7 @@ public struct ${Self}<Element>
10401037
where
10411038
// FIXME(ABI)#101 (Associated Types with where clauses): these constraints should be applied to
10421039
// associated types of Collection.
1043-
C.SubSequence : ${SubProtocol},
1044-
C.SubSequence.Iterator.Element == Element,
1045-
C.SubSequence.Indices : ${SubProtocol},
1046-
C.Indices : ${SubProtocol}
1040+
C.SubSequence.Iterator.Element == Element
10471041
{
10481042
// Traversal: ${Traversal}
10491043
// SubTraversal: ${SubTraversal}

stdlib/public/core/Mirror.swift

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -214,13 +214,7 @@ public struct Mirror {
214214
children: C,
215215
displayStyle: DisplayStyle? = nil,
216216
ancestorRepresentation: AncestorRepresentation = .generated
217-
) where
218-
C.Iterator.Element == Child,
219-
// FIXME(ABI)#47 (Associated Types with where clauses): these constraints should be applied to
220-
// associated types of Collection.
221-
C.SubSequence : Collection,
222-
C.SubSequence.Indices : Collection,
223-
C.Indices : Collection {
217+
) where C.Iterator.Element == Child {
224218

225219
self.subjectType = Subject.self
226220
self._makeSuperclassMirror = Mirror._superclassIterator(
@@ -267,11 +261,7 @@ public struct Mirror {
267261
unlabeledChildren: C,
268262
displayStyle: DisplayStyle? = nil,
269263
ancestorRepresentation: AncestorRepresentation = .generated
270-
) where
271-
// FIXME(ABI)#48 (Associated Types with where clauses): these constraints should be applied to
272-
// associated types of Collection.
273-
C.SubSequence : Collection,
274-
C.Indices : Collection {
264+
) {
275265

276266
self.subjectType = Subject.self
277267
self._makeSuperclassMirror = Mirror._superclassIterator(

stdlib/public/core/MutableCollection.swift

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -196,14 +196,9 @@ public protocol _MutableIndexable : _Indexable {
196196
/// // Must be equivalent to:
197197
/// a[i] = x
198198
/// let y = x
199-
public protocol MutableCollection : _MutableIndexable, Collection {
200-
// FIXME(ABI)#181: should be constrained to MutableCollection
201-
// (<rdar://problem/20715009> Implement recursive protocol
202-
// constraints)
203-
/// A collection that represents a contiguous subrange of the collection's
204-
/// elements.
205-
associatedtype SubSequence : Collection /*: MutableCollection*/
206-
= MutableSlice<Self>
199+
public protocol MutableCollection : _MutableIndexable, Collection
200+
where SubSequence: MutableCollection {
201+
associatedtype SubSequence = MutableSlice<Self>
207202

208203
/// Accesses the element at the specified position.
209204
///

stdlib/public/core/RandomAccessCollection.swift

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -48,20 +48,15 @@ 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
5152
{
5253
/// A collection that represents a contiguous subrange of the collection's
5354
/// elements.
54-
associatedtype SubSequence : _RandomAccessIndexable, BidirectionalCollection
55-
= RandomAccessSlice<Self>
56-
// FIXME(ABI)#102 (Recursive Protocol Constraints):
57-
// associatedtype SubSequence : RandomAccessCollection
55+
associatedtype SubSequence = RandomAccessSlice<Self>
5856

5957
/// A type that represents the indices that are valid for subscripting the
6058
/// collection, in ascending order.
61-
associatedtype Indices : _RandomAccessIndexable, BidirectionalCollection
62-
= DefaultRandomAccessIndices<Self>
63-
// FIXME(ABI)#103 (Recursive Protocol Constraints):
64-
// associatedtype Indices : RandomAccessCollection
59+
associatedtype Indices = DefaultRandomAccessIndices<Self>
6560

6661
/// The indices that are valid for subscripting the collection, in ascending
6762
/// order.

validation-test/stdlib/CollectionDiagnostics.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ struct CollectionWithBadSubSequence : Collection {
2323
fatalError("unreachable")
2424
}
2525

26-
// expected-note@+3 {{possibly intended match 'CollectionWithBadSubSequence.SubSequence' (aka 'OpaqueValue<Int8>') does not conform to 'Sequence'}}
26+
// expected-note@+3 {{possibly intended match}}
2727
// expected-note@+2 {{possibly intended match}}
2828
// expected-note@+1 {{possibly intended match}}
2929
typealias SubSequence = OpaqueValue<Int8>

0 commit comments

Comments
 (0)