Skip to content

Commit 6c0d325

Browse files
author
Lance Parker
authored
Merge pull request #17841 from lancep/print_mismatched_types
[stdlib]Better error message for Arrays with mismatched types
2 parents 823d63b + 6375508 commit 6c0d325

File tree

2 files changed

+27
-10
lines changed

2 files changed

+27
-10
lines changed

stdlib/public/core/ArrayBuffer.swift

+26-9
Original file line numberDiff line numberDiff line change
@@ -196,15 +196,24 @@ extension _ArrayBuffer {
196196
internal func _typeCheckSlowPath(_ index: Int) {
197197
if _fastPath(_isNative) {
198198
let element: AnyObject = cast(toBufferOf: AnyObject.self)._native[index]
199-
_precondition(
199+
precondition(
200200
element is Element,
201-
"Down-casted Array element failed to match the target type")
201+
"""
202+
Down-casted Array element failed to match the target type
203+
Expected \(Element.self) but found \(type(of: element))
204+
"""
205+
)
202206
}
203207
else {
204208
let ns = _nonNative
205-
_precondition(
206-
ns.objectAt(index) is Element,
207-
"NSArray element failed to match the Swift Array Element type")
209+
let element = ns.objectAt(index)
210+
precondition(
211+
element is Element,
212+
"""
213+
NSArray element failed to match the Swift Array Element type
214+
Expected \(Element.self) but found \(type(of: element))
215+
"""
216+
)
208217
}
209218
}
210219

@@ -404,15 +413,23 @@ extension _ArrayBuffer {
404413
_native._checkValidSubscript(i)
405414

406415
element = cast(toBufferOf: AnyObject.self)._native[i]
407-
_precondition(
416+
precondition(
408417
element is Element,
409-
"Down-casted Array element failed to match the target type")
418+
"""
419+
Down-casted Array element failed to match the target type
420+
Expected \(Element.self) but found \(type(of: element))
421+
"""
422+
)
410423
} else {
411424
// ObjC arrays do their own subscript checking.
412425
element = _nonNative.objectAt(i)
413-
_precondition(
426+
precondition(
414427
element is Element,
415-
"NSArray element failed to match the Swift Array Element type")
428+
"""
429+
NSArray element failed to match the Swift Array Element type
430+
Expected \(Element.self) but found \(type(of: element))
431+
"""
432+
)
416433
}
417434
return element
418435
}

stdlib/public/core/Assert.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ public func precondition(
8585
_ message: @autoclosure () -> String = String(),
8686
file: StaticString = #file, line: UInt = #line
8787
) {
88-
// Only check in debug and release mode. In release mode just trap.
88+
// Only check in debug and release mode. In release mode just trap.
8989
if _isDebugAssertConfiguration() {
9090
if !_branchHint(condition(), expected: true) {
9191
_assertionFailure("Precondition failed", message(), file: file, line: line,

0 commit comments

Comments
 (0)