swiftlang / swift-corelibs-foundation

The Foundation Project, providing core utilities, internationalization, and OS independence
swift.org
Apache License 2.0
5.29k stars 1.14k forks source link

[SR-10058] Swift 5 compiler regression runtime crash when using 4.2 mode #3518

Open tkrajacic opened 5 years ago

tkrajacic commented 5 years ago
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 #&#8203;1, queue = 'com.apple.main-thread', stop reason = Fatal error: Unexpectedly found nil while unwrapping an Optional value
    frame #&#8203;0: 0x0000000100667830 libswiftCore.dylib`_swift_runtime_on_report
    frame #&#8203;1: 0x00000001006c73c8 libswiftCore.dylib`_swift_stdlib_reportFatalError + 168
    frame #&#8203;2: 0x00000001005c7b46 libswiftCore.dylib`function signature specialization <Arg[0] = Exploded, Arg[1] = Exploded> of closure #&#8203;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 #&#8203;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 #&#8203;4: 0x00000001003c73b3 libswiftCore.dylib`Swift._fatalErrorMessage(_: Swift.StaticString, _: Swift.StaticString, file: Swift.StaticString, line: Swift.UInt, flags: Swift.UInt32) -> Swift.Never + 19
  * frame #&#8203;5: 0x00000001000032d4 CastingCrasher`closure #&#8203;1 in NSObject<>.observe<A>(change=Foundation.NSKeyValueObservedChange<Swift.Optional<Swift.String>> @ 0x00007ffeefbfe310, onChange=0x00000001000035b0 CastingCrasher`partial apply forwarder for closure #&#8203;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 #&#8203;6: 0x0000000100003795 CastingCrasher`partial apply for closure #&#8203;1 in NSObject<>.observe<A>(_:onChange:) at <compiler-generated>:0
    frame #&#8203;7: 0x000000010000336f CastingCrasher`thunk for @escaping @callee_guaranteed (@guaranteed A, @in_guaranteed NSKeyValueObservedChange<A1>) -> () at <compiler-generated>:0
    frame #&#8203;8: 0x0000000100003825 CastingCrasher`partial apply for thunk for @escaping @callee_guaranteed (@guaranteed A, @in_guaranteed NSKeyValueObservedChange<A1>) -> () at <compiler-generated>:0
    frame #&#8203;9: 0x0000000100b7e236 libswiftFoundation.dylib`closure #&#8203;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 #&#8203;10: 0x0000000100c0fce9 libswiftFoundation.dylib`partial apply forwarder for closure #&#8203;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 #&#8203;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 #&#8203;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 #&#8203;13: 0x00007fff363c3086 Foundation`NSKeyValueNotifyObserver + 346
    frame #&#8203;14: 0x00007fff363bc685 Foundation`-[NSObject(NSKeyValueObserverRegistration) _addObserver:forProperty:options:context:] + 255
    frame #&#8203;15: 0x00007fff363bb69c Foundation`-[NSObject(NSKeyValueObserverRegistration) addObserver:forKeyPath:options:context:] + 103
    frame #&#8203;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 #&#8203;17: 0x0000000100002efc CastingCrasher`NSObject<>.observe<A>(keyPath=0x0000600001757340, onChange=0x00000001000035b0 CastingCrasher`partial apply forwarder for closure #&#8203;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 #&#8203;18: 0x0000000100002406 CastingCrasher`NSObject<>.bind<A, B>(sourceKeyPath=0x0000600001757340, target=0x0000600003007de0, targetKeyPath=0x0000600001757540, self=0x0000600003007de0) at ViewController.swift:40:16
    frame #&#8203;19: 0x0000000100001ff3 CastingCrasher`ViewController.viewDidLoad(self=0x0000600003007de0) at ViewController.swift:22:13
    frame #&#8203;20: 0x0000000100002454 CastingCrasher`@objc ViewController.viewDidLoad() at <compiler-generated>:0
    frame #&#8203;21: 0x00007fff31737b5a AppKit`-[NSViewController _sendViewDidLoad] + 97
    frame #&#8203;22: 0x00007fff31736c9b AppKit`-[NSViewController _loadViewIfRequired] + 390
    frame #&#8203;23: 0x00007fff31736acb AppKit`-[NSViewController view] + 30
    frame #&#8203;24: 0x00007fff318291da AppKit`-[NSWindow _contentViewControllerChanged] + 109
    frame #&#8203;25: 0x00007fff36420450 Foundation`-[NSObject(NSKeyValueCoding) setValue:forKey:] + 331
    frame #&#8203;26: 0x00007fff31733987 AppKit`-[NSWindow setValue:forKey:] + 111
    frame #&#8203;27: 0x00007fff31685e0c AppKit`-[NSIBUserDefinedRuntimeAttributesConnector establishConnection] + 637
    frame #&#8203;28: 0x00007fff315a6739 AppKit`-[NSIBObjectData nibInstantiateWithOwner:options:topLevelObjects:] + 1472
    frame #&#8203;29: 0x00007fff3173c114 AppKit`-[NSNib _instantiateNibWithExternalNameTable:options:] + 679
    frame #&#8203;30: 0x00007fff3173bd70 AppKit`-[NSNib _instantiateWithOwner:options:topLevelObjects:] + 136
    frame #&#8203;31: 0x00007fff31e3364c AppKit`-[NSStoryboard instantiateControllerWithIdentifier:] + 236
    frame #&#8203;32: 0x00007fff31597855 AppKit`NSApplicationMain + 702
    frame #&#8203;33: 0x000000010000457d CastingCrasher`main at AppDelegate.swift:12:7
    frame #&#8203;34: 0x00007fff612afed9 libdyld.dylib`start + 1
bc7072e8-7d37-4a38-8fee-cab463d443b7 commented 5 years ago

+@millenomi

tkrajacic commented 5 years ago

This still crashes in the released version of Swift 5 (tested with Xcode 10.2)

tkrajacic commented 4 years ago

What's vicious about this is, that this seems not to crash any more under 10.15, but when you deploy the app to 10.14 it crashes.

Compiled and tested with Xcode 12