Skip to content

[stdlib] Replace precondition with the internal _precondition #40056

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 40 additions & 28 deletions stdlib/public/core/ArrayBuffer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -254,23 +254,29 @@ extension _ArrayBuffer {
internal func _typeCheckSlowPath(_ index: Int) {
if _fastPath(_isNative) {
let element: AnyObject = cast(toBufferOf: AnyObject.self)._native[index]
precondition(
element is Element,
"""
Down-casted Array element failed to match the target type
Expected \(Element.self) but found \(type(of: element))
"""
)
guard element is Element else {
_assertionFailure(
"Fatal error",
"""
Down-casted Array element failed to match the target type
Expected \(Element.self) but found \(type(of: element))
""",
flags: _fatalErrorFlags()
)
}
}
else {
let element = _nonNative[index]
precondition(
element is Element,
"""
NSArray element failed to match the Swift Array Element type
Expected \(Element.self) but found \(type(of: element))
"""
)
guard element is Element else {
_assertionFailure(
"Fatal error",
"""
NSArray element failed to match the Swift Array Element type
Expected \(Element.self) but found \(type(of: element))
""",
flags: _fatalErrorFlags()
)
}
}
}

Expand Down Expand Up @@ -505,23 +511,29 @@ extension _ArrayBuffer {
_native._checkValidSubscript(i)

element = cast(toBufferOf: AnyObject.self)._native[i]
precondition(
element is Element,
"""
Down-casted Array element failed to match the target type
Expected \(Element.self) but found \(type(of: element))
"""
)
guard element is Element else {
_assertionFailure(
"Fatal error",
"""
Down-casted Array element failed to match the target type
Expected \(Element.self) but found \(type(of: element))
""",
flags: _fatalErrorFlags()
)
}
} else {
// ObjC arrays do their own subscript checking.
element = _nonNative[i]
precondition(
element is Element,
"""
NSArray element failed to match the Swift Array Element type
Expected \(Element.self) but found \(type(of: element))
"""
)
guard element is Element else {
_assertionFailure(
"Fatal error",
"""
NSArray element failed to match the Swift Array Element type
Expected \(Element.self) but found \(type(of: element))
""",
flags: _fatalErrorFlags()
)
}
}
return element
}
Expand Down
2 changes: 1 addition & 1 deletion stdlib/public/core/Misc.swift
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ func _mangledTypeName(_ type: Any.Type) -> String? {
let (result, repairsMade) = String._fromUTF8Repairing(
UnsafeBufferPointer(start: stringPtr, count: count))

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

return result
}
Expand Down
2 changes: 1 addition & 1 deletion stdlib/public/core/Sort.swift
Original file line number Diff line number Diff line change
Expand Up @@ -696,6 +696,6 @@ extension UnsafeMutableBufferPointer {
}

// FIXME: Remove this, it works around rdar://problem/45044610
precondition(result)
_precondition(result)
}
}
2 changes: 1 addition & 1 deletion stdlib/public/core/StringBridge.swift
Original file line number Diff line number Diff line change
Expand Up @@ -481,7 +481,7 @@ private func getConstantTaggedCocoaContents(_ cocoaString: _CocoaString) ->
let length = ivarPointer.pointee.length
let isUTF16Mask:UInt = 0x0000_0000_0000_0004 //CFStringFlags bit 4: isUnicode
let isASCII = ivarPointer.pointee.flags & isUTF16Mask == 0
precondition(isASCII) // we don't currently support non-ASCII here
_precondition(isASCII) // we don't currently support non-ASCII here
let contentsPtr = ivarPointer.pointee.str
return (
utf16Length: Int(length),
Expand Down
2 changes: 1 addition & 1 deletion stdlib/public/core/StringLegacy.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ extension String {
/// - count: The number of times to repeat `repeatedValue` in the resulting
/// string.
public init(repeating repeatedValue: String, count: Int) {
precondition(count >= 0, "Negative count not allowed")
_precondition(count >= 0, "Negative count not allowed")
guard count > 1 else {
self = count == 0 ? "" : repeatedValue
return
Expand Down
2 changes: 1 addition & 1 deletion stdlib/public/core/StringStorage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -445,7 +445,7 @@ extension __StringStorage {

// @opaque
fileprivate var _breadcrumbsAddress: UnsafeMutablePointer<_StringBreadcrumbs?> {
precondition(
_precondition(
hasBreadcrumbs, "Internal error: string breadcrumbs not present")
return UnsafeMutablePointer(_realCapacityEnd)
}
Expand Down
2 changes: 1 addition & 1 deletion stdlib/public/core/StringUTF16View.swift
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ extension String.UTF16View: BidirectionalCollection {

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

Expand Down
2 changes: 1 addition & 1 deletion stdlib/public/core/StringUTF8View.swift
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ extension String.UTF8View: BidirectionalCollection {

@inlinable @inline(__always)
public func index(before i: Index) -> Index {
precondition(!i.isZeroPosition)
_precondition(!i.isZeroPosition)
if _fastPath(_guts.isFastUTF8) {
return i.strippingTranscoding.priorEncoded
}
Expand Down
2 changes: 1 addition & 1 deletion stdlib/public/core/StringUnicodeScalarView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ extension String.UnicodeScalarView: BidirectionalCollection {
@inlinable @inline(__always)
public func index(before i: Index) -> Index {
let i = _guts.scalarAlign(i)
precondition(i._encodedOffset > 0)
_precondition(i._encodedOffset > 0)
// TODO(String performance): isASCII fast-path

if _fastPath(_guts.isFastUTF8) {
Expand Down