Skip to content

Commit 776b33b

Browse files
committed
Make _precondition internal
1 parent f5a2a81 commit 776b33b

27 files changed

+223
-204
lines changed

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+
precondition(offset >= 0, "Negative UTF16 index offset not allowed")
1919
self.init(encodedOffset: offset)
2020
}
2121

stdlib/public/SDK/GLKit/GLKMath.swift.gyb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@ def defineSubscript(Type, limit):
3434
public subscript(i: Int) -> Float {{
3535
@inline(__always)
3636
get {{
37-
_precondition(i >= 0, "Negative {0} index out of range")
38-
_precondition(i < {1}, "{0} index out of range")
37+
precondition(i >= 0, "Negative {0} index out of range")
38+
precondition(i < {1}, "{0} index out of range")
3939
4040
// We can't derive an UnsafePointer from a let binding. Lame.
4141
var clone = self

stdlib/public/SDK/SpriteKit/SpriteKit.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,13 +64,13 @@ extension SKWarpGeometryGrid {
6464
case (0, 0):
6565
self.init(__columns: columns, rows: rows, sourcePositions: nil, destPositions: nil)
6666
case (let dests, 0):
67-
_precondition(dests == requiredElementsCount, "Mismatch found between rows/columns and positions.")
67+
precondition(dests == requiredElementsCount, "Mismatch found between rows/columns and positions.")
6868
self.init(__columns: columns, rows: rows, sourcePositions: nil, destPositions: destinationPositions)
6969
case (0, let sources):
70-
_precondition(sources == requiredElementsCount, "Mismatch found between rows/columns and positions.")
70+
precondition(sources == requiredElementsCount, "Mismatch found between rows/columns and positions.")
7171
self.init(__columns: columns, rows: rows, sourcePositions: sourcePositions, destPositions: nil)
7272
case (let dests, let sources):
73-
_precondition(dests == requiredElementsCount && sources == requiredElementsCount, "Mismatch found between rows/columns and positions.")
73+
precondition(dests == requiredElementsCount && sources == requiredElementsCount, "Mismatch found between rows/columns and positions.")
7474
self.init(__columns: columns, rows: rows, sourcePositions: sourcePositions, destPositions: destinationPositions)
7575
}
7676
}

stdlib/public/SDK/XCTest/XCTest.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -388,7 +388,7 @@ public func XCTAssertEqual<T : FloatingPoint>(_ expression1: @autoclosure () thr
388388

389389
default:
390390
// unknown type, fail with prejudice
391-
_preconditionFailure("Unsupported floating-point type passed to XCTAssertEqual")
391+
preconditionFailure("Unsupported floating-point type passed to XCTAssertEqual")
392392
}
393393

394394
if !equalWithAccuracy {
@@ -464,7 +464,7 @@ public func XCTAssertNotEqual<T : FloatingPoint>(_ expression1: @autoclosure ()
464464

465465
default:
466466
// unknown type, fail with prejudice
467-
_preconditionFailure("Unsupported floating-point type passed to XCTAssertNotEqual")
467+
preconditionFailure("Unsupported floating-point type passed to XCTAssertNotEqual")
468468
}
469469

470470
if !notEqualWithAccuracy {

stdlib/public/SDK/simd/simd.swift.gyb

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ public struct ${vectype} {
100100
///
101101
/// - Precondition: `array` must have exactly ${cardinal[count]} elements.
102102
public init(_ array: [${scalar}]) {
103-
_precondition(array.count == ${count},
103+
precondition(array.count == ${count},
104104
"${vectype} requires a ${cardinal[count]}-element array")
105105
self.init(${', '.join(map(lambda i:
106106
'array[' + str(i) + ']',
@@ -111,16 +111,16 @@ public struct ${vectype} {
111111
public subscript(index: Int) -> ${scalar} {
112112
@_transparent
113113
get {
114-
_precondition(index >= 0, "Vector index out of range")
115-
_precondition(index < ${count}, "Vector index out of range")
114+
precondition(index >= 0, "Vector index out of range")
115+
precondition(index < ${count}, "Vector index out of range")
116116
let elt = Builtin.${extractelement}(_value,
117117
Int32(index)._value)
118118
return ${scalar}(elt)
119119
}
120120
@_transparent
121121
set(value) {
122-
_precondition(index >= 0, "Vector index out of range")
123-
_precondition(index < ${count}, "Vector index out of range")
122+
precondition(index >= 0, "Vector index out of range")
123+
precondition(index < ${count}, "Vector index out of range")
124124
_value = Builtin.${insertelement}(_value,
125125
value._value,
126126
Int32(index)._value)
@@ -652,7 +652,7 @@ extension ${mattype} {
652652
653653
/// Initialize matrix to have specified `columns`.
654654
public init(_ columns: [${coltype}]) {
655-
_precondition(columns.count == ${cols}, "Requires array of ${cols} vectors")
655+
precondition(columns.count == ${cols}, "Requires array of ${cols} vectors")
656656
self.init()
657657
% for i in range(cols):
658658
self.columns.${i} = columns[${i}]
@@ -661,7 +661,7 @@ extension ${mattype} {
661661
662662
/// Initialize matrix to have specified `rows`.
663663
public init(rows: [${rowtype}]) {
664-
_precondition(rows.count == ${rows}, "Requires array of ${rows} vectors")
664+
precondition(rows.count == ${rows}, "Requires array of ${rows} vectors")
665665
self = ${transtype}(rows).transpose
666666
}
667667
@@ -695,15 +695,15 @@ extension ${mattype} {
695695
% for i in range(cols):
696696
case ${i}: return columns.${i}
697697
% end
698-
default: _preconditionFailure("Column index out of range")
698+
default: preconditionFailure("Column index out of range")
699699
}
700700
}
701701
set (value) {
702702
switch(column) {
703703
% for i in range(cols):
704704
case ${i}: columns.${i} = value
705705
% end
706-
default: _preconditionFailure("Column index out of range")
706+
default: preconditionFailure("Column index out of range")
707707
}
708708
}
709709
}

stdlib/public/core/Assert.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -199,8 +199,8 @@ public func fatalError(
199199
/// building in fast mode they are disabled. In release mode they don't print
200200
/// an error message but just trap. In debug mode they print an error message
201201
/// and abort.
202-
@_transparent
203-
public func _precondition(
202+
@usableFromInline @_transparent
203+
internal func _precondition(
204204
_ condition: @autoclosure () -> Bool, _ message: StaticString = StaticString(),
205205
file: StaticString = #file, line: UInt = #line
206206
) {
@@ -216,8 +216,8 @@ public func _precondition(
216216
}
217217
}
218218

219-
@_transparent
220-
public func _preconditionFailure(
219+
@usableFromInline @_transparent
220+
internal func _preconditionFailure(
221221
_ message: StaticString = StaticString(),
222222
file: StaticString = #file, line: UInt = #line
223223
) -> Never {

0 commit comments

Comments
 (0)