Skip to content

[stdlib] Fix type inference issue for BidirectionalCollection #3510

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

Merged
merged 2 commits into from
Jul 14, 2016
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion stdlib/public/core/BidirectionalCollection.swift
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ extension BidirectionalIndexable {
/// Supply the default "slicing" `subscript` for `BidirectionalCollection`
/// models that accept the default associated `SubSequence`,
/// `BidirectionalSlice<Self>`.
extension BidirectionalIndexable where SubSequence == BidirectionalSlice<Self> {
extension BidirectionalCollection where SubSequence == BidirectionalSlice<Self> {
public subscript(bounds: Range<Index>) -> BidirectionalSlice<Self> {
_failEarlyRangeCheck(bounds, bounds: startIndex..<endIndex)
return BidirectionalSlice(base: self, bounds: bounds)
Expand Down
42 changes: 41 additions & 1 deletion validation-test/stdlib/CollectionType.swift.gyb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

% import os.path
% import gyb
% from gyb_stdlib_support import TRAVERSALS, collectionForTraversal
% from gyb_stdlib_support import TRAVERSALS, collectionForTraversal, sliceTypeName, defaultIndicesForTraversal

import StdlibUnittest
import StdlibCollectionUnittest
Expand Down Expand Up @@ -788,4 +788,44 @@ CollectionTypeTests.test("_withUnsafeMutableBufferPointerIfSupported/dispatch")
expectCustomizable(tester, tester.log._withUnsafeMutableBufferPointerIfSupported)
}


//===----------------------------------------------------------------------===//
// Associated Type Inference
//===----------------------------------------------------------------------===//

// This section verifies that only the absolutely minimal protocol requirements
// are required to declare each kind of collection. All remaining requirements
// and associated types should be either inferred from the declared properties
// and methods or inherited as defaults via protocol extension.

struct FatalIndex : Comparable { }
func <(lhs: FatalIndex, rhs: FatalIndex) -> Bool { fatalError() }
func ==(lhs: FatalIndex, rhs: FatalIndex) -> Bool { fatalError() }

% for Traversal in TRAVERSALS:
% Collection = collectionForTraversal(Traversal)
% Self = 'Fatal' + Collection
% Slice = sliceTypeName(Traversal, False, False)
% Indices = defaultIndicesForTraversal(Traversal)

struct ${Self}<T> : ${Collection} {
var startIndex: FatalIndex { fatalError() }
var endIndex: FatalIndex { fatalError() }
subscript(i: FatalIndex) -> T { fatalError() }
func index(after i: FatalIndex) -> FatalIndex { fatalError() }
% if Traversal in ['Bidirectional', 'RandomAccess']:
func index(before i: FatalIndex) -> FatalIndex { fatalError() }
% end
}

CollectionTypeTests.test("AssociatedTypes/${Collection}") {
expectTrue(${Self}<Void>.Index.self == FatalIndex.self)
expectTrue(${Self}<Void>.Indices.self == ${Indices}<${Self}<Void>>.self)
expectTrue(${Self}<Void>.IndexDistance.self == Int.self)
expectTrue(${Self}<Void>.Iterator.self == IndexingIterator<${Self}<Void>>.self)
expectTrue(${Self}<Void>.SubSequence.self == ${Slice}<${Self}<Void>>.self)
}
% end


runAllTests()