Skip to content

Commit 03cbb49

Browse files
author
Max Moiseev
committed
[stdlib] _stdlibAssert => _debugPrecondition
1 parent f5511da commit 03cbb49

17 files changed

+50
-50
lines changed

Diff for: stdlib/public/core/Arrays.swift.gyb

+2-2
Original file line numberDiff line numberDiff line change
@@ -955,12 +955,12 @@ internal func _arrayOutOfPlaceReplace<
955955
)
956956
}
957957

958-
/// A _stdlibAssert check that `i` has exactly reached the end of
958+
/// A _debugPrecondition check that `i` has exactly reached the end of
959959
/// `s`. This test is never used to ensure memory safety; that is
960960
/// always guaranteed by measuring `s` once and re-using that value.
961961
// FIXME(ABI): add argument labels.
962962
internal func _expectEnd<C : Collection>(i: C.Index, _ s: C) {
963-
_stdlibAssert(
963+
_debugPrecondition(
964964
i == s.endIndex,
965965
"invalid Collection: count differed in successive traversals")
966966
}

Diff for: stdlib/public/core/Assert.swift

+2-2
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ public func _overflowChecked<T>(
209209
/// They are meant to be used when the check is not comprehensively checking for
210210
/// all possible errors.
211211
@_transparent
212-
public func _stdlibAssert(
212+
public func _debugPrecondition(
213213
@autoclosure condition: () -> Bool, _ message: StaticString = StaticString(),
214214
file: StaticString = #file, line: UInt = #line
215215
) {
@@ -223,7 +223,7 @@ public func _stdlibAssert(
223223
}
224224

225225
@_transparent @noreturn
226-
public func _stdlibAssertionFailure(
226+
public func _debugPreconditionFailure(
227227
message: StaticString = StaticString(),
228228
file: StaticString = #file, line: UInt = #line) {
229229
if _isDebugAssertConfiguration() {

Diff for: stdlib/public/core/BridgeObjectiveC.swift

+3-3
Original file line numberDiff line numberDiff line change
@@ -395,7 +395,7 @@ public struct AutoreleasingUnsafeMutablePointer<Pointee /* TODO : class */>
395395
public var pointee: Pointee {
396396
/// Retrieve the value the pointer points to.
397397
@_transparent get {
398-
_stdlibAssert(!_isNull)
398+
_debugPrecondition(!_isNull)
399399
// We can do a strong load normally.
400400
return UnsafeMutablePointer<Pointee>(self).pointee
401401
}
@@ -406,7 +406,7 @@ public struct AutoreleasingUnsafeMutablePointer<Pointee /* TODO : class */>
406406
/// in ARC. This autoreleases the argument before trivially
407407
/// storing it to the referenced memory.
408408
@_transparent nonmutating set {
409-
_stdlibAssert(!_isNull)
409+
_debugPrecondition(!_isNull)
410410
// Autorelease the object reference.
411411
typealias OptionalAnyObject = AnyObject?
412412
Builtin.retain(unsafeBitCast(newValue, to: OptionalAnyObject.self))
@@ -427,7 +427,7 @@ public struct AutoreleasingUnsafeMutablePointer<Pointee /* TODO : class */>
427427
public subscript(i: Int) -> Pointee {
428428
@_transparent
429429
get {
430-
_stdlibAssert(!_isNull)
430+
_debugPrecondition(!_isNull)
431431
// We can do a strong load normally.
432432
return (UnsafePointer<Pointee>(self) + i).pointee
433433
}

Diff for: stdlib/public/core/Builtin.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,7 @@ public func _unsafeReferenceCast<T, U>(x: T, to: U.Type) -> U {
236236
@_transparent
237237
@warn_unused_result
238238
public func unsafeDowncast<T : AnyObject>(x: AnyObject, to: T.Type) -> T {
239-
_stdlibAssert(x is T, "invalid unsafeDowncast")
239+
_debugPrecondition(x is T, "invalid unsafeDowncast")
240240
return Builtin.castReference(x)
241241
}
242242

Diff for: stdlib/public/core/ManagedBuffer.swift

+2-2
Original file line numberDiff line numberDiff line change
@@ -360,15 +360,15 @@ public struct ManagedBufferPointer<Value, Element> : Equatable {
360360
internal static func _checkValidBufferClass(
361361
bufferClass: AnyClass, creating: Bool = false
362362
) {
363-
_stdlibAssert(
363+
_debugPrecondition(
364364
_class_getInstancePositiveExtentSize(bufferClass) == sizeof(_HeapObject.self)
365365
|| (
366366
!creating
367367
&& _class_getInstancePositiveExtentSize(bufferClass)
368368
== _valueOffset + sizeof(Value.self)),
369369
"ManagedBufferPointer buffer class has illegal stored properties"
370370
)
371-
_stdlibAssert(
371+
_debugPrecondition(
372372
_usesNativeSwiftReferenceCounting(bufferClass),
373373
"ManagedBufferPointer buffer class must be non-@objc"
374374
)

Diff for: stdlib/public/core/Optional.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ public enum Optional<Wrapped> : NilLiteralConvertible {
6767
if let x = self {
6868
return x
6969
}
70-
_stdlibAssertionFailure("unsafelyUnwrapped of nil optional")
70+
_debugPreconditionFailure("unsafelyUnwrapped of nil optional")
7171
}
7272
}
7373

Diff for: stdlib/public/core/Pointer.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ func _convertMutableArrayToPointerArgument<
5555

5656
// Call reserve to force contiguous storage.
5757
a.reserveCapacity(0)
58-
_stdlibAssert(a._baseAddressIfContiguous != nil || a.isEmpty)
58+
_debugPrecondition(a._baseAddressIfContiguous != nil || a.isEmpty)
5959

6060
return (a._owner, ToPointer(a._baseAddressIfContiguous._rawValue))
6161
}

Diff for: stdlib/public/core/Range.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ public struct Range<
9494
/// - Precondition: `position` is a valid position in `self` and
9595
/// `position != endIndex`.
9696
public subscript(position: Element) -> Element {
97-
_stdlibAssert(position != endIndex, "Index out of range")
97+
_debugPrecondition(position != endIndex, "Index out of range")
9898
return position
9999
}
100100

Diff for: stdlib/public/core/Sort.swift.gyb

+1-1
Original file line numberDiff line numberDiff line change
@@ -312,7 +312,7 @@ public func swap<T>(a: inout T, _ b: inout T) {
312312
// Microoptimized to avoid retain/release traffic.
313313
let p1 = Builtin.addressof(&a)
314314
let p2 = Builtin.addressof(&b)
315-
_stdlibAssert(
315+
_debugPrecondition(
316316
p1 != p2,
317317
"swapping a location with itself is not supported")
318318

Diff for: stdlib/public/core/Unmanaged.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public struct Unmanaged<Instance : AnyObject> {
3131
public static func fromOpaque(value: OpaquePointer) -> Unmanaged {
3232
// Null pointer check is a debug check, because it guards only against one
3333
// specific bad pointer value.
34-
_stdlibAssert(
34+
_debugPrecondition(
3535
value != nil,
3636
"attempt to create an Unmanaged instance from a null pointer")
3737

Diff for: stdlib/public/core/UnsafeBufferPointer.swift.gyb

+4-4
Original file line numberDiff line numberDiff line change
@@ -55,14 +55,14 @@ public struct Unsafe${Mutable}BufferPointer<Element>
5555
/// Access the `i`th element in the buffer.
5656
public subscript(i: Int) -> Element {
5757
get {
58-
_stdlibAssert(i >= 0)
59-
_stdlibAssert(i < endIndex)
58+
_debugPrecondition(i >= 0)
59+
_debugPrecondition(i < endIndex)
6060
return _position[i]
6161
}
6262
%if Mutable:
6363
nonmutating set {
64-
_stdlibAssert(i >= 0)
65-
_stdlibAssert(i < endIndex)
64+
_debugPrecondition(i >= 0)
65+
_debugPrecondition(i < endIndex)
6666
_position[i] = newValue
6767
}
6868
%end

Diff for: stdlib/public/core/UnsafePointer.swift.gyb

+14-14
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ public struct ${Self}<Pointee>
144144
/// be destroyed or moved from to avoid leaks.
145145
// FIXME: add tests (since the `count` has been added)
146146
public func initialize(with newValue: Pointee, count: Int = 1) {
147-
_stdlibAssert(count >= 0,
147+
_debugPrecondition(count >= 0,
148148
"${Self}.initialize(with:): negative count")
149149
// Must not use `initializeFrom` with a `Collection` as that will introduce
150150
// a cycle.
@@ -179,9 +179,9 @@ public struct ${Self}<Pointee>
179179
/// - Precondition: The `Pointee`s at `self..<self + count` and
180180
/// `source..<source + count` are initialized.
181181
public func assignFrom(source: ${Self}, count: Int) {
182-
_stdlibAssert(
182+
_debugPrecondition(
183183
count >= 0, "${Self}.assignFrom with negative count")
184-
_stdlibAssert(
184+
_debugPrecondition(
185185
self < source || self >= source + count,
186186
"assignFrom non-following overlapping range; use assignBackwardFrom")
187187
for i in 0..<count {
@@ -204,9 +204,9 @@ public struct ${Self}<Pointee>
204204
/// - Precondition: The `Pointee`s at `self..<self + count` and
205205
/// `source..<source + count` are initialized.
206206
public func assignBackwardFrom(source: ${Self}, count: Int) {
207-
_stdlibAssert(
207+
_debugPrecondition(
208208
count >= 0, "${Self}.assignBackwardFrom with negative count")
209-
_stdlibAssert(
209+
_debugPrecondition(
210210
source < self || source >= self + count,
211211
"${Self}.assignBackwardFrom non-preceding overlapping range; use assignFrom instead")
212212
var i = count-1
@@ -234,9 +234,9 @@ public struct ${Self}<Pointee>
234234
/// initialized and the memory at `source..<source + count` is
235235
/// uninitialized.
236236
public func moveInitializeFrom(source: ${Self}, count: Int) {
237-
_stdlibAssert(
237+
_debugPrecondition(
238238
count >= 0, "${Self}.moveInitializeFrom with negative count")
239-
_stdlibAssert(
239+
_debugPrecondition(
240240
self < source || self >= source + count,
241241
"${Self}.moveInitializeFrom non-following overlapping range; use moveInitializeBackwardFrom")
242242
Builtin.takeArrayFrontToBack(
@@ -265,9 +265,9 @@ public struct ${Self}<Pointee>
265265
/// initialized and the memory at `source..<source + count` is
266266
/// uninitialized.
267267
public func moveInitializeBackwardFrom(source: ${Self}, count: Int) {
268-
_stdlibAssert(
268+
_debugPrecondition(
269269
count >= 0, "${Self}.moveInitializeBackwardFrom with negative count")
270-
_stdlibAssert(
270+
_debugPrecondition(
271271
source < self || source >= self + count,
272272
"${Self}.moveInitializeBackwardFrom non-preceding overlapping range; use moveInitializeFrom instead")
273273
Builtin.takeArrayBackToFront(
@@ -296,9 +296,9 @@ public struct ${Self}<Pointee>
296296
/// - Postcondition: The `Pointee`s at `self..<self + count` and
297297
/// `source..<source + count` are initialized.
298298
public func initializeFrom(source: ${Self}, count: Int) {
299-
_stdlibAssert(
299+
_debugPrecondition(
300300
count >= 0, "${Self}.initializeFrom with negative count")
301-
_stdlibAssert(
301+
_debugPrecondition(
302302
self + count <= source || source + count <= self,
303303
"${Self}.initializeFrom non-following overlapping range")
304304
Builtin.copyArray(
@@ -337,9 +337,9 @@ public struct ${Self}<Pointee>
337337
/// initialized and the `Pointees` at `source..<source + count`
338338
/// are uninitialized.
339339
public func moveAssignFrom(source: ${Self}, count: Int) {
340-
_stdlibAssert(
340+
_debugPrecondition(
341341
count >= 0, "${Self}.moveAssignFrom with negative count")
342-
_stdlibAssert(
342+
_debugPrecondition(
343343
self + count <= source || source + count <= self,
344344
"moveAssignFrom overlapping range")
345345
Builtin.destroyArray(Pointee.self, self._rawValue, count._builtinWordValue)
@@ -359,7 +359,7 @@ public struct ${Self}<Pointee>
359359
///
360360
/// - Postcondition: The memory is uninitialized.
361361
public func deinitialize(count count: Int = 1) {
362-
_stdlibAssert(count >= 0, "${Self}.deinitialize with negative count")
362+
_debugPrecondition(count >= 0, "${Self}.deinitialize with negative count")
363363
// FIXME: optimization should be implemented, where if the `count` value
364364
// is 1, the `Builtin.destroy(Pointee.self, _rawValue)` gets called.
365365
Builtin.destroyArray(Pointee.self, _rawValue, count._builtinWordValue)

Diff for: test/1_stdlib/Unmanaged.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ var UnmanagedTests = TestSuite("Unmanaged")
2727
UnmanagedTests.test("fromOpaque()/trap")
2828
.skip(.custom(
2929
{ !_isDebugAssertConfiguration() },
30-
reason: "init(bitPattern:) does a _stdlibAssert() for null pointers"))
30+
reason: "init(bitPattern:) does a _debugPrecondition() for null pointers"))
3131
.code {
3232
let null: OpaquePointer = getPointer(nil)
3333
expectCrashLater()

Diff for: test/1_stdlib/UnsafePointer.swift.gyb

+8-8
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ UnsafeMutablePointerTestSuite.test("moveInitializeBackwardFrom") {
192192
check(Check.RightOverlap)
193193
check(Check.Disjoint)
194194

195-
// This check relies on _stdlibAssert() so will only trigger in -Onone mode.
195+
// This check relies on _debugPrecondition() so will only trigger in -Onone mode.
196196
if _isDebugAssertConfiguration() {
197197
expectCrashLater()
198198
check(Check.LeftOverlap)
@@ -202,7 +202,7 @@ UnsafeMutablePointerTestSuite.test("moveInitializeBackwardFrom") {
202202
UnsafeMutablePointerTestSuite.test("moveAssignFrom") {
203203
let check = checkPtr(UnsafeMutablePointer.moveAssignFrom, true)
204204
check(Check.Disjoint)
205-
// This check relies on _stdlibAssert() so will only trigger in -Onone mode.
205+
// This check relies on _debugPrecondition() so will only trigger in -Onone mode.
206206
if _isDebugAssertConfiguration() {
207207
expectCrashLater()
208208
check(Check.LeftOverlap)
@@ -211,7 +211,7 @@ UnsafeMutablePointerTestSuite.test("moveAssignFrom") {
211211

212212
UnsafeMutablePointerTestSuite.test("moveAssignFrom.Right") {
213213
let check = checkPtr(UnsafeMutablePointer.moveAssignFrom, true)
214-
// This check relies on _stdlibAssert() so will only trigger in -Onone mode.
214+
// This check relies on _debugPrecondition() so will only trigger in -Onone mode.
215215
if _isDebugAssertConfiguration() {
216216
expectCrashLater()
217217
check(Check.RightOverlap)
@@ -222,7 +222,7 @@ UnsafeMutablePointerTestSuite.test("assignFrom") {
222222
let check = checkPtr(UnsafeMutablePointer.assignFrom, true)
223223
check(Check.LeftOverlap)
224224
check(Check.Disjoint)
225-
// This check relies on _stdlibAssert() so will only trigger in -Onone mode.
225+
// This check relies on _debugPrecondition() so will only trigger in -Onone mode.
226226
if _isDebugAssertConfiguration() {
227227
expectCrashLater()
228228
check(Check.RightOverlap)
@@ -233,7 +233,7 @@ UnsafeMutablePointerTestSuite.test("assignBackwardFrom") {
233233
let check = checkPtr(UnsafeMutablePointer.assignBackwardFrom, true)
234234
check(Check.RightOverlap)
235235
check(Check.Disjoint)
236-
// This check relies on _stdlibAssert() so will only trigger in -Onone mode.
236+
// This check relies on _debugPrecondition() so will only trigger in -Onone mode.
237237
if _isDebugAssertConfiguration() {
238238
expectCrashLater()
239239
check(Check.LeftOverlap)
@@ -244,7 +244,7 @@ UnsafeMutablePointerTestSuite.test("moveInitializeFrom") {
244244
let check = checkPtr(UnsafeMutablePointer.moveInitializeFrom, false)
245245
check(Check.LeftOverlap)
246246
check(Check.Disjoint)
247-
// This check relies on _stdlibAssert() so will only trigger in -Onone mode.
247+
// This check relies on _debugPrecondition() so will only trigger in -Onone mode.
248248
if _isDebugAssertConfiguration() {
249249
expectCrashLater()
250250
check(Check.RightOverlap)
@@ -254,7 +254,7 @@ UnsafeMutablePointerTestSuite.test("moveInitializeFrom") {
254254
UnsafeMutablePointerTestSuite.test("initializeFrom") {
255255
let check = checkPtr(UnsafeMutablePointer.initializeFrom, false)
256256
check(Check.Disjoint)
257-
// This check relies on _stdlibAssert() so will only trigger in -Onone mode.
257+
// This check relies on _debugPrecondition() so will only trigger in -Onone mode.
258258
if _isDebugAssertConfiguration() {
259259
expectCrashLater()
260260
check(Check.LeftOverlap)
@@ -263,7 +263,7 @@ UnsafeMutablePointerTestSuite.test("initializeFrom") {
263263

264264
UnsafeMutablePointerTestSuite.test("initializeFrom.Right") {
265265
let check = checkPtr(UnsafeMutablePointer.initializeFrom, false)
266-
// This check relies on _stdlibAssert() so will only trigger in -Onone mode.
266+
// This check relies on _debugPrecondition() so will only trigger in -Onone mode.
267267
if _isDebugAssertConfiguration() {
268268
expectCrashLater()
269269
check(Check.RightOverlap)

Diff for: test/Frontend/OptimizationOptions-with-stdlib-checks.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ func test_precondition_check(x: Int, y: Int) -> Int {
2525
}
2626

2727
func test_partial_safety_check(x: Int, y: Int) -> Int {
28-
_stdlibAssert(x > y, "test partial safety check")
28+
_debugPrecondition(x > y, "test partial safety check")
2929
return x + y
3030
}
3131

Diff for: test/Frontend/OptimizationOptions-without-stdlib-checks.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ func test_precondition_check(x: Int, y: Int) -> Int {
2525
}
2626

2727
func test_partial_safety_check(x: Int, y: Int) -> Int {
28-
_stdlibAssert(x > y, "test partial safety check")
28+
_debugPrecondition(x > y, "test partial safety check")
2929
return x + y
3030
}
3131

Diff for: validation-test/stdlib/Assert.swift

+6-6
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ func testTrapsAreNoreturn(i: Int) -> Int {
3434
case 3:
3535
_preconditionFailure("cannot happen")
3636
case 4:
37-
_stdlibAssertionFailure("cannot happen")
37+
_debugPreconditionFailure("cannot happen")
3838
case 5:
3939
_sanityCheckFailure("cannot happen")
4040

@@ -176,26 +176,26 @@ Assert.test("_preconditionFailure")
176176
_preconditionFailure("this should fail")
177177
}
178178

179-
Assert.test("_stdlibAssert")
179+
Assert.test("_debugPrecondition")
180180
.xfail(.custom(
181181
{ !_isDebugAssertConfiguration() },
182182
reason: "debug preconditions are disabled in Release and Unchecked mode"))
183183
.crashOutputMatches(_isDebugAssertConfiguration() ? "this should fail" : "")
184184
.code {
185185
var x = 2
186-
_stdlibAssert(x * 21 == 42, "should not fail")
186+
_debugPrecondition(x * 21 == 42, "should not fail")
187187
expectCrashLater()
188-
_stdlibAssert(x == 42, "this should fail")
188+
_debugPrecondition(x == 42, "this should fail")
189189
}
190190

191-
Assert.test("_stdlibAssertionFailure")
191+
Assert.test("_debugPreconditionFailure")
192192
.skip(.custom(
193193
{ !_isDebugAssertConfiguration() },
194194
reason: "optimizer assumes that the code path is unreachable"))
195195
.crashOutputMatches("this should fail")
196196
.code {
197197
expectCrashLater()
198-
_stdlibAssertionFailure("this should fail")
198+
_debugPreconditionFailure("this should fail")
199199
}
200200

201201
Assert.test("_sanityCheck")

0 commit comments

Comments
 (0)