Skip to content

Commit 9289861

Browse files
authored
Merge pull request #10982 from apple/stdlib-swift4-modernization
2 parents e8ba44a + 1f68678 commit 9289861

14 files changed

+69
-65
lines changed

stdlib/private/StdlibCollectionUnittest/CheckSequenceType.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1922,7 +1922,7 @@ self.test("\(testNamePrefix)._preprocessingPass/semantics") {
19221922
let s = makeWrappedSequence(test.sequence.map(OpaqueValue.init))
19231923
var wasInvoked = false
19241924
let result = s._preprocessingPass {
1925-
(sequence) -> OpaqueValue<Int> in
1925+
() -> OpaqueValue<Int> in
19261926
wasInvoked = true
19271927

19281928
expectEqualSequence(
@@ -1945,7 +1945,7 @@ self.test("\(testNamePrefix)._preprocessingPass/semantics") {
19451945
var result: OpaqueValue<Int>?
19461946
do {
19471947
result = try s._preprocessingPass {
1948-
_ -> OpaqueValue<Int> in
1948+
() -> OpaqueValue<Int> in
19491949
wasInvoked = true
19501950
throw TestError.error2
19511951
}

stdlib/private/StdlibUnittest/StdlibCoreExtras.swift

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@ import Foundation
2727
// useful in tests, and stdlib does not have such facilities yet.
2828
//
2929

30+
func findSubstring(_ haystack: Substring, _ needle: String) -> String.Index? {
31+
return findSubstring(String(haystack._ephemeralContent), needle)
32+
}
33+
3034
func findSubstring(_ string: String, _ substring: String) -> String.Index? {
3135
if substring.isEmpty {
3236
return string.startIndex
@@ -48,7 +52,7 @@ func findSubstring(_ string: String, _ substring: String) -> String.Index? {
4852
while true {
4953
if needleIndex == needle.endIndex {
5054
// if we hit the end of the search string, we found the needle
51-
return matchStartIndex.samePosition(in: string)
55+
return matchStartIndex
5256
}
5357
if matchIndex == haystack.endIndex {
5458
// if we hit the end of the string before finding the end of the needle,
@@ -149,7 +153,7 @@ extension MutableCollection
149153
var f = subrange.lowerBound
150154
var l = index(before: subrange.upperBound)
151155
while f < l {
152-
swap(&self[f], &self[l])
156+
swapAt(f, l)
153157
formIndex(after: &f)
154158
formIndex(before: &l)
155159
}
@@ -195,7 +199,7 @@ extension MutableCollection
195199
repeat {
196200
formIndex(before: &j)
197201
} while !(elementAtBeforeI < self[j])
198-
swap(&self[beforeI], &self[j])
202+
swapAt(beforeI, j)
199203
_reverseSubrange(i..<endIndex)
200204
return .success
201205
}

stdlib/private/StdlibUnittest/StdlibUnittest.swift.gyb

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -766,7 +766,7 @@ struct _ParentProcess {
766766
parameter: Int?
767767
) -> (anyExpectFailed: Bool, seenExpectCrash: Bool,
768768
status: ProcessTerminationStatus?,
769-
crashStdout: [String], crashStderr: [String]) {
769+
crashStdout: [Substring], crashStderr: [Substring]) {
770770
if _pid == nil {
771771
_spawnChild()
772772
}
@@ -790,15 +790,15 @@ struct _ParentProcess {
790790
var stderrSeenCrashDelimiter = false
791791
var stdoutEnd = false
792792
var stderrEnd = false
793-
var capturedCrashStdout: [String] = []
794-
var capturedCrashStderr: [String] = []
793+
var capturedCrashStdout: [Substring] = []
794+
var capturedCrashStderr: [Substring] = []
795795
var anyExpectFailedInChild = false
796796

797797
func processLine(_ line: String, isStdout: Bool) -> (done: Bool, Void) {
798-
var line = line
798+
var line = line[...]
799799
if let index = findSubstring(line, _stdlibUnittestStreamPrefix) {
800800
let controlMessage =
801-
line[index..<line.endIndex]._split(separator: ";")
801+
line[index..<line.endIndex].split(separator: ";")
802802
switch controlMessage[1] {
803803
case "expectCrash":
804804
if isStdout {
@@ -830,7 +830,7 @@ struct _ParentProcess {
830830
if stderrSeenCrashDelimiter {
831831
capturedCrashStderr.append(line)
832832
if findSubstring(line, _crashedPrefix) != nil {
833-
line = "OK: saw expected \"\(line.lowercased())\""
833+
line = "OK: saw expected \"\(line.lowercased())\""[...]
834834
}
835835
}
836836
}
@@ -937,8 +937,8 @@ struct _ParentProcess {
937937

938938
var expectCrash = false
939939
var childTerminationStatus: ProcessTerminationStatus?
940-
var crashStdout: [String] = []
941-
var crashStderr: [String] = []
940+
var crashStdout: [Substring] = []
941+
var crashStderr: [Substring] = []
942942
if _runTestsInProcess {
943943
if t.stdinText != nil {
944944
print("The test \(fullTestName) requires stdin input and can't be run in-process, marking as failed")
@@ -2380,11 +2380,11 @@ public func expectEqualsUnordered<
23802380
Expected.Iterator.Element == Actual.Iterator.Element {
23812381

23822382
let x: [Expected.Iterator.Element] =
2383-
expected.sorted(by: compose(compare, { $0.isLT() }))
2383+
expected.sorted { compare($0, $1).isLT() }
23842384
let y: [Actual.Iterator.Element] =
2385-
actual.sorted(by: compose(compare, { $0.isLT() }))
2385+
actual.sorted { compare($0, $1).isLT() }
23862386
expectEqualSequence(
2387-
x, y, ${trace}, sameValue: compose(compare, { $0.isEQ() }))
2387+
x, y, ${trace}, sameValue: { compare($0, $1).isEQ() })
23882388
}
23892389

23902390
public func expectEqualsUnordered<
@@ -2534,12 +2534,6 @@ public func expectEqualUnicodeScalars(
25342534
}
25352535
}
25362536

2537-
func compose<A, B, C>(_ f: @escaping (A) -> B, _ g: @escaping (B) -> C) -> (A) -> C {
2538-
return { a in
2539-
return g(f(a))
2540-
}
2541-
}
2542-
25432537
// ${'Local Variables'}:
25442538
// eval: (read-only-mode 1)
25452539
// End:

stdlib/public/core/DoubleWidth.swift.gyb

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -499,11 +499,13 @@ public struct DoubleWidth<Base : FixedWidthInteger> :
499499

500500
lhs._storage.low >>= rhs._storage.low
501501
if Base.bitWidth > rhs._storage.low {
502-
lhs._storage.low |= Low(extendingOrTruncating: lhs._storage.high <<
503-
numericCast(numericCast(Base.bitWidth) - rhs._storage.low))
502+
lhs._storage.low |= Low(
503+
extendingOrTruncating:
504+
lhs._storage.high << (numericCast(Base.bitWidth) - rhs._storage.low))
504505
} else {
505-
lhs._storage.low |= Low(extendingOrTruncating: lhs._storage.high >>
506-
numericCast(rhs._storage.low - numericCast(Base.bitWidth)))
506+
lhs._storage.low |= Low(
507+
extendingOrTruncating: lhs._storage.high >>
508+
(rhs._storage.low - numericCast(Base.bitWidth)))
507509
}
508510
lhs._storage.high >>= High(extendingOrTruncating: rhs._storage.low)
509511
}

stdlib/public/core/ExistentialCollection.swift.gyb

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -308,29 +308,29 @@ internal class _AnyRandomAccessCollectionBox<Element>
308308
@_versioned
309309
@_inlineable
310310
internal func _index(
311-
_ i: _AnyIndexBox, offsetBy n: IntMax
311+
_ i: _AnyIndexBox, offsetBy n: Int64
312312
) -> _AnyIndexBox {
313313
_abstract()
314314
}
315315

316316
@_versioned
317317
@_inlineable
318318
internal func _index(
319-
_ i: _AnyIndexBox, offsetBy n: IntMax, limitedBy limit: _AnyIndexBox
319+
_ i: _AnyIndexBox, offsetBy n: Int64, limitedBy limit: _AnyIndexBox
320320
) -> _AnyIndexBox? {
321321
_abstract()
322322
}
323323

324324
@_versioned
325325
@_inlineable
326-
internal func _formIndex(_ i: inout _AnyIndexBox, offsetBy n: IntMax) {
326+
internal func _formIndex(_ i: inout _AnyIndexBox, offsetBy n: Int64) {
327327
_abstract()
328328
}
329329

330330
@_versioned
331331
@_inlineable
332332
internal func _formIndex(
333-
_ i: inout _AnyIndexBox, offsetBy n: IntMax, limitedBy limit: _AnyIndexBox
333+
_ i: inout _AnyIndexBox, offsetBy n: Int64, limitedBy limit: _AnyIndexBox
334334
) -> Bool {
335335
_abstract()
336336
}
@@ -339,7 +339,7 @@ internal class _AnyRandomAccessCollectionBox<Element>
339339
@_inlineable
340340
internal func _distance(
341341
from start: _AnyIndexBox, to end: _AnyIndexBox
342-
) -> IntMax {
342+
) -> Int64 {
343343
_abstract()
344344
}
345345

@@ -357,7 +357,7 @@ internal class _AnyRandomAccessCollectionBox<Element>
357357
*/
358358

359359
@_versioned
360-
internal var _count: IntMax { _abstract() }
360+
internal var _count: Int64 { _abstract() }
361361

362362
// TODO: swift-3-indexing-model: forward the following methods.
363363
/*
@@ -609,7 +609,7 @@ internal final class _${Kind}Box<S : ${Kind}> : _Any${Kind}Box<S.Iterator.Elemen
609609
@_versioned
610610
@_inlineable
611611
internal override func _index(
612-
_ i: _AnyIndexBox, offsetBy n: IntMax
612+
_ i: _AnyIndexBox, offsetBy n: Int64
613613
) -> _AnyIndexBox {
614614
return _IndexBox(_base: _base.index(_unbox(i), offsetBy: numericCast(n)))
615615
}
@@ -618,7 +618,7 @@ internal final class _${Kind}Box<S : ${Kind}> : _Any${Kind}Box<S.Iterator.Elemen
618618
@_inlineable
619619
internal override func _index(
620620
_ i: _AnyIndexBox,
621-
offsetBy n: IntMax,
621+
offsetBy n: Int64,
622622
limitedBy limit: _AnyIndexBox
623623
) -> _AnyIndexBox? {
624624
return _base.index(
@@ -631,7 +631,7 @@ internal final class _${Kind}Box<S : ${Kind}> : _Any${Kind}Box<S.Iterator.Elemen
631631
@_versioned
632632
@_inlineable
633633
internal override func _formIndex(
634-
_ i: inout _AnyIndexBox, offsetBy n: IntMax
634+
_ i: inout _AnyIndexBox, offsetBy n: Int64
635635
) {
636636
if let box = i as? _IndexBox<S.Index> {
637637
return _base.formIndex(&box._base, offsetBy: numericCast(n))
@@ -642,7 +642,7 @@ internal final class _${Kind}Box<S : ${Kind}> : _Any${Kind}Box<S.Iterator.Elemen
642642
@_versioned
643643
@_inlineable
644644
internal override func _formIndex(
645-
_ i: inout _AnyIndexBox, offsetBy n: IntMax, limitedBy limit: _AnyIndexBox
645+
_ i: inout _AnyIndexBox, offsetBy n: Int64, limitedBy limit: _AnyIndexBox
646646
) -> Bool {
647647
if let box = i as? _IndexBox<S.Index> {
648648
return _base.formIndex(
@@ -658,13 +658,13 @@ internal final class _${Kind}Box<S : ${Kind}> : _Any${Kind}Box<S.Iterator.Elemen
658658
internal override func _distance(
659659
from start: _AnyIndexBox,
660660
to end: _AnyIndexBox
661-
) -> IntMax {
661+
) -> Int64 {
662662
return numericCast(_base.distance(from: _unbox(start), to: _unbox(end)))
663663
}
664664

665665
@_versioned
666666
@_inlineable
667-
internal override var _count: IntMax {
667+
internal override var _count: Int64 {
668668
return numericCast(_base.count)
669669
}
670670

@@ -1079,7 +1079,7 @@ public struct ${Self}<Element>
10791079
% end
10801080

10811081
public typealias Index = AnyIndex
1082-
public typealias IndexDistance = IntMax
1082+
public typealias IndexDistance = Int64
10831083

10841084
/// The position of the first element in a non-empty collection.
10851085
///
@@ -1144,22 +1144,22 @@ public struct ${Self}<Element>
11441144
}
11451145

11461146
@_inlineable
1147-
public func index(_ i: AnyIndex, offsetBy n: IntMax) -> AnyIndex {
1147+
public func index(_ i: AnyIndex, offsetBy n: Int64) -> AnyIndex {
11481148
return AnyIndex(_box: _box._index(i._box, offsetBy: n))
11491149
}
11501150

11511151
@_inlineable
11521152
public func index(
11531153
_ i: AnyIndex,
1154-
offsetBy n: IntMax,
1154+
offsetBy n: Int64,
11551155
limitedBy limit: AnyIndex
11561156
) -> AnyIndex? {
11571157
return _box._index(i._box, offsetBy: n, limitedBy: limit._box)
11581158
.map { AnyIndex(_box:$0) }
11591159
}
11601160

11611161
@_inlineable
1162-
public func formIndex(_ i: inout AnyIndex, offsetBy n: IntMax) {
1162+
public func formIndex(_ i: inout AnyIndex, offsetBy n: Int64) {
11631163
if _isUnique(&i._box) {
11641164
return _box._formIndex(&i._box, offsetBy: n)
11651165
} else {
@@ -1170,7 +1170,7 @@ public struct ${Self}<Element>
11701170
@_inlineable
11711171
public func formIndex(
11721172
_ i: inout AnyIndex,
1173-
offsetBy n: IntMax,
1173+
offsetBy n: Int64,
11741174
limitedBy limit: AnyIndex
11751175
) -> Bool {
11761176
if _isUnique(&i._box) {
@@ -1185,7 +1185,7 @@ public struct ${Self}<Element>
11851185
}
11861186

11871187
@_inlineable
1188-
public func distance(from start: AnyIndex, to end: AnyIndex) -> IntMax {
1188+
public func distance(from start: AnyIndex, to end: AnyIndex) -> Int64 {
11891189
return _box._distance(from: start._box, to: end._box)
11901190
}
11911191

@@ -1199,7 +1199,7 @@ public struct ${Self}<Element>
11991199
% end
12001200
/// - Complexity: ${'O(1)' if Traversal == 'RandomAccess' else 'O(*n*)'}
12011201
@_inlineable
1202-
public var count: IntMax {
1202+
public var count: Int64 {
12031203
return _box._count
12041204
}
12051205

stdlib/public/core/HashedCollections.swift.gyb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1739,9 +1739,9 @@ public struct Dictionary<Key : Hashable, Value> :
17391739
self = Dictionary(minimumCapacity: keysAndValues.underestimatedCount)
17401740
// '_MergeError.keyCollision' is caught and handled with an appropriate
17411741
// error message one level down, inside _variantBuffer.merge(_:...).
1742-
try! _variantBuffer.merge(keysAndValues, uniquingKeysWith: { _ in
1743-
throw _MergeError.keyCollision
1744-
})
1742+
try! _variantBuffer.merge(
1743+
keysAndValues,
1744+
uniquingKeysWith: { _, _ in throw _MergeError.keyCollision})
17451745
}
17461746
}
17471747

@@ -5885,7 +5885,7 @@ extension ${Self} {
58855885
@_inlineable
58865886
@available(swift, obsoleted: 4.0)
58875887
public func filter(
5888-
_ isIncluded: (Element) throws -> Bool
5888+
_ isIncluded: (Element) throws -> Bool, obsoletedInSwift4: () = ()
58895889
) rethrows -> [Element] {
58905890
var result: [Element] = []
58915891
for x in self {

stdlib/public/core/Integers.swift.gyb

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2931,7 +2931,7 @@ ${assignmentOperatorComment(x.operator, True)}
29312931
% end
29322932

29332933
@available(swift, obsoleted: 4.0, message: "Use initializers instead")
2934-
public func to${U}IntMax() -> ${U}IntMax {
2934+
public func to${U}IntMax() -> ${U}Int64 {
29352935
return numericCast(self)
29362936
}
29372937

@@ -3167,15 +3167,15 @@ extension SignedNumeric where Self : Comparable {
31673167
@available(swift, obsoleted: 4)
31683168
extension BinaryInteger {
31693169
@available(swift, obsoleted: 4)
3170-
public func toIntMax() -> IntMax {
3171-
return IntMax(self)
3170+
public func toIntMax() -> Int64 {
3171+
return Int64(self)
31723172
}
31733173
}
31743174

31753175
extension UnsignedInteger {
31763176
@available(swift, obsoleted: 4)
3177-
public func toUIntMax() -> UIntMax {
3178-
return UIntMax(self)
3177+
public func toUIntMax() -> UInt64 {
3178+
return UInt64(self)
31793179
}
31803180
}
31813181

stdlib/public/core/ManagedBuffer.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ open class ManagedBuffer<Header, Element> {
9595
public final func withUnsafeMutablePointerToElements<R>(
9696
_ body: (UnsafeMutablePointer<Element>) throws -> R
9797
) rethrows -> R {
98-
return try withUnsafeMutablePointers { return try body($0.1) }
98+
return try withUnsafeMutablePointers { return try body($1) }
9999
}
100100

101101
/// Call `body` with `UnsafeMutablePointer`s to the stored `Header`
@@ -269,7 +269,7 @@ public struct ManagedBufferPointer<Header, Element> : Equatable {
269269
public func withUnsafeMutablePointerToElements<R>(
270270
_ body: (UnsafeMutablePointer<Element>) throws -> R
271271
) rethrows -> R {
272-
return try withUnsafeMutablePointers { return try body($0.1) }
272+
return try withUnsafeMutablePointers { return try body($1) }
273273
}
274274

275275
/// Call `body` with `UnsafeMutablePointer`s to the stored `Header`

stdlib/public/core/Mirror.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -430,7 +430,7 @@ extension Mirror {
430430
if case let label as String = e {
431431
position = children.index { $0.label == label } ?? children.endIndex
432432
}
433-
else if let offset = (e as? Int).map({ IntMax($0) }) ?? (e as? IntMax) {
433+
else if let offset = (e as? Int).map({ Int64($0) }) ?? (e as? Int64) {
434434
position = children.index(children.startIndex,
435435
offsetBy: offset,
436436
limitedBy: children.endIndex) ?? children.endIndex

0 commit comments

Comments
 (0)