Skip to content

Commit bbd6759

Browse files
authored
Merge pull request #40056 from HassanElDesouky/stdlib-internalPrecondition
[stdlib] Replace `precondition` with the internal `_precondition`
2 parents 89c9778 + e87a3e3 commit bbd6759

File tree

9 files changed

+48
-36
lines changed

9 files changed

+48
-36
lines changed

stdlib/public/core/ArrayBuffer.swift

Lines changed: 40 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -254,23 +254,29 @@ extension _ArrayBuffer {
254254
internal func _typeCheckSlowPath(_ index: Int) {
255255
if _fastPath(_isNative) {
256256
let element: AnyObject = cast(toBufferOf: AnyObject.self)._native[index]
257-
precondition(
258-
element is Element,
259-
"""
260-
Down-casted Array element failed to match the target type
261-
Expected \(Element.self) but found \(type(of: element))
262-
"""
263-
)
257+
guard element is Element else {
258+
_assertionFailure(
259+
"Fatal error",
260+
"""
261+
Down-casted Array element failed to match the target type
262+
Expected \(Element.self) but found \(type(of: element))
263+
""",
264+
flags: _fatalErrorFlags()
265+
)
266+
}
264267
}
265268
else {
266269
let element = _nonNative[index]
267-
precondition(
268-
element is Element,
269-
"""
270-
NSArray element failed to match the Swift Array Element type
271-
Expected \(Element.self) but found \(type(of: element))
272-
"""
273-
)
270+
guard element is Element else {
271+
_assertionFailure(
272+
"Fatal error",
273+
"""
274+
NSArray element failed to match the Swift Array Element type
275+
Expected \(Element.self) but found \(type(of: element))
276+
""",
277+
flags: _fatalErrorFlags()
278+
)
279+
}
274280
}
275281
}
276282

@@ -505,23 +511,29 @@ extension _ArrayBuffer {
505511
_native._checkValidSubscript(i)
506512

507513
element = cast(toBufferOf: AnyObject.self)._native[i]
508-
precondition(
509-
element is Element,
510-
"""
511-
Down-casted Array element failed to match the target type
512-
Expected \(Element.self) but found \(type(of: element))
513-
"""
514-
)
514+
guard element is Element else {
515+
_assertionFailure(
516+
"Fatal error",
517+
"""
518+
Down-casted Array element failed to match the target type
519+
Expected \(Element.self) but found \(type(of: element))
520+
""",
521+
flags: _fatalErrorFlags()
522+
)
523+
}
515524
} else {
516525
// ObjC arrays do their own subscript checking.
517526
element = _nonNative[i]
518-
precondition(
519-
element is Element,
520-
"""
521-
NSArray element failed to match the Swift Array Element type
522-
Expected \(Element.self) but found \(type(of: element))
523-
"""
524-
)
527+
guard element is Element else {
528+
_assertionFailure(
529+
"Fatal error",
530+
"""
531+
NSArray element failed to match the Swift Array Element type
532+
Expected \(Element.self) but found \(type(of: element))
533+
""",
534+
flags: _fatalErrorFlags()
535+
)
536+
}
525537
}
526538
return element
527539
}

stdlib/public/core/Misc.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ func _mangledTypeName(_ type: Any.Type) -> String? {
8181
let (result, repairsMade) = String._fromUTF8Repairing(
8282
UnsafeBufferPointer(start: stringPtr, count: count))
8383

84-
precondition(!repairsMade, "repairs made to _mangledTypeName, this is not expected since names should be valid UTF-8")
84+
_precondition(!repairsMade, "repairs made to _mangledTypeName, this is not expected since names should be valid UTF-8")
8585

8686
return result
8787
}

stdlib/public/core/Sort.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -696,6 +696,6 @@ extension UnsafeMutableBufferPointer {
696696
}
697697

698698
// FIXME: Remove this, it works around rdar://problem/45044610
699-
precondition(result)
699+
_precondition(result)
700700
}
701701
}

stdlib/public/core/StringBridge.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -481,7 +481,7 @@ private func getConstantTaggedCocoaContents(_ cocoaString: _CocoaString) ->
481481
let length = ivarPointer.pointee.length
482482
let isUTF16Mask:UInt = 0x0000_0000_0000_0004 //CFStringFlags bit 4: isUnicode
483483
let isASCII = ivarPointer.pointee.flags & isUTF16Mask == 0
484-
precondition(isASCII) // we don't currently support non-ASCII here
484+
_precondition(isASCII) // we don't currently support non-ASCII here
485485
let contentsPtr = ivarPointer.pointee.str
486486
return (
487487
utf16Length: Int(length),

stdlib/public/core/StringLegacy.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ extension String {
2828
/// - count: The number of times to repeat `repeatedValue` in the resulting
2929
/// string.
3030
public init(repeating repeatedValue: String, count: Int) {
31-
precondition(count >= 0, "Negative count not allowed")
31+
_precondition(count >= 0, "Negative count not allowed")
3232
guard count > 1 else {
3333
self = count == 0 ? "" : repeatedValue
3434
return

stdlib/public/core/StringStorage.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -445,7 +445,7 @@ extension __StringStorage {
445445

446446
// @opaque
447447
fileprivate var _breadcrumbsAddress: UnsafeMutablePointer<_StringBreadcrumbs?> {
448-
precondition(
448+
_precondition(
449449
hasBreadcrumbs, "Internal error: string breadcrumbs not present")
450450
return UnsafeMutablePointer(_realCapacityEnd)
451451
}

stdlib/public/core/StringUTF16View.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ extension String.UTF16View: BidirectionalCollection {
158158

159159
@inlinable @inline(__always)
160160
public func index(before idx: Index) -> Index {
161-
precondition(!idx.isZeroPosition)
161+
_precondition(!idx.isZeroPosition)
162162
if _slowPath(_guts.isForeign) { return _foreignIndex(before: idx) }
163163
if _guts.isASCII { return idx.priorEncoded }
164164

stdlib/public/core/StringUTF8View.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ extension String.UTF8View: BidirectionalCollection {
145145

146146
@inlinable @inline(__always)
147147
public func index(before i: Index) -> Index {
148-
precondition(!i.isZeroPosition)
148+
_precondition(!i.isZeroPosition)
149149
if _fastPath(_guts.isFastUTF8) {
150150
return i.strippingTranscoding.priorEncoded
151151
}

stdlib/public/core/StringUnicodeScalarView.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ extension String.UnicodeScalarView: BidirectionalCollection {
129129
@inlinable @inline(__always)
130130
public func index(before i: Index) -> Index {
131131
let i = _guts.scalarAlign(i)
132-
precondition(i._encodedOffset > 0)
132+
_precondition(i._encodedOffset > 0)
133133
// TODO(String performance): isASCII fast-path
134134

135135
if _fastPath(_guts.isFastUTF8) {

0 commit comments

Comments
 (0)