Skip to content

Commit 3458794

Browse files
[stdlib] Take several underscored stdlib functions private (#18134)
* Make _sanityCheck internal * Make _debugPrecondition internal * Make Optional._unsafelyUnwrappedUnchecked internal. * Make _precondition internal * Switch Foundation _sanityChecks to assertions * Update file check tests * Remove one more _debugPrecondition * Update Optimization-with-check tests
1 parent 23acb96 commit 3458794

40 files changed

+330
-305
lines changed

benchmark/single-source/OpaqueConsumingUsers.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,8 +80,8 @@ func callFrame4(_ data: Klass, _ user: ConsumingUser) {
8080

8181
@inline(never)
8282
public func run_OpaqueConsumingUsers(_ N: Int) {
83-
let d = data._unsafelyUnwrappedUnchecked
84-
let u = user._unsafelyUnwrappedUnchecked
83+
let d = data.unsafelyUnwrapped
84+
let u = user.unsafelyUnwrapped
8585
for _ in 0..<N*200000 {
8686
callFrame4(d, u)
8787
}

benchmark/single-source/Radix2CooleyTukey.swift

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -108,12 +108,12 @@ func testDouble(iter: Int) {
108108
let size = doubleSize
109109
let level = Int(log2(Double(doubleN)))
110110

111-
let input_real = double_input_real._unsafelyUnwrappedUnchecked
112-
let input_imag = double_input_imag._unsafelyUnwrappedUnchecked
113-
let output_real = double_output_real._unsafelyUnwrappedUnchecked
114-
let output_imag = double_output_imag._unsafelyUnwrappedUnchecked
115-
let temp_real = double_temp_real._unsafelyUnwrappedUnchecked
116-
let temp_imag = double_temp_imag._unsafelyUnwrappedUnchecked
111+
let input_real = double_input_real.unsafelyUnwrapped
112+
let input_imag = double_input_imag.unsafelyUnwrapped
113+
let output_real = double_output_real.unsafelyUnwrapped
114+
let output_imag = double_output_imag.unsafelyUnwrapped
115+
let temp_real = double_temp_real.unsafelyUnwrapped
116+
let temp_imag = double_temp_imag.unsafelyUnwrapped
117117

118118
for _ in 0..<iter {
119119
memset(UnsafeMutableRawPointer(input_real), 0, size)
@@ -224,12 +224,12 @@ func testFloat(iter: Int) {
224224
let n = floatN
225225
let size = floatSize
226226

227-
let input_real = float_input_real._unsafelyUnwrappedUnchecked
228-
let input_imag = float_input_imag._unsafelyUnwrappedUnchecked
229-
let output_real = float_output_real._unsafelyUnwrappedUnchecked
230-
let output_imag = float_output_imag._unsafelyUnwrappedUnchecked
231-
let temp_real = float_temp_real._unsafelyUnwrappedUnchecked
232-
let temp_imag = float_temp_imag._unsafelyUnwrappedUnchecked
227+
let input_real = float_input_real.unsafelyUnwrapped
228+
let input_imag = float_input_imag.unsafelyUnwrapped
229+
let output_real = float_output_real.unsafelyUnwrapped
230+
let output_imag = float_output_imag.unsafelyUnwrapped
231+
let temp_real = float_temp_real.unsafelyUnwrapped
232+
let temp_imag = float_temp_imag.unsafelyUnwrapped
233233

234234
let level = Int(log2(Float(n)))
235235

benchmark/single-source/StringComparison.swift

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ public func setup_StringComparison_ascii() {
130130

131131
@inline(never)
132132
public func run_StringComparison_ascii(_ N: Int) {
133-
let workload = Workload_ascii._unsafelyUnwrappedUnchecked
133+
let workload = Workload_ascii.unsafelyUnwrapped
134134
let tripCount = workload.tripCount
135135
let payload = workload.payload
136136
for _ in 1...tripCount*N {
@@ -167,7 +167,7 @@ public func setup_StringComparison_latin1() {
167167

168168
@inline(never)
169169
public func run_StringComparison_latin1(_ N: Int) {
170-
let workload = Workload_latin1._unsafelyUnwrappedUnchecked
170+
let workload = Workload_latin1.unsafelyUnwrapped
171171
let tripCount = workload.tripCount
172172
let payload = workload.payload
173173
for _ in 1...tripCount*N {
@@ -204,7 +204,7 @@ public func setup_StringComparison_fastPrenormal() {
204204

205205
@inline(never)
206206
public func run_StringComparison_fastPrenormal(_ N: Int) {
207-
let workload = Workload_fastPrenormal._unsafelyUnwrappedUnchecked
207+
let workload = Workload_fastPrenormal.unsafelyUnwrapped
208208
let tripCount = workload.tripCount
209209
let payload = workload.payload
210210
for _ in 1...tripCount*N {
@@ -241,7 +241,7 @@ public func setup_StringComparison_slowerPrenormal() {
241241

242242
@inline(never)
243243
public func run_StringComparison_slowerPrenormal(_ N: Int) {
244-
let workload = Workload_slowerPrenormal._unsafelyUnwrappedUnchecked
244+
let workload = Workload_slowerPrenormal.unsafelyUnwrapped
245245
let tripCount = workload.tripCount
246246
let payload = workload.payload
247247
for _ in 1...tripCount*N {
@@ -278,7 +278,7 @@ public func setup_StringComparison_nonBMPSlowestPrenormal() {
278278

279279
@inline(never)
280280
public func run_StringComparison_nonBMPSlowestPrenormal(_ N: Int) {
281-
let workload = Workload_nonBMPSlowestPrenormal._unsafelyUnwrappedUnchecked
281+
let workload = Workload_nonBMPSlowestPrenormal.unsafelyUnwrapped
282282
let tripCount = workload.tripCount
283283
let payload = workload.payload
284284
for _ in 1...tripCount*N {
@@ -315,7 +315,7 @@ public func setup_StringComparison_emoji() {
315315

316316
@inline(never)
317317
public func run_StringComparison_emoji(_ N: Int) {
318-
let workload = Workload_emoji._unsafelyUnwrappedUnchecked
318+
let workload = Workload_emoji.unsafelyUnwrapped
319319
let tripCount = workload.tripCount
320320
let payload = workload.payload
321321
for _ in 1...tripCount*N {
@@ -352,7 +352,7 @@ public func setup_StringComparison_abnormal() {
352352

353353
@inline(never)
354354
public func run_StringComparison_abnormal(_ N: Int) {
355-
let workload = Workload_abnormal._unsafelyUnwrappedUnchecked
355+
let workload = Workload_abnormal.unsafelyUnwrapped
356356
let tripCount = workload.tripCount
357357
let payload = workload.payload
358358
for _ in 1...tripCount*N {
@@ -389,7 +389,7 @@ public func setup_StringComparison_zalgo() {
389389

390390
@inline(never)
391391
public func run_StringComparison_zalgo(_ N: Int) {
392-
let workload = Workload_zalgo._unsafelyUnwrappedUnchecked
392+
let workload = Workload_zalgo.unsafelyUnwrapped
393393
let tripCount = workload.tripCount
394394
let payload = workload.payload
395395
for _ in 1...tripCount*N {
@@ -426,7 +426,7 @@ public func setup_StringComparison_longSharedPrefix() {
426426

427427
@inline(never)
428428
public func run_StringComparison_longSharedPrefix(_ N: Int) {
429-
let workload = Workload_longSharedPrefix._unsafelyUnwrappedUnchecked
429+
let workload = Workload_longSharedPrefix.unsafelyUnwrapped
430430
let tripCount = workload.tripCount
431431
let payload = workload.payload
432432
for _ in 1...tripCount*N {

benchmark/single-source/StringComparison.swift.gyb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ public func setup_StringComparison_${Name}() {
6565

6666
@inline(never)
6767
public func run_StringComparison_${Name}(_ N: Int) {
68-
let workload = Workload_${Name}._unsafelyUnwrappedUnchecked
68+
let workload = Workload_${Name}.unsafelyUnwrapped
6969
let tripCount = workload.tripCount
7070
let payload = workload.payload
7171
for _ in 1...tripCount*N {

stdlib/private/StdlibCollectionUnittest/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ add_swift_library(swiftStdlibCollectionUnittest ${SWIFT_STDLIB_LIBRARY_BUILD_TYP
1717
LoggingWrappers.swift
1818
MinimalCollections.swift
1919
RangeSelection.swift
20-
../../public/core/WriteBackMutableSlice.swift
20+
WriteBackMutableSlice.swift
2121

2222
SWIFT_MODULE_DEPENDS StdlibUnittest
2323
SWIFT_MODULE_DEPENDS_LINUX Glibc

stdlib/private/StdlibCollectionUnittest/MinimalCollections.swift

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -705,7 +705,7 @@ public struct MinimalCollection<T> : Collection {
705705

706706
public func distance(from start: MinimalIndex, to end: MinimalIndex)
707707
-> Int {
708-
_precondition(start <= end,
708+
precondition(start <= end,
709709
"Only BidirectionalCollections can have end come before start")
710710
// FIXME: swift-3-indexing-model: perform a range check properly.
711711
if start != endIndex {
@@ -718,7 +718,7 @@ public struct MinimalCollection<T> : Collection {
718718
}
719719

720720
public func index(_ i: Index, offsetBy n: Int) -> Index {
721-
_precondition(n >= 0,
721+
precondition(n >= 0,
722722
"Only BidirectionalCollections can be advanced by a negative amount")
723723
// FIXME: swift-3-indexing-model: perform a range check properly.
724724
if i != endIndex {
@@ -880,7 +880,7 @@ public struct MinimalRangeReplaceableCollection<T> : Collection, RangeReplaceabl
880880

881881
public func distance(from start: MinimalIndex, to end: MinimalIndex)
882882
-> Int {
883-
_precondition(start <= end,
883+
precondition(start <= end,
884884
"Only BidirectionalCollections can have end come before start")
885885
// FIXME: swift-3-indexing-model: perform a range check properly.
886886
if start != endIndex {
@@ -893,7 +893,7 @@ public struct MinimalRangeReplaceableCollection<T> : Collection, RangeReplaceabl
893893
}
894894

895895
public func index(_ i: Index, offsetBy n: Int) -> Index {
896-
_precondition(n >= 0,
896+
precondition(n >= 0,
897897
"Only BidirectionalCollections can be advanced by a negative amount")
898898
// FIXME: swift-3-indexing-model: perform a range check properly.
899899
if i != endIndex {
@@ -1130,7 +1130,7 @@ public struct MinimalMutableCollection<T> : Collection, MutableCollection {
11301130

11311131
public func distance(from start: MinimalIndex, to end: MinimalIndex)
11321132
-> Int {
1133-
_precondition(start <= end,
1133+
precondition(start <= end,
11341134
"Only BidirectionalCollections can have end come before start")
11351135
// FIXME: swift-3-indexing-model: perform a range check properly.
11361136
if start != endIndex {
@@ -1143,7 +1143,7 @@ public struct MinimalMutableCollection<T> : Collection, MutableCollection {
11431143
}
11441144

11451145
public func index(_ i: Index, offsetBy n: Int) -> Index {
1146-
_precondition(n >= 0,
1146+
precondition(n >= 0,
11471147
"Only BidirectionalCollections can be advanced by a negative amount")
11481148
// FIXME: swift-3-indexing-model: perform a range check properly.
11491149
if i != endIndex {
@@ -1313,7 +1313,7 @@ public struct MinimalMutableRangeReplaceableCollection<T> : Collection, MutableC
13131313

13141314
public func distance(from start: MinimalIndex, to end: MinimalIndex)
13151315
-> Int {
1316-
_precondition(start <= end,
1316+
precondition(start <= end,
13171317
"Only BidirectionalCollections can have end come before start")
13181318
// FIXME: swift-3-indexing-model: perform a range check properly.
13191319
if start != endIndex {
@@ -1326,7 +1326,7 @@ public struct MinimalMutableRangeReplaceableCollection<T> : Collection, MutableC
13261326
}
13271327

13281328
public func index(_ i: Index, offsetBy n: Int) -> Index {
1329-
_precondition(n >= 0,
1329+
precondition(n >= 0,
13301330
"Only BidirectionalCollections can be advanced by a negative amount")
13311331
// FIXME: swift-3-indexing-model: perform a range check properly.
13321332
if i != endIndex {
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
//===--- WriteBackMutableSlice.swift --------------------------------------===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See https://swift.org/LICENSE.txt for license information
9+
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
@inlinable
14+
internal func _writeBackMutableSlice<C, Slice_>(
15+
_ self_: inout C, bounds: Range<C.Index>, slice: Slice_
16+
) where
17+
C : MutableCollection,
18+
Slice_ : Collection,
19+
C.Element == Slice_.Element,
20+
C.Index == Slice_.Index {
21+
22+
self_._failEarlyRangeCheck(bounds, bounds: self_.startIndex..<self_.endIndex)
23+
24+
// FIXME(performance): can we use
25+
// _withUnsafeMutableBufferPointerIfSupported? Would that create inout
26+
// aliasing violations if the newValue points to the same buffer?
27+
28+
var selfElementIndex = bounds.lowerBound
29+
let selfElementsEndIndex = bounds.upperBound
30+
var newElementIndex = slice.startIndex
31+
let newElementsEndIndex = slice.endIndex
32+
33+
while selfElementIndex != selfElementsEndIndex &&
34+
newElementIndex != newElementsEndIndex {
35+
36+
self_[selfElementIndex] = slice[newElementIndex]
37+
self_.formIndex(after: &selfElementIndex)
38+
slice.formIndex(after: &newElementIndex)
39+
}
40+
41+
precondition(
42+
selfElementIndex == selfElementsEndIndex,
43+
"Cannot replace a slice of a MutableCollection with a slice of a smaller size")
44+
precondition(
45+
newElementIndex == newElementsEndIndex,
46+
"Cannot replace a slice of a MutableCollection with a slice of a larger size")
47+
}
48+

stdlib/private/StdlibUnittest/StdlibUnittest.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1507,7 +1507,7 @@ func stopTrackingObjects(_: UnsafePointer<CChar>) -> Int
15071507
public final class TestSuite {
15081508
public init(_ name: String) {
15091509
self.name = name
1510-
_precondition(
1510+
precondition(
15111511
_testNameToIndex[name] == nil,
15121512
"test suite with the same name already exists")
15131513
_allTestSuites.append(self)
@@ -1543,12 +1543,12 @@ public final class TestSuite {
15431543
}
15441544

15451545
public func setUp(_ code: @escaping () -> Void) {
1546-
_precondition(_testSetUpCode == nil, "set-up code already set")
1546+
precondition(_testSetUpCode == nil, "set-up code already set")
15471547
_testSetUpCode = code
15481548
}
15491549

15501550
public func tearDown(_ code: @escaping () -> Void) {
1551-
_precondition(_testTearDownCode == nil, "tear-down code already set")
1551+
precondition(_testTearDownCode == nil, "tear-down code already set")
15521552
_testTearDownCode = code
15531553
}
15541554

stdlib/private/StdlibUnittestFoundationExtras/StdlibUnittestFoundationExtras.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,13 @@ public func withOverriddenLocaleCurrentLocale<Result>(
3030
guard let oldMethod = class_getClassMethod(
3131
NSLocale.self, #selector(getter: NSLocale.current)) as Optional
3232
else {
33-
_preconditionFailure("Could not find +[Locale currentLocale]")
33+
preconditionFailure("Could not find +[Locale currentLocale]")
3434
}
3535

3636
guard let newMethod = class_getClassMethod(
3737
NSLocale.self, #selector(NSLocale._swiftUnittest_currentLocale)) as Optional
3838
else {
39-
_preconditionFailure("Could not find +[Locale _swiftUnittest_currentLocale]")
39+
preconditionFailure("Could not find +[Locale _swiftUnittest_currentLocale]")
4040
}
4141

4242
precondition(_temporaryLocaleCurrentLocale == nil,

stdlib/public/SDK/Accelerate/BNNS.swift.gyb

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ extension BNNSImageStackDescriptor {
9999
image_stride: Int,
100100
data_type: BNNSDataType) {
101101

102-
_precondition(data_type != .indexed8,
102+
precondition(data_type != .indexed8,
103103
"Image stacks cannot use the indexed8 data type.")
104104

105105
self.init(width: width,
@@ -119,7 +119,7 @@ extension BNNSVectorDescriptor {
119119
public init(size: Int,
120120
data_type: BNNSDataType) {
121121

122-
_precondition(data_type != .indexed8,
122+
precondition(data_type != .indexed8,
123123
"Vectors cannot use the indexed8 data type.")
124124

125125
self.init(size: size,
@@ -137,7 +137,7 @@ extension BNNSLayerData {
137137
data_scale: Float = 1,
138138
data_bias: Float = 0) {
139139

140-
_precondition(data_type != .indexed8,
140+
precondition(data_type != .indexed8,
141141
"This initializer cannot be used with the indexed8 data type; use BNNSLayerData.indexed8 instead.")
142142

143143
self.init(data: data,
@@ -173,9 +173,9 @@ extension BNNSActivation {
173173
alpha: Float = .nan,
174174
beta: Float = .nan) {
175175
if #available(macOS 10.13, iOS 11.0, tvOS 11.0, watchOS 4.0, *) {
176-
_precondition(function != .integerLinearSaturate,
176+
precondition(function != .integerLinearSaturate,
177177
"This initializer cannot be used with the integerLinearSaturate activation function; use BNNSActivation.integerLinearSaturate(scale:Int32, offset:Int32, shift:Int32) instead.")
178-
_precondition(function != .integerLinearSaturatePerChannel,
178+
precondition(function != .integerLinearSaturatePerChannel,
179179
"This initializer cannot be used with the integerLinearSaturatePerChannel activation function; use BNNSActivation.integerLinearSaturatePerChannel(scale:UnsafePointer<Int32>, offset:UnsafePointer<Int32>, shift:UnsafePointer<Int32>) instead.")
180180
}
181181
self.init(function: function,

stdlib/public/SDK/CoreAudio/CoreAudio.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ extension AudioBufferList {
5252
/// - Returns: the size in bytes of an `AudioBufferList` that can hold up to
5353
/// `maximumBuffers` `AudioBuffer`s.
5454
public static func sizeInBytes(maximumBuffers: Int) -> Int {
55-
_precondition(maximumBuffers >= 1,
55+
precondition(maximumBuffers >= 1,
5656
"AudioBufferList should contain at least one AudioBuffer")
5757
return MemoryLayout<AudioBufferList>.size +
5858
(maximumBuffers - 1) * MemoryLayout<AudioBuffer>.stride
@@ -69,7 +69,7 @@ extension AudioBufferList {
6969
-> UnsafeMutableAudioBufferListPointer {
7070
let byteSize = sizeInBytes(maximumBuffers: maximumBuffers)
7171
let ablMemory = calloc(byteSize, 1)
72-
_precondition(ablMemory != nil,
72+
precondition(ablMemory != nil,
7373
"failed to allocate memory for an AudioBufferList")
7474

7575
let listPtr = ablMemory!.bindMemory(to: AudioBufferList.self, capacity: 1)
@@ -153,12 +153,12 @@ extension UnsafeMutableAudioBufferListPointer
153153
/// Access an indexed `AudioBuffer` (`mBuffers[i]`).
154154
public subscript(index: Index) -> Element {
155155
get {
156-
_precondition(index >= 0 && index < self.count,
156+
precondition(index >= 0 && index < self.count,
157157
"subscript index out of range")
158158
return (_audioBuffersPointer + index).pointee
159159
}
160160
nonmutating set(newValue) {
161-
_precondition(index >= 0 && index < self.count,
161+
precondition(index >= 0 && index < self.count,
162162
"subscript index out of range")
163163
(_audioBuffersPointer + index).pointee = newValue
164164
}

stdlib/public/SDK/Foundation/ExtraStringAPIs.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ extension String.UTF16View.Index {
1515
@available(swift, deprecated: 3.2)
1616
@available(swift, obsoleted: 4.0)
1717
public init(_ offset: Int) {
18-
_precondition(offset >= 0, "Negative UTF16 index offset not allowed")
18+
assert(offset >= 0, "Negative UTF16 index offset not allowed")
1919
self.init(encodedOffset: offset)
2020
}
2121

stdlib/public/SDK/Foundation/NSArray.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ extension Array : _ObjectiveCBridgeable {
3131
/// The provided `NSArray` will be copied to ensure that the copy can
3232
/// not be mutated by other code.
3333
internal init(_cocoaArray: NSArray) {
34-
_sanityCheck(_isBridgedVerbatimToObjectiveC(Element.self),
34+
assert(_isBridgedVerbatimToObjectiveC(Element.self),
3535
"Array can be backed by NSArray only when the element type can be bridged verbatim to Objective-C")
3636
// FIXME: We would like to call CFArrayCreateCopy() to avoid doing an
3737
// objc_msgSend() for instances of CoreFoundation types. We can't do that

0 commit comments

Comments
 (0)