That thunk is just a shim to match the calling convention between what _introSortImpl expects for its closure argument and the normal calling convention for the function that was passed to sort. The real crash is most likely in the inner function.
I think the best way forward here is to look at this crash report at the assembly level to determine the immediate cause of the crash. That’s not necessarily easy, so before that I think it’s worthwhile to rule out this possibility:
Wondering if the startTime property could be nil for some of the
elements in the collection. The Obj-C class is defined using NS_ASSUME_NONNULL_BEGIN.
To that end I created a tiny test project with code like this:
let v1 = Visit(startTime: Date(timeIntervalSinceReferenceDate: 0.0))
let v2 = Visit(startTime: Date(timeIntervalSinceReferenceDate: 1.0))
let v3 = Visit(startTime: nil)
let v4 = Visit(startTime: Date(timeIntervalSinceReferenceDate: 2.0))
let visits = [v1, v2, v3, v4]
let visitsSorted = visits.sorted { $0.startTime < $1.startTime }
where Visit is an Objective-C object that claims that startTime is never nil but where it actually can be.
When I run this code (Xcode 9.4, optimised build of Mac command-line tool) I see a crash like this:
(lldb) bt
* … stop reason = EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP, subcode=0x0)
* frame #0: … static Foundation.Date._unconditionallyBridgeFromObjectiveC(Swift.Optional<__ObjC.NSDate>) -> Foundation.Date + 54
frame #1: … specialized thunk for @callee_guaranteed (@owned Visit, @owned Visit) -> (@unowned Bool, @error @owned Error) [inlined] function signature specialization <Arg[0] = Owned To Guaranteed, Arg[1] = Owned To Guaranteed> of closure #1 ($0=0x0000000100e08450, $1=0x0000000100e08dc0) -> Swift.Bool in xxst.main() -> () at main.swift:18 [opt]
…
frame #13: … specialized MutableCollection.sorted(by:) at main.swift:0 [opt]
frame #14: … main() [inlined] generic specialization <Swift.Array<__ObjC.Visit>> of (extension in Swift):Swift.MutableCollection.sorted(by: (A.Element, A.Element) throws -> Swift.Bool) throws -> Swift.Array<A.Element> at main.swift:0 [opt]
frame #15: … main() at main.swift:18 [opt]
frame #16: … main at main.swift:22 [opt]
…
Note the EXC_BAD_INSTRUCTION, which is a trap indicating that frame 0 (Foundation.Date._unconditionallyBridgeFromObjectiveC…) explicitly caught and trapped in this case.