[stdlib] replace precondition with guard-else-_assertionFailure

This commit is contained in:
Guillaume Lessard
2021-11-04 21:33:32 -06:00
committed by Hassan
parent 1d4f220ed4
commit e87a3e30ca

View File

@@ -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()
)
}
}
}
@@ -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
}