Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SE-0157] Standard library uses of Recursive Protocol Constraints #11923

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -40,8 +40,8 @@ public struct CollectionMisuseResiliencyChecks {
}

% for inc, protocol, direction, end in (
% ('inc', '_Indexable', 'after', 'end'),
% ('dec', '_BidirectionalIndexable', 'before', 'start')):
% ('inc', 'Collection', 'after', 'end'),
% ('dec', 'BidirectionalCollection', 'before', 'start')):

/// Test that the elements of `instances` satisfy
/// ${'some of ' if inc == 'dec' else ''}the semantic
Expand Down Expand Up @@ -88,7 +88,7 @@ internal func _checkIncrementalAdvance<Instances, BaseCollection>(
${TRACE}
) where
Instances : Collection,
BaseCollection : _Indexable,
BaseCollection : Collection,
Instances.Iterator.Element == BaseCollection.Index {
for i in instances {
let d: BaseCollection.IndexDistance = sign > 0 ?
Expand Down Expand Up @@ -122,7 +122,7 @@ public func checkForwardIndex<Instances, BaseCollection>(
endIndex: Instances.Iterator.Element, ${TRACE}
) where
Instances : Collection,
BaseCollection : _Indexable,
BaseCollection : Collection,
Instances.Iterator.Element == BaseCollection.Index {

checkIncrementable(instances, of: baseCollection,
Expand All @@ -149,7 +149,7 @@ public func checkBidirectionalIndex<Instances, BaseCollection>(
${TRACE}
) where
Instances: Collection,
BaseCollection : _BidirectionalIndexable,
BaseCollection : BidirectionalCollection,
Instances.Iterator.Element == BaseCollection.Index {

checkForwardIndex(instances, of: baseCollection,
Expand Down Expand Up @@ -185,7 +185,7 @@ public func checkRandomAccessIndex<Instances, Distances, BaseCollection>(
) where
Instances : Collection,
Distances : Collection,
BaseCollection : _RandomAccessIndexable,
BaseCollection : RandomAccessCollection,
Instances.Iterator.Element == BaseCollection.Index,
Distances.Iterator.Element == BaseCollection.IndexDistance {

Expand Down Expand Up @@ -214,7 +214,7 @@ public func checkAdvancesAndDistances<Instances, Distances, BaseCollection>(
) where
Instances : Collection,
Distances : Collection,
BaseCollection : _Indexable,
BaseCollection : Collection,
Instances.Iterator.Element == BaseCollection.Index,
Distances.Iterator.Element == BaseCollection.IndexDistance {

Expand Down Expand Up @@ -257,8 +257,7 @@ public func checkCollection<${genericParam}, C : Collection>(
${TRACE},
resiliencyChecks: CollectionMisuseResiliencyChecks = .all,
sameValue: (${Element}, ${Element}) -> Bool
) where C.Iterator.Element == ${Element},
C.SubSequence : Collection {
) where C.Iterator.Element == ${Element} {

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

check${Traversal}Collection(
Expand All @@ -298,8 +296,7 @@ public func check${Traversal}Collection<
resiliencyChecks: CollectionMisuseResiliencyChecks = .all,
sameValue: (${Element}, ${Element}) -> Bool
) where
C.Iterator.Element == ${Element},
C.SubSequence : ${TraversalCollection} {
C.Iterator.Element == ${Element} {

checkOneLevelOf${Traversal}Collection(expected, collection, ${trace},
resiliencyChecks: resiliencyChecks, sameValue: sameValue)
Expand Down Expand Up @@ -504,8 +501,7 @@ ${genericParam}, S : ${TraversalCollection}
resiliencyChecks: CollectionMisuseResiliencyChecks = .all,
sameValue: (${Element}, ${Element}) -> Bool
) where
S.Iterator.Element == ${Element},
S.SubSequence : ${TraversalCollection} {
S.Iterator.Element == ${Element} {

let expectedArray = Array(expected)

Expand Down
Expand Up @@ -471,16 +471,9 @@ internal enum _SubSequenceSubscriptOnRangeMode {
%{
from gyb_stdlib_support import collectionForTraversal
def testConstraints(protocol):
if protocol == 'Collection':
subseq_as_collection = 'CollectionWithEquatableElement.SubSequence : Collection,'
else:
subseq_as_collection=''
return '''
C : %(protocol)s,
CollectionWithEquatableElement : %(protocol)s,
%(subseq_as_collection)s
C.SubSequence : %(protocol)s,
C.Indices : %(protocol)s,
CollectionWithEquatableElement.Iterator.Element : Equatable
''' % locals()

Expand Down
Expand Up @@ -121,8 +121,6 @@ extension TestSuite {
isFixedLengthCollection: Bool,
collectionIsBidirectional: Bool = false
) where
C.SubSequence : MutableCollection,
C.Indices : Collection,
CollectionWithEquatableElement.Iterator.Element : Equatable,
CollectionWithComparableElement.Iterator.Element : Comparable {

Expand Down Expand Up @@ -776,8 +774,6 @@ self.test("\(testNamePrefix).partition/InvalidOrderings") {
withUnsafeMutableBufferPointerIsSupported: Bool,
isFixedLengthCollection: Bool
) where
C.SubSequence : BidirectionalCollection & MutableCollection,
C.Indices : BidirectionalCollection,
CollectionWithEquatableElement.Iterator.Element : Equatable,
CollectionWithComparableElement.Iterator.Element : Comparable {

Expand Down Expand Up @@ -922,8 +918,6 @@ self.test("\(testNamePrefix).partition/DispatchesThrough_withUnsafeMutableBuffer
withUnsafeMutableBufferPointerIsSupported: Bool,
isFixedLengthCollection: Bool
) where
C.SubSequence : RandomAccessCollection & MutableCollection,
C.Indices : RandomAccessCollection,
CollectionWithEquatableElement.Iterator.Element : Equatable,
CollectionWithComparableElement.Iterator.Element : Comparable {

Expand Down
Expand Up @@ -462,10 +462,7 @@ extension TestSuite {
outOfBoundsIndexOffset: Int = 1,
collectionIsBidirectional: Bool = false
) where
C.SubSequence : Collection,
C.Indices : Collection,
CollectionWithEquatableElement.Iterator.Element : Equatable,
CollectionWithEquatableElement.SubSequence : Collection {
CollectionWithEquatableElement.Iterator.Element : Equatable {

var testNamePrefix = testNamePrefix

Expand Down Expand Up @@ -1180,8 +1177,6 @@ self.test("\(testNamePrefix).OperatorPlus") {
resiliencyChecks: CollectionMisuseResiliencyChecks = .all,
outOfBoundsIndexOffset: Int = 1
) where
C.SubSequence : BidirectionalCollection & RangeReplaceableCollection,
C.Indices : BidirectionalCollection,
CollectionWithEquatableElement.Iterator.Element : Equatable {

var testNamePrefix = testNamePrefix
Expand Down Expand Up @@ -1302,8 +1297,6 @@ self.test("\(testNamePrefix).removeLast(n: Int)/whereIndexIsBidirectional/remove
resiliencyChecks: CollectionMisuseResiliencyChecks = .all,
outOfBoundsIndexOffset: Int = 1
) where
C.SubSequence : RandomAccessCollection & RangeReplaceableCollection,
C.Indices : RandomAccessCollection,
CollectionWithEquatableElement.Iterator.Element : Equatable {

var testNamePrefix = testNamePrefix
Expand Down
Expand Up @@ -33,7 +33,6 @@ extension TestSuite {
collectionIsBidirectional: Bool = false
) where
C.SubSequence == C,
C.Indices : Collection,
CollectionWithEquatableElement.SubSequence == CollectionWithEquatableElement,
CollectionWithEquatableElement.Iterator.Element : Equatable {

Expand Down Expand Up @@ -165,7 +164,6 @@ extension TestSuite {
outOfBoundsIndexOffset: Int = 1
) where
C.SubSequence == C,
C.Indices : BidirectionalCollection,
CollectionWithEquatableElement.SubSequence == CollectionWithEquatableElement,
CollectionWithEquatableElement.Iterator.Element : Equatable {

Expand Down Expand Up @@ -310,7 +308,6 @@ extension TestSuite {
outOfBoundsIndexOffset: Int = 1
) where
C.SubSequence == C,
C.Indices : RandomAccessCollection,
CollectionWithEquatableElement.SubSequence == CollectionWithEquatableElement,
CollectionWithEquatableElement.Iterator.Element : Equatable {

Expand Down
Expand Up @@ -1536,13 +1536,7 @@ extension TestSuite {

resiliencyChecks: CollectionMisuseResiliencyChecks = .all
) where
SequenceWithEquatableElement.Iterator.Element : Equatable,
SequenceWithEquatableElement.SubSequence : Sequence,
SequenceWithEquatableElement.SubSequence.Iterator.Element
== SequenceWithEquatableElement.Iterator.Element,
S.SubSequence : Sequence,
S.SubSequence.Iterator.Element == S.Iterator.Element,
S.SubSequence.SubSequence == S.SubSequence {
SequenceWithEquatableElement.Iterator.Element : Equatable {

var testNamePrefix = testNamePrefix

Expand Down
44 changes: 6 additions & 38 deletions stdlib/private/StdlibUnittest/StdlibUnittest.swift.gyb
Expand Up @@ -378,25 +378,14 @@ public func expectTrapping<Bound>(
public func expectType<T>(_: T.Type, _ x: inout T) {}
public func expectEqualType<T>(_: T.Type, _: T.Type) {}

public func expectSequenceType<X : Sequence>(_ x: X) -> X
where
X.SubSequence : Sequence,
X.SubSequence.Iterator.Element == X.Iterator.Element,
X.SubSequence.SubSequence == X.SubSequence {
public func expectSequenceType<X : Sequence>(_ x: X) -> X {
return x
}

% for Mutable in ['', 'Mutable']:
public func expect${Mutable}CollectionType<X : ${Mutable}Collection>(
_ x: X.Type
) where
// FIXME(ABI)#2 (Associated Types with where clauses): there should be no constraints in
// the 'where' clause, all of these should be required by the protocol.
% if Mutable == '':
X.SubSequence : Collection,
% end
// X.SubSequence.Indices == X.Indices, // FIXME(ABI)#3 (Recursive Protocol Constraints): can't have this constraint now.
X.Indices : Collection {}
) {}
% end

/// A slice is a `Collection` that when sliced returns an instance of
Expand All @@ -417,13 +406,7 @@ public func expectSequenceAssociatedTypes<X : Sequence>(
sequenceType: X.Type,
iteratorType: X.Iterator.Type,
subSequenceType: X.SubSequence.Type
) where
// FIXME(ABI)#4 (Associated Types with where clauses): there should be no constraints in
// the 'where' clause, all of these should be required by the protocol.
X.SubSequence : Sequence,
X.SubSequence.Iterator.Element == X.Iterator.Element,
// X.SubSequence.Indices == X.Indices, // FIXME(ABI)#5 (Recursive Protocol Constraints): can't have this constraint now.
X.SubSequence.SubSequence == X.SubSequence {}
) {}

/// Check that all associated types of a `Collection` are what we expect them
/// to be.
Expand All @@ -434,12 +417,7 @@ public func expectCollectionAssociatedTypes<X : Collection>(
indexType: X.Index.Type,
indexDistanceType: X.IndexDistance.Type,
indicesType: X.Indices.Type
) where
// FIXME(ABI)#6 (Associated Types with where clauses): there should be no constraints in
// the 'where' clause, all of these should be required by the protocol.
X.SubSequence : Collection,
// X.SubSequence.Indices == X.Indices, // FIXME(ABI)#7 (Recursive Protocol Constraints): can't have this constraint now.
X.Indices : Collection {}
) {}

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

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

public struct AssertionResult : CustomStringConvertible {
init(isPass: Bool) {
Expand Down
1 change: 1 addition & 0 deletions stdlib/public/SDK/Dispatch/Data.swift
Expand Up @@ -299,6 +299,7 @@ public struct DispatchData : RandomAccessCollection, _ObjectiveCBridgeable {
}

public struct DispatchDataIterator : IteratorProtocol, Sequence {
public typealias Element = UInt8

/// Create an iterator over the given DispatchData
public init(_data: DispatchData) {
Expand Down
2 changes: 1 addition & 1 deletion stdlib/public/SDK/Foundation/IndexSet.swift
Expand Up @@ -371,7 +371,7 @@ public struct IndexSet : ReferenceConvertible, Equatable, BidirectionalCollectio
public func intersects(integersIn range: CountableClosedRange<Element>) -> Bool { return self.intersects(integersIn: Range(range)) }

// MARK: -
// Indexable
// Collection

public func index(after i: Index) -> Index {
if i.value + 1 == i.extent.upperBound {
Expand Down
5 changes: 1 addition & 4 deletions stdlib/public/core/ArrayBufferProtocol.swift
Expand Up @@ -16,10 +16,7 @@
internal protocol _ArrayBufferProtocol
: MutableCollection, RandomAccessCollection {

associatedtype Indices
// FIXME(ABI) (Revert Where Clauses): Remove this conformance
: RandomAccessCollection
= CountableRange<Int>
associatedtype Indices = CountableRange<Int>

/// Create an empty buffer.
init()
Expand Down