Skip to content

Commit 4ddac3f

Browse files
[stdlib] Eradicate IndexDistance associated type (#12641)
* Eradicate IndexDistance associated type, replacing with Int everywhere * Consistently use Int for ExistentialCollection’s IndexDistance type. * Fix test for IndexDistance removal * Remove a handful of no-longer-needed explicit types * Add compatibility shims for non-Int index distances * Test compatibility shim * Move IndexDistance typealias into the Collection protocol
1 parent ddac6ab commit 4ddac3f

File tree

60 files changed

+268
-302
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

60 files changed

+268
-302
lines changed

benchmark/single-source/StaticArray.swift

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,6 @@ struct StaticArray<
5858
func count() -> Int { return values.count() }
5959

6060
typealias Index = Int
61-
typealias IndexDistance = Int
6261
let startIndex: Int = 0
6362
var endIndex: Int { return count()}
6463

stdlib/private/StdlibCollectionUnittest/CheckCollectionInstance.swift.gyb

Lines changed: 31 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -56,11 +56,11 @@ public func check${inc.capitalize()}rementable<Instances, BaseCollection>(
5656
_ instances: Instances,
5757
of baseCollection: BaseCollection,
5858
equalityOracle: (Instances.Index, Instances.Index) -> Bool,
59-
${end}Index: Instances.Iterator.Element, ${TRACE}
59+
${end}Index: Instances.Element, ${TRACE}
6060
) where
6161
Instances : Collection,
6262
BaseCollection : ${protocol},
63-
Instances.Iterator.Element == BaseCollection.Index {
63+
Instances.Element == BaseCollection.Index {
6464

6565
checkEquatable(instances, oracle: equalityOracle, ${trace})
6666
for i in instances {
@@ -82,20 +82,20 @@ internal func _checkIncrementalAdvance<Instances, BaseCollection>(
8282
_ instances: Instances,
8383
of baseCollection : BaseCollection,
8484
equalityOracle: (Instances.Index, Instances.Index) -> Bool,
85-
limit: Instances.Iterator.Element,
86-
sign: BaseCollection.IndexDistance, // 1 or -1
87-
next: (Instances.Iterator.Element) -> Instances.Iterator.Element,
85+
limit: Instances.Element,
86+
sign: Int, // 1 or -1
87+
next: (Instances.Element) -> Instances.Element,
8888
${TRACE}
8989
) where
9090
Instances : Collection,
9191
BaseCollection : Collection,
92-
Instances.Iterator.Element == BaseCollection.Index {
92+
Instances.Element == BaseCollection.Index {
9393
for i in instances {
94-
let d: BaseCollection.IndexDistance = sign > 0 ?
94+
let d: Int = sign > 0 ?
9595
baseCollection.distance(from: i, to: limit) :
9696
-baseCollection.distance(from: limit, to: i)
9797

98-
var offset: BaseCollection.IndexDistance = 0
98+
var offset: Int = 0
9999
for _ in 0...Int64(d * sign) {
100100
let j = baseCollection.index(i, offsetBy: offset)
101101
let k = baseCollection.index(i, offsetBy: offset + sign, limitedBy: limit) ?? limit
@@ -119,11 +119,11 @@ public func checkForwardIndex<Instances, BaseCollection>(
119119
_ instances: Instances,
120120
of baseCollection: BaseCollection,
121121
equalityOracle: (Instances.Index, Instances.Index) -> Bool,
122-
endIndex: Instances.Iterator.Element, ${TRACE}
122+
endIndex: Instances.Element, ${TRACE}
123123
) where
124124
Instances : Collection,
125125
BaseCollection : Collection,
126-
Instances.Iterator.Element == BaseCollection.Index {
126+
Instances.Element == BaseCollection.Index {
127127

128128
checkIncrementable(instances, of: baseCollection,
129129
equalityOracle: equalityOracle, endIndex: endIndex, ${trace})
@@ -144,13 +144,13 @@ public func checkBidirectionalIndex<Instances, BaseCollection>(
144144
_ instances: Instances,
145145
of baseCollection: BaseCollection,
146146
equalityOracle: (Instances.Index, Instances.Index) -> Bool,
147-
startIndex: Instances.Iterator.Element,
148-
endIndex: Instances.Iterator.Element,
147+
startIndex: Instances.Element,
148+
endIndex: Instances.Element,
149149
${TRACE}
150150
) where
151151
Instances: Collection,
152152
BaseCollection : BidirectionalCollection,
153-
Instances.Iterator.Element == BaseCollection.Index {
153+
Instances.Element == BaseCollection.Index {
154154

155155
checkForwardIndex(instances, of: baseCollection,
156156
equalityOracle: equalityOracle, endIndex: endIndex)
@@ -176,18 +176,18 @@ public func checkRandomAccessIndex<Instances, Distances, BaseCollection>(
176176
_ instances: Instances, distances: Distances,
177177
of baseCollection: BaseCollection,
178178
distanceOracle:
179-
(Instances.Index, Instances.Index) -> Distances.Iterator.Element,
179+
(Instances.Index, Instances.Index) -> Distances.Element,
180180
advanceOracle:
181-
(Instances.Index, Distances.Index) -> Instances.Iterator.Element,
181+
(Instances.Index, Distances.Index) -> Instances.Element,
182182
startIndex: Instances.Iterator.Element,
183183
endIndex: Instances.Iterator.Element,
184184
${TRACE}
185185
) where
186186
Instances : Collection,
187187
Distances : Collection,
188188
BaseCollection : RandomAccessCollection,
189-
Instances.Iterator.Element == BaseCollection.Index,
190-
Distances.Iterator.Element == BaseCollection.IndexDistance {
189+
Instances.Element == BaseCollection.Index,
190+
Distances.Element == Int {
191191

192192
checkBidirectionalIndex(instances, of: baseCollection,
193193
equalityOracle: { distanceOracle($0, $1) == 0 },
@@ -207,16 +207,16 @@ public func checkAdvancesAndDistances<Instances, Distances, BaseCollection>(
207207
_ instances: Instances, distances: Distances,
208208
of baseCollection: BaseCollection,
209209
distanceOracle:
210-
(Instances.Index, Instances.Index) -> Distances.Iterator.Element,
210+
(Instances.Index, Instances.Index) -> Distances.Element,
211211
advanceOracle:
212-
(Instances.Index, Distances.Index) -> Instances.Iterator.Element,
212+
(Instances.Index, Distances.Index) -> Instances.Element,
213213
${TRACE}
214214
) where
215215
Instances : Collection,
216216
Distances : Collection,
217217
BaseCollection : Collection,
218-
Instances.Iterator.Element == BaseCollection.Index,
219-
Distances.Iterator.Element == BaseCollection.IndexDistance {
218+
Instances.Element == BaseCollection.Index,
219+
Distances.Element == Int {
220220

221221
checkComparable(
222222
instances,
@@ -246,7 +246,7 @@ public func checkAdvancesAndDistances<Instances, Distances, BaseCollection>(
246246
// picked up when the caller passes a literal), and another that
247247
// accepts any appropriate Collection type.
248248
% for genericParam, Element, Expected in [
249-
% ('Expected: Collection', 'Expected.Iterator.Element', 'Expected'),
249+
% ('Expected: Collection', 'Expected.Element', 'Expected'),
250250
% ('Element' , 'Element' , 'Array<Element>')]:
251251

252252
// Top-level check for Collection instances. Alias for checkForwardCollection.
@@ -257,7 +257,7 @@ public func checkCollection<${genericParam}, C : Collection>(
257257
${TRACE},
258258
resiliencyChecks: CollectionMisuseResiliencyChecks = .all,
259259
sameValue: (${Element}, ${Element}) -> Bool
260-
) where C.Iterator.Element == ${Element} {
260+
) where C.Element == ${Element} {
261261

262262
checkForwardCollection(expected, collection, message(),
263263
stackTrace: stackTrace, showFrame: showFrame, file: file, line: line,
@@ -276,7 +276,7 @@ public func check${Traversal}Collection<
276276
${TRACE},
277277
resiliencyChecks: CollectionMisuseResiliencyChecks = .all
278278
) where
279-
C.Iterator.Element == ${Element},
279+
C.Element == ${Element},
280280
${Element} : Equatable {
281281

282282
check${Traversal}Collection(
@@ -296,7 +296,7 @@ public func check${Traversal}Collection<
296296
resiliencyChecks: CollectionMisuseResiliencyChecks = .all,
297297
sameValue: (${Element}, ${Element}) -> Bool
298298
) where
299-
C.Iterator.Element == ${Element} {
299+
C.Element == ${Element} {
300300

301301
checkOneLevelOf${Traversal}Collection(expected, collection, ${trace},
302302
resiliencyChecks: resiliencyChecks, sameValue: sameValue)
@@ -324,7 +324,7 @@ public func checkOneLevelOf${Traversal}Collection<
324324
${TRACE},
325325
resiliencyChecks: CollectionMisuseResiliencyChecks = .all,
326326
sameValue: (${Element}, ${Element}) -> Bool
327-
) where C.Iterator.Element == ${Element} {
327+
) where C.Element == ${Element} {
328328

329329
// A `Collection` is a multi-pass `Sequence`.
330330
for _ in 0..<3 {
@@ -370,7 +370,7 @@ public func checkOneLevelOf${Traversal}Collection<
370370

371371
% else:
372372
% assert(Traversal == 'RandomAccess')
373-
typealias Distance = C.IndexDistance
373+
typealias Distance = Int
374374

375375
let count: Distance = collection.count
376376
let offset0 = min(5, count)
@@ -501,7 +501,7 @@ ${genericParam}, S : ${TraversalCollection}
501501
resiliencyChecks: CollectionMisuseResiliencyChecks = .all,
502502
sameValue: (${Element}, ${Element}) -> Bool
503503
) where
504-
S.Iterator.Element == ${Element} {
504+
S.Element == ${Element} {
505505

506506
let expectedArray = Array(expected)
507507

@@ -564,14 +564,14 @@ public func checkRangeReplaceable<C, N>(
564564
) where
565565
C : RangeReplaceableCollection,
566566
N : Collection,
567-
C.Iterator.Element : Equatable,
568-
C.Iterator.Element == N.Iterator.Element {
567+
C.Element : Equatable,
568+
C.Element == N.Element {
569569

570570
typealias A = C
571571

572572
// First make an independent copy of the array that we can use for
573573
// comparison later.
574-
let source = Array<A.Iterator.Element>(makeCollection())
574+
let source = Array<A.Element>(makeCollection())
575575

576576
for (ix, i) in source.indices.enumerated() {
577577
for (jx_, j) in (i..<source.endIndex).enumerated() {

stdlib/private/StdlibCollectionUnittest/CheckCollectionType.swift.gyb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1198,10 +1198,10 @@ extension TestSuite {
11981198

11991199
// FIXME: swift-3-indexing-model -
12001200
// enhance the following for negative direction?
1201-
// advance(i: Index, by n: IndexDistance) -> Index
1201+
// advance(i: Index, by n: Int) -> Index
12021202
// advance(
1203-
// i: Index, by n: IndexDistance, limitedBy: Index) -> Index
1204-
// distance(from start: Index, to end: Index) -> IndexDistance
1203+
// i: Index, by n: Int, limitedBy: Index) -> Index
1204+
// distance(from start: Index, to end: Index) -> Int
12051205

12061206
//===------------------------------------------------------------------===//
12071207
// last

stdlib/private/StdlibCollectionUnittest/LoggingWrappers.swift.gyb

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -414,9 +414,7 @@ public struct ${Self}<
414414
return base.isEmpty
415415
}
416416

417-
public typealias IndexDistance = Base.IndexDistance
418-
419-
public var count: IndexDistance {
417+
public var count: Int {
420418
Log.count[selfType] += 1
421419
return base.count
422420
}
@@ -433,19 +431,19 @@ public struct ${Self}<
433431
return base.first
434432
}
435433

436-
public func index(_ i: Index, offsetBy n: IndexDistance) -> Index {
434+
public func index(_ i: Index, offsetBy n: Int) -> Index {
437435
Log.advance[selfType] += 1
438436
return base.index(i, offsetBy: n)
439437
}
440438

441439
public func index(
442-
_ i: Index, offsetBy n: IndexDistance, limitedBy limit: Index
440+
_ i: Index, offsetBy n: Int, limitedBy limit: Index
443441
) -> Index? {
444442
Log.advanceLimit[selfType] += 1
445443
return base.index(i, offsetBy: n, limitedBy: limit)
446444
}
447445

448-
public func distance(from start: Index, to end: Index) -> IndexDistance {
446+
public func distance(from start: Index, to end: Index) -> Int {
449447
Log.distance[selfType] += 1
450448
return base.distance(from: start, to: end)
451449
}
@@ -558,7 +556,7 @@ public struct ${Self}<
558556
base.replaceSubrange(bounds, with: newElements)
559557
}
560558

561-
public mutating func reserveCapacity(_ n: IndexDistance) {
559+
public mutating func reserveCapacity(_ n: Int) {
562560
Log.reserveCapacity[selfType] += 1
563561
base.reserveCapacity(n)
564562
}
@@ -601,8 +599,8 @@ public struct ${Self}<
601599
public typealias Log = MutableCollectionLog
602600

603601
public typealias SubSequence = Base.SubSequence
604-
605602
public typealias Iterator = Base.Iterator
603+
public typealias Element = Base.Element
606604

607605
public init(wrapping base: Base) {
608606
self.base = base
@@ -613,6 +611,7 @@ public struct ${Self}<
613611
}
614612

615613
public typealias Index = Base.Index
614+
public typealias Indices = Base.Indices
616615

617616
public var startIndex: Index {
618617
return base.startIndex
@@ -621,8 +620,12 @@ public struct ${Self}<
621620
public var endIndex: Index {
622621
return base.endIndex
623622
}
624-
625-
public subscript(position: Index) -> Base.Iterator.Element {
623+
624+
public var indices: Indices {
625+
return base.indices
626+
}
627+
628+
public subscript(position: Index) -> Element {
626629
get {
627630
return base[position]
628631
}
@@ -650,11 +653,11 @@ public struct ${Self}<
650653
}
651654
% end
652655

653-
public func index(_ i: Index, offsetBy n: Base.IndexDistance) -> Index {
656+
public func index(_ i: Index, offsetBy n: Int) -> Index {
654657
return base.index(i, offsetBy: n)
655658
}
656659

657-
public func distance(from start: Index, to end: Index) -> Base.IndexDistance {
660+
public func distance(from start: Index, to end: Index) -> Int {
658661
return base.distance(from: start, to: end)
659662
}
660663

0 commit comments

Comments
 (0)