Skip to content

Commit 9ee856f

Browse files
[stdlib][WIP] Eliminate (Closed)CountableRange using conditional conformance (#13342)
* Make Range conditionally a Collection * Convert ClosedRange to conditionally a collection * De-gyb Range/ClosedRange, refactoring some methods. * Remove use of Countable{Closed}Range from stdlib * Remove Countable use from Foundation * Fix test errors and warnings resulting from Range/CountableRange collapse * fix prespecialize test for new mangling * Update CoreAudio use of CountableRange * Update SwiftSyntax use of CountableRange * Restore ClosedRange.Index: Hashable conformance * Move fixed typechecker slowness test for array-of-ranges from slow to fast, yay * Apply Doug's patch to loosen test to just check for error
1 parent ab8e3a7 commit 9ee856f

Some content is hidden

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

58 files changed

+679
-1363
lines changed

stdlib/private/StdlibCollectionUnittest/CheckCollectionType.swift.gyb

+1-1
Original file line numberDiff line numberDiff line change
@@ -1556,7 +1556,7 @@ extension TestSuite {
15561556
return
15571557
}
15581558

1559-
func toCollection(_ r: CountableRange<Int>) -> C {
1559+
func toCollection(_ r: Range<Int>) -> C {
15601560
return makeCollection(r.map { wrapValue(OpaqueValue($0)) })
15611561
}
15621562

stdlib/private/StdlibCollectionUnittest/MinimalCollections.swift.gyb

+8-8
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ internal enum _CollectionOperation : Equatable {
160160
case .reserveCapacity:
161161
let invalidIndices = newElementsIds.indices
162162
newElementsIds.replaceSubrange(
163-
Range(invalidIndices),
163+
invalidIndices,
164164
with: repeatElement(nextStateId, count: invalidIndices.count))
165165
newEndIndexId = nextStateId
166166

@@ -180,7 +180,7 @@ internal enum _CollectionOperation : Equatable {
180180

181181
let invalidIndices = subRange.lowerBound..<newElementsIds.endIndex
182182
newElementsIds.replaceSubrange(
183-
Range(invalidIndices),
183+
invalidIndices,
184184
with: repeatElement(nextStateId, count: invalidIndices.count))
185185
newEndIndexId = nextStateId
186186

@@ -189,7 +189,7 @@ internal enum _CollectionOperation : Equatable {
189189

190190
let invalidIndices = atIndex..<newElementsIds.endIndex
191191
newElementsIds.replaceSubrange(
192-
Range(invalidIndices),
192+
invalidIndices,
193193
with: repeatElement(nextStateId, count: invalidIndices.count))
194194
newEndIndexId = nextStateId
195195

@@ -200,7 +200,7 @@ internal enum _CollectionOperation : Equatable {
200200

201201
let invalidIndices = atIndex..<newElementsIds.endIndex
202202
newElementsIds.replaceSubrange(
203-
Range(invalidIndices),
203+
invalidIndices,
204204
with: repeatElement(nextStateId, count: invalidIndices.count))
205205
newEndIndexId = nextStateId
206206

@@ -209,7 +209,7 @@ internal enum _CollectionOperation : Equatable {
209209

210210
let invalidIndices = index..<newElementsIds.endIndex
211211
newElementsIds.replaceSubrange(
212-
Range(invalidIndices),
212+
invalidIndices,
213213
with: repeatElement(nextStateId, count: invalidIndices.count))
214214
newEndIndexId = nextStateId
215215

@@ -222,7 +222,7 @@ internal enum _CollectionOperation : Equatable {
222222

223223
let invalidIndices = subRange.lowerBound..<newElementsIds.endIndex
224224
newElementsIds.replaceSubrange(
225-
Range(invalidIndices),
225+
invalidIndices,
226226
with: repeatElement(nextStateId, count: invalidIndices.count))
227227
newEndIndexId = nextStateId
228228

@@ -592,7 +592,7 @@ public struct ${Self}<T> : ${SelfProtocols} {
592592
}
593593

594594
% if StrideableIndex:
595-
public typealias Indices = CountableRange<${Index}>
595+
public typealias Indices = Range<${Index}>
596596
% elif Traversal == 'RandomAccess':
597597
// FIXME: this shouldn't be necessary, should come by default
598598
public typealias Indices = DefaultIndices<${Self}<T>>
@@ -866,7 +866,7 @@ public struct ${Self}<Element> : ${SelfProtocols} {
866866
public typealias Index = ${Index}
867867

868868
% if StrideableIndex:
869-
public typealias Indices = CountableRange<${Index}>
869+
public typealias Indices = Range<${Index}>
870870
% elif Traversal == 'RandomAccess':
871871
// FIXME: this shouldn't be necessary, should come by default
872872
public typealias Indices = DefaultIndices<${Self}<Element>>

stdlib/private/StdlibCollectionUnittest/RangeSelection.swift

-8
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,6 @@ public enum RangeSelection {
5555
}
5656
}
5757

58-
public func countableRange<C : Collection>(in c: C) -> CountableRange<C.Index> {
59-
return CountableRange(range(in: c))
60-
}
61-
6258
public func closedRange<C : Collection>(in c: C) -> ClosedRange<C.Index> {
6359
switch self {
6460
case .emptyRange: fatalError("Closed range cannot be empty")
@@ -87,8 +83,4 @@ public enum RangeSelection {
8783
return start...end
8884
}
8985
}
90-
91-
public func countableClosedRange<C : Collection>(in c: C) -> CountableClosedRange<C.Index> {
92-
return CountableClosedRange(closedRange(in: c))
93-
}
9486
}

stdlib/private/StdlibUnittest/StdlibUnittest.swift.gyb

+5-25
Original file line numberDiff line numberDiff line change
@@ -319,13 +319,8 @@ public func expectGE<T : Comparable>(_ lhs: T, _ rhs: T, ${TRACE}) {
319319
}
320320
}
321321

322-
% for OtherRange in ['Range', 'CountableRange', 'ClosedRange', 'CountableClosedRange']:
323-
extension Range
324-
% if 'Countable' in OtherRange:
325-
where
326-
Bound : Strideable, Bound.Stride : SignedInteger
327-
% end
328-
{
322+
% for OtherRange in ['Range', 'ClosedRange']:
323+
extension Range {
329324
internal func _contains(_ other: ${OtherRange}<Bound>) -> Bool {
330325
if other.lowerBound < lowerBound { return false }
331326
% if 'Closed' in OtherRange:
@@ -338,7 +333,7 @@ extension Range
338333
}
339334
% end
340335

341-
% for Range in ['Range', 'CountableRange', 'ClosedRange', 'CountableClosedRange']:
336+
% for Range in ['Range', 'ClosedRange']:
342337
public func expectTrapping<Bound>(
343338
_ point: Bound, in range: ${Range}<Bound>, ${TRACE}
344339
) {
@@ -2433,31 +2428,16 @@ public func expectEqualsUnordered<T : Comparable>(
24332428
expectEqualSequence(x, y, ${trace})
24342429
}
24352430

2436-
public func expectEqualsUnordered<
2437-
T : Strideable
2438-
>(
2431+
public func expectEqualsUnordered<T : Strideable>(
24392432
_ expected: Range<T>, _ actual: [T], ${TRACE}
24402433
) where T.Stride: SignedInteger {
2441-
expectEqualsUnordered(
2442-
CountableRange(uncheckedBounds:
2443-
(lower: expected.lowerBound, upper: expected.upperBound)),
2444-
actual,
2445-
${trace},
2446-
showFrame: false)
2447-
}
2448-
2449-
public func expectEqualsUnordered<T : Strideable>(
2450-
_ expected: CountableRange<T>, _ actual: [T], ${TRACE}
2451-
) {
24522434
if expected.count != actual.count {
24532435
expectationFailure("expected elements: \"\(expected)\"\n"
24542436
+ "actual: \"\(actual)\" (of type \(String(reflecting: type(of: actual))))",
24552437
trace: ${trace})
24562438
}
2457-
let r = Range(uncheckedBounds:
2458-
(lower: expected.lowerBound, upper: expected.upperBound))
24592439
for e in actual {
2460-
if !r.contains(e) {
2440+
if !expected.contains(e) {
24612441
expectationFailure("expected elements: \"\(expected)\"\n"
24622442
+ "actual: \"\(actual)\" (of type \(String(reflecting: type(of: actual))))",
24632443
trace: ${trace})

stdlib/public/SDK/CoreAudio/CoreAudio.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,6 @@ extension UnsafeMutableAudioBufferListPointer
172172
}
173173
}
174174

175-
public typealias Indices = CountableRange<Int>
175+
public typealias Indices = Range<Int>
176176
}
177177

stdlib/public/SDK/Dispatch/Data.swift

+7-7
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ public struct DispatchData : RandomAccessCollection, _ObjectiveCBridgeable {
154154
}
155155
}
156156

157-
private func _copyBytesHelper(to pointer: UnsafeMutableRawPointer, from range: CountableRange<Index>) {
157+
private func _copyBytesHelper(to pointer: UnsafeMutableRawPointer, from range: Range<Index>) {
158158
var copiedCount = 0
159159
if range.isEmpty { return }
160160
let rangeSize = range.count
@@ -195,8 +195,8 @@ public struct DispatchData : RandomAccessCollection, _ObjectiveCBridgeable {
195195
/// - parameter pointer: A pointer to the buffer you wish to copy the bytes into.
196196
/// - parameter range: The range in the `Data` to copy.
197197
/// - warning: This method does not verify that the contents at pointer have enough space to hold the required number of bytes.
198-
@available(swift, deprecated: 4, message: "Use copyBytes(to: UnsafeMutableRawBufferPointer, from: CountableRange<Index>) instead")
199-
public func copyBytes(to pointer: UnsafeMutablePointer<UInt8>, from range: CountableRange<Index>) {
198+
@available(swift, deprecated: 4, message: "Use copyBytes(to: UnsafeMutableRawBufferPointer, from: Range<Index>) instead")
199+
public func copyBytes(to pointer: UnsafeMutablePointer<UInt8>, from range: Range<Index>) {
200200
_copyBytesHelper(to: pointer, from: range)
201201
}
202202

@@ -205,7 +205,7 @@ public struct DispatchData : RandomAccessCollection, _ObjectiveCBridgeable {
205205
/// - parameter pointer: A pointer to the buffer you wish to copy the bytes into. The buffer must be large
206206
/// enough to hold `count` bytes.
207207
/// - parameter range: The range in the `Data` to copy.
208-
public func copyBytes(to pointer: UnsafeMutableRawBufferPointer, from range: CountableRange<Index>) {
208+
public func copyBytes(to pointer: UnsafeMutableRawBufferPointer, from range: Range<Index>) {
209209
assert(range.count <= pointer.count, "Buffer too small to copy \(range.count) bytes")
210210
guard pointer.baseAddress != nil else { return }
211211
_copyBytesHelper(to: pointer.baseAddress!, from: range)
@@ -218,11 +218,11 @@ public struct DispatchData : RandomAccessCollection, _ObjectiveCBridgeable {
218218
/// - parameter buffer: A buffer to copy the data into.
219219
/// - parameter range: A range in the data to copy into the buffer. If the range is empty, this function will return 0 without copying anything. If the range is nil, as much data as will fit into `buffer` is copied.
220220
/// - returns: Number of bytes copied into the destination buffer.
221-
public func copyBytes<DestinationType>(to buffer: UnsafeMutableBufferPointer<DestinationType>, from range: CountableRange<Index>? = nil) -> Int {
221+
public func copyBytes<DestinationType>(to buffer: UnsafeMutableBufferPointer<DestinationType>, from range: Range<Index>? = nil) -> Int {
222222
let cnt = count
223223
guard cnt > 0 else { return 0 }
224224

225-
let copyRange : CountableRange<Index>
225+
let copyRange : Range<Index>
226226
if let r = range {
227227
guard !r.isEmpty else { return 0 }
228228
precondition(r.startIndex >= 0)
@@ -262,7 +262,7 @@ public struct DispatchData : RandomAccessCollection, _ObjectiveCBridgeable {
262262
/// Return a new copy of the data in a specified range.
263263
///
264264
/// - parameter range: The range to copy.
265-
public func subdata(in range: CountableRange<Index>) -> DispatchData {
265+
public func subdata(in range: Range<Index>) -> DispatchData {
266266
let subrange = __dispatch_data_create_subrange(
267267
__wrapped, range.startIndex, range.endIndex - range.startIndex)
268268
return DispatchData(data: subrange)

stdlib/public/SDK/Foundation/Data.swift

+2-8
Original file line numberDiff line numberDiff line change
@@ -996,7 +996,7 @@ public struct Data : ReferenceConvertible, Equatable, Hashable, RandomAccessColl
996996
public typealias Base64DecodingOptions = NSData.Base64DecodingOptions
997997

998998
public typealias Index = Int
999-
public typealias Indices = CountableRange<Int>
999+
public typealias Indices = Range<Int>
10001000

10011001
@_versioned internal var _backing : _DataStorage
10021002
@_versioned internal var _sliceRange: Range<Index>
@@ -1537,12 +1537,6 @@ public struct Data : ReferenceConvertible, Equatable, Hashable, RandomAccessColl
15371537
}
15381538
}
15391539

1540-
@inline(__always)
1541-
public mutating func replaceSubrange(_ subrange: CountableRange<Index>, with data: Data) {
1542-
let range: Range<Int> = subrange.lowerBound..<subrange.upperBound
1543-
replaceSubrange(range, with: data)
1544-
}
1545-
15461540
/// Replace a region of bytes in the data with new bytes from a buffer.
15471541
///
15481542
/// This will resize the data if required, to fit the entire contents of `buffer`.
@@ -1743,7 +1737,7 @@ public struct Data : ReferenceConvertible, Equatable, Hashable, RandomAccessColl
17431737
return i + 1
17441738
}
17451739

1746-
public var indices: CountableRange<Int> {
1740+
public var indices: Range<Int> {
17471741
@inline(__always)
17481742
get {
17491743
return startIndex..<endIndex

0 commit comments

Comments
 (0)