Skip to content

Commit 4b01574

Browse files
authored
Merge pull request #3510 from natecook1000/nc-bidirectionalfix
[stdlib] Fix type inference issue for BidirectionalCollection
2 parents 8cfefd3 + 3d2d342 commit 4b01574

File tree

2 files changed

+42
-2
lines changed

2 files changed

+42
-2
lines changed

stdlib/public/core/BidirectionalCollection.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ extension BidirectionalIndexable {
167167
/// Supply the default "slicing" `subscript` for `BidirectionalCollection`
168168
/// models that accept the default associated `SubSequence`,
169169
/// `BidirectionalSlice<Self>`.
170-
extension BidirectionalIndexable where SubSequence == BidirectionalSlice<Self> {
170+
extension BidirectionalCollection where SubSequence == BidirectionalSlice<Self> {
171171
public subscript(bounds: Range<Index>) -> BidirectionalSlice<Self> {
172172
_failEarlyRangeCheck(bounds, bounds: startIndex..<endIndex)
173173
return BidirectionalSlice(base: self, bounds: bounds)

validation-test/stdlib/CollectionType.swift.gyb

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
% import os.path
99
% import gyb
10-
% from gyb_stdlib_support import TRAVERSALS, collectionForTraversal
10+
% from gyb_stdlib_support import TRAVERSALS, collectionForTraversal, sliceTypeName, defaultIndicesForTraversal
1111

1212
import StdlibUnittest
1313
import StdlibCollectionUnittest
@@ -788,4 +788,44 @@ CollectionTypeTests.test("_withUnsafeMutableBufferPointerIfSupported/dispatch")
788788
expectCustomizable(tester, tester.log._withUnsafeMutableBufferPointerIfSupported)
789789
}
790790

791+
792+
//===----------------------------------------------------------------------===//
793+
// Associated Type Inference
794+
//===----------------------------------------------------------------------===//
795+
796+
// This section verifies that only the absolutely minimal protocol requirements
797+
// are required to declare each kind of collection. All remaining requirements
798+
// and associated types should be either inferred from the declared properties
799+
// and methods or inherited as defaults via protocol extension.
800+
801+
struct FatalIndex : Comparable { }
802+
func <(lhs: FatalIndex, rhs: FatalIndex) -> Bool { fatalError() }
803+
func ==(lhs: FatalIndex, rhs: FatalIndex) -> Bool { fatalError() }
804+
805+
% for Traversal in TRAVERSALS:
806+
% Collection = collectionForTraversal(Traversal)
807+
% Self = 'Fatal' + Collection
808+
% Slice = sliceTypeName(Traversal, False, False)
809+
% Indices = defaultIndicesForTraversal(Traversal)
810+
811+
struct ${Self}<T> : ${Collection} {
812+
var startIndex: FatalIndex { fatalError() }
813+
var endIndex: FatalIndex { fatalError() }
814+
subscript(i: FatalIndex) -> T { fatalError() }
815+
func index(after i: FatalIndex) -> FatalIndex { fatalError() }
816+
% if Traversal in ['Bidirectional', 'RandomAccess']:
817+
func index(before i: FatalIndex) -> FatalIndex { fatalError() }
818+
% end
819+
}
820+
821+
CollectionTypeTests.test("AssociatedTypes/${Collection}") {
822+
expectTrue(${Self}<Void>.Index.self == FatalIndex.self)
823+
expectTrue(${Self}<Void>.Indices.self == ${Indices}<${Self}<Void>>.self)
824+
expectTrue(${Self}<Void>.IndexDistance.self == Int.self)
825+
expectTrue(${Self}<Void>.Iterator.self == IndexingIterator<${Self}<Void>>.self)
826+
expectTrue(${Self}<Void>.SubSequence.self == ${Slice}<${Self}<Void>>.self)
827+
}
828+
% end
829+
830+
791831
runAllTests()

0 commit comments

Comments
 (0)