Open
Description
Previous ID | SR-10058 |
Radar | rdar://problem/48703153 |
Original Reporter | @tkrajacic |
Type | Bug |
Attachment: Download
Additional Detail from JIRA
Votes | 0 |
Component/s | Foundation |
Labels | Bug, 5.0Regression, RunTimeCrash |
Assignee | None |
Priority | Medium |
md5: 164979c1f09e3db1a7e8921ff1597e14
Issue Description:
Attached is a sample app that shows a runtime crash when compiling the app with the Swift 5 toolchain (tested in Xcode 10.2b4 integrated or 2019-03-05 toolchain) in Swift 4.2 language mode.
This only happens when the language is set to Swift 4.2
When setting the language to Swift 5, there is no crash.
Using Xcode 10.1 also works fine with Swift 4.2. Using Swift 5 toolchain with Xcode 10.1 also crashes.
This is related to SR-6066
func observe<Value>(_ keyPath: KeyPath<Self, Value>, onChange: @escaping (Value) -> ()) -> Disposable {
let observation = observe(keyPath, options: [.initial, .new]) { _, change in
// See: https://bugs.swift.org/browse/SR-6066
let newValue = change.newValue ?? (nil as Any? as! Value) // Crash here!!
onChange(newValue)
}
return Disposable { observation.invalidate() }
}
Backtrace:
* thread #​1, queue = 'com.apple.main-thread', stop reason = Fatal error: Unexpectedly found nil while unwrapping an Optional value
frame #​0: 0x0000000100667830 libswiftCore.dylib`_swift_runtime_on_report
frame #​1: 0x00000001006c73c8 libswiftCore.dylib`_swift_stdlib_reportFatalError + 168
frame #​2: 0x00000001005c7b46 libswiftCore.dylib`function signature specialization <Arg[0] = Exploded, Arg[1] = Exploded> of closure #​2 (Swift.UnsafeBufferPointer<Swift.UInt8>) -> () in Swift._fatalErrorMessage(_: Swift.StaticString, _: Swift.StaticString, file: Swift.StaticString, line: Swift.UInt, flags: Swift.UInt32) -> Swift.Never + 70
frame #​3: 0x00000001005c7ec3 libswiftCore.dylib`function signature specialization <Arg[0] = Exploded, Arg[1] = Exploded, Arg[2] = Dead, Arg[3] = Dead> of Swift._fatalErrorMessage(_: Swift.StaticString, _: Swift.StaticString, file: Swift.StaticString, line: Swift.UInt, flags: Swift.UInt32) -> Swift.Never + 307
frame #​4: 0x00000001003c73b3 libswiftCore.dylib`Swift._fatalErrorMessage(_: Swift.StaticString, _: Swift.StaticString, file: Swift.StaticString, line: Swift.UInt, flags: Swift.UInt32) -> Swift.Never + 19
* frame #​5: 0x00000001000032d4 CastingCrasher`closure #​1 in NSObject<>.observe<A>(change=Foundation.NSKeyValueObservedChange<Swift.Optional<Swift.String>> @ 0x00007ffeefbfe310, onChange=0x00000001000035b0 CastingCrasher`partial apply forwarder for closure #​1 (A1) -> () in (extension in CastingCrasher):__C.NSObject< where A: __C.NSObject>.bind<A, B>(_: Swift.KeyPath<A, A1>, to: B1, at: Swift.ReferenceWritableKeyPath<B1, A1>) -> CastingCrasher.Disposable at <compiler-generated>) at ViewController.swift:33:44
frame #​6: 0x0000000100003795 CastingCrasher`partial apply for closure #​1 in NSObject<>.observe<A>(_:onChange:) at <compiler-generated>:0
frame #​7: 0x000000010000336f CastingCrasher`thunk for @escaping @callee_guaranteed (@guaranteed A, @in_guaranteed NSKeyValueObservedChange<A1>) -> () at <compiler-generated>:0
frame #​8: 0x0000000100003825 CastingCrasher`partial apply for thunk for @escaping @callee_guaranteed (@guaranteed A, @in_guaranteed NSKeyValueObservedChange<A1>) -> () at <compiler-generated>:0
frame #​9: 0x0000000100b7e236 libswiftFoundation.dylib`closure #​1 (__C.NSObject, Foundation.NSKeyValueObservedChange<Any>) -> () in (extension in Foundation):Foundation._KeyValueCodingAndObserving.observe<A>(_: Swift.KeyPath<A, A1>, options: __C.NSKeyValueObservingOptions, changeHandler: (A, Foundation.NSKeyValueObservedChange<A1>) -> ()) -> Foundation.NSKeyValueObservation + 646
frame #​10: 0x0000000100c0fce9 libswiftFoundation.dylib`partial apply forwarder for closure #​1 (__C.NSObject, Foundation.NSKeyValueObservedChange<Any>) -> () in (extension in Foundation):Foundation._KeyValueCodingAndObserving.observe<A>(_: Swift.KeyPath<A, A1>, options: __C.NSKeyValueObservingOptions, changeHandler: (A, Foundation.NSKeyValueObservedChange<A1>) -> ()) -> Foundation.NSKeyValueObservation + 25
frame #​11: 0x0000000100c0fb47 libswiftFoundation.dylib`function signature specialization <Arg[0] = Dead, Arg[3] = Dead> of Foundation.NSKeyValueObservation._swizzle_me_observeValue(forKeyPath: Swift.Optional<Swift.String>, of: Swift.Optional<Any>, change: Swift.Optional<Swift.Dictionary<__C.NSString, Any>>, context: Swift.Optional<Swift.UnsafeMutableRawPointer>) -> () + 2295
frame #​12: 0x0000000100b7d996 libswiftFoundation.dylib`@objc Foundation.NSKeyValueObservation._swizzle_me_observeValue(forKeyPath: Swift.Optional<Swift.String>, of: Swift.Optional<Any>, change: Swift.Optional<Swift.Dictionary<__C.NSString, Any>>, context: Swift.Optional<Swift.UnsafeMutableRawPointer>) -> () + 198
frame #​13: 0x00007fff363c3086 Foundation`NSKeyValueNotifyObserver + 346
frame #​14: 0x00007fff363bc685 Foundation`-[NSObject(NSKeyValueObserverRegistration) _addObserver:forProperty:options:context:] + 255
frame #​15: 0x00007fff363bb69c Foundation`-[NSObject(NSKeyValueObserverRegistration) addObserver:forKeyPath:options:context:] + 103
frame #​16: 0x0000000100b7debb libswiftFoundation.dylib`(extension in Foundation):Foundation._KeyValueCodingAndObserving.observe<A>(_: Swift.KeyPath<A, A1>, options: __C.NSKeyValueObservingOptions, changeHandler: (A, Foundation.NSKeyValueObservedChange<A1>) -> ()) -> Foundation.NSKeyValueObservation + 811
frame #​17: 0x0000000100002efc CastingCrasher`NSObject<>.observe<A>(keyPath=0x0000600001757340, onChange=0x00000001000035b0 CastingCrasher`partial apply forwarder for closure #​1 (A1) -> () in (extension in CastingCrasher):__C.NSObject< where A: __C.NSObject>.bind<A, B>(_: Swift.KeyPath<A, A1>, to: B1, at: Swift.ReferenceWritableKeyPath<B1, A1>) -> CastingCrasher.Disposable at <compiler-generated>, self=0x0000600003007de0) at ViewController.swift:31:27
frame #​18: 0x0000000100002406 CastingCrasher`NSObject<>.bind<A, B>(sourceKeyPath=0x0000600001757340, target=0x0000600003007de0, targetKeyPath=0x0000600001757540, self=0x0000600003007de0) at ViewController.swift:40:16
frame #​19: 0x0000000100001ff3 CastingCrasher`ViewController.viewDidLoad(self=0x0000600003007de0) at ViewController.swift:22:13
frame #​20: 0x0000000100002454 CastingCrasher`@objc ViewController.viewDidLoad() at <compiler-generated>:0
frame #​21: 0x00007fff31737b5a AppKit`-[NSViewController _sendViewDidLoad] + 97
frame #​22: 0x00007fff31736c9b AppKit`-[NSViewController _loadViewIfRequired] + 390
frame #​23: 0x00007fff31736acb AppKit`-[NSViewController view] + 30
frame #​24: 0x00007fff318291da AppKit`-[NSWindow _contentViewControllerChanged] + 109
frame #​25: 0x00007fff36420450 Foundation`-[NSObject(NSKeyValueCoding) setValue:forKey:] + 331
frame #​26: 0x00007fff31733987 AppKit`-[NSWindow setValue:forKey:] + 111
frame #​27: 0x00007fff31685e0c AppKit`-[NSIBUserDefinedRuntimeAttributesConnector establishConnection] + 637
frame #​28: 0x00007fff315a6739 AppKit`-[NSIBObjectData nibInstantiateWithOwner:options:topLevelObjects:] + 1472
frame #​29: 0x00007fff3173c114 AppKit`-[NSNib _instantiateNibWithExternalNameTable:options:] + 679
frame #​30: 0x00007fff3173bd70 AppKit`-[NSNib _instantiateWithOwner:options:topLevelObjects:] + 136
frame #​31: 0x00007fff31e3364c AppKit`-[NSStoryboard instantiateControllerWithIdentifier:] + 236
frame #​32: 0x00007fff31597855 AppKit`NSApplicationMain + 702
frame #​33: 0x000000010000457d CastingCrasher`main at AppDelegate.swift:12:7
frame #​34: 0x00007fff612afed9 libdyld.dylib`start + 1