Skip to content

Commit 4e7b4de

Browse files
[4.1][stdlib] Fix signature error in two IndexDistance compatibility shims (#14444)
* [stdlib] Collection compatibility shim fixes (#14342) * Fix compatibility shims for IndexDistance * Add a second test for implied IndexDistance, Bidirectional conformance * Merge pull request #14414 from airspeedswift/move-collection-compat
1 parent 93facda commit 4e7b4de

File tree

2 files changed

+112
-3
lines changed

2 files changed

+112
-3
lines changed

stdlib/public/core/Collection.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1707,11 +1707,11 @@ extension Collection {
17071707
return index(i, offsetBy: Int(n))
17081708
}
17091709
@available(*, deprecated, message: "all index distances are now of type Int")
1710-
public func formIndex<T: BinaryInteger>(_ i: Index, offsetBy n: T) {
1711-
return formIndex(i, offsetBy: Int(n))
1710+
public func formIndex<T: BinaryInteger>(_ i: inout Index, offsetBy n: T) {
1711+
return formIndex(&i, offsetBy: Int(n))
17121712
}
17131713
@available(*, deprecated, message: "all index distances are now of type Int")
1714-
public func index<T: BinaryInteger>(_ i: Index, offsetBy n: T, limitedBy limit: Index) -> Index {
1714+
public func index<T: BinaryInteger>(_ i: Index, offsetBy n: T, limitedBy limit: Index) -> Index? {
17151715
return index(i, offsetBy: Int(n), limitedBy: limit)
17161716
}
17171717
@available(*, deprecated, message: "all index distances are now of type Int")
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
// RUN: rm -rf %t ; mkdir -p %t
2+
// RUN: %target-build-swift %s -o %t/a.out3 -swift-version 3 && %target-run %t/a.out3
3+
// RUN: %target-build-swift %s -o %t/a.out4 -swift-version 4 && %target-run %t/a.out4
4+
5+
// REQUIRES: executable_test
6+
7+
import StdlibUnittest
8+
import StdlibCollectionUnittest
9+
10+
//===--- MyCollection -----------------------------------------------------===//
11+
/// A simple collection that attempts to use an Int16 IndexDistance
12+
struct MyCollection<Element>: Collection {
13+
var _elements: [Element]
14+
15+
typealias IndexDistance = Int16
16+
typealias Index = Int16
17+
18+
var startIndex: Index { return 0 }
19+
var endIndex: Index { return numericCast(_elements.count) }
20+
21+
subscript(i: Index) -> Element { return _elements[Int(i)] }
22+
23+
func index(after: Index) -> Index { return after+1 }
24+
}
25+
26+
//===--- MyBidiCollection -------------------------------------------------===//
27+
/// A simple collection that attempts to use an Int16 IndexDistance
28+
struct MyBidiCollection<Element>: BidirectionalCollection {
29+
var _elements: [Element]
30+
31+
typealias Index = Int64
32+
33+
var startIndex: Index { return 0 }
34+
var endIndex: Index { return numericCast(_elements.count) }
35+
36+
subscript(i: Index) -> Element { return _elements[Int(i)] }
37+
38+
func index(after: Index) -> Index { return after+1 }
39+
func index(before: Index) -> Index { return before-1 }
40+
func index(_ i: Index, advancedBy d: Int64) -> Index { return i+d }
41+
}
42+
43+
44+
let CollectionDistance = TestSuite("Collection.IndexDistance")
45+
46+
CollectionDistance.test("Int16/distance") {
47+
let c = MyCollection<Int>(_elements: [1,2,3])
48+
let d: Int16 = c.distance(from: c.startIndex, to: c.endIndex)
49+
expectEqual(3, d)
50+
// without type context, you now get an Int
51+
var i = c.distance(from: c.startIndex, to: c.endIndex)
52+
expectType(Int.self, &i)
53+
expectType(MyCollection<Int>.IndexDistance.self, &i)
54+
}
55+
56+
CollectionDistance.test("Int16/advance") {
57+
let c = MyCollection<Int>(_elements: [1,2,3])
58+
let d: Int16 = 1
59+
var i = c.index(c.startIndex, offsetBy: d)
60+
expectEqual(1, i)
61+
c.formIndex(&i, offsetBy: d)
62+
expectEqual(2, i)
63+
let j = c.index(c.startIndex, offsetBy: d, limitedBy: c.endIndex)
64+
expectEqual(1, j)
65+
var b = c.formIndex(&i, offsetBy: d, limitedBy: c.endIndex)
66+
expectTrue(b)
67+
expectEqual(3, i)
68+
b = c.formIndex(&i, offsetBy: d+5, limitedBy: c.endIndex)
69+
expectFalse(b)
70+
expectEqual(3, i)
71+
let k = c.index(c.startIndex, offsetBy: d+5, limitedBy: c.endIndex)
72+
expectEqual(nil, k)
73+
74+
checkCollection(c, [1,2,3], stackTrace: SourceLocStack()) { $0 == $1 }
75+
}
76+
77+
CollectionDistance.test("Int64/distance") {
78+
let c = MyBidiCollection<Int>(_elements: [1,2,3])
79+
let d: Int16 = c.distance(from: c.startIndex, to: c.endIndex)
80+
expectEqual(3, d)
81+
// without type context, you now get an Int
82+
var i = c.distance(from: c.startIndex, to: c.endIndex)
83+
expectType(Int.self, &i)
84+
expectType(MyCollection<Int>.IndexDistance.self, &i)
85+
}
86+
87+
CollectionDistance.test("Int64/advance") {
88+
let c = MyBidiCollection<Int>(_elements: [1,2,3])
89+
let d: Int16 = 1
90+
var i = c.index(c.startIndex, offsetBy: d)
91+
expectEqual(1, i)
92+
c.formIndex(&i, offsetBy: d)
93+
expectEqual(2, i)
94+
let j = c.index(c.startIndex, offsetBy: d, limitedBy: c.endIndex)
95+
expectEqual(1, j)
96+
var b = c.formIndex(&i, offsetBy: d, limitedBy: c.endIndex)
97+
expectTrue(b)
98+
expectEqual(3, i)
99+
b = c.formIndex(&i, offsetBy: d+5, limitedBy: c.endIndex)
100+
expectFalse(b)
101+
expectEqual(3, i)
102+
let k = c.index(c.startIndex, offsetBy: d+5, limitedBy: c.endIndex)
103+
expectEqual(nil, k)
104+
105+
checkCollection(c, [1,2,3], stackTrace: SourceLocStack()) { $0 == $1 }
106+
checkBidirectionalCollection(c, [1,2,3])
107+
}
108+
109+
runAllTests()

0 commit comments

Comments
 (0)