0

Using a ReferenceWritaleKeyPath to read a property is causing a compiler segmentation fault.

I am setting up a helper to simplify binding two variables. With help from OOPer I got the basic binding to work, but if modify the code as shown below to check the values are different before doing the binding it has a segmentation fault.

protocol Bindable: class {
    var observers: [NSKeyValueObservation] {get set}
}

extension Bindable {
    func bind<Value>(to targetKeyPath: ReferenceWritableKeyPath<Self, Value>, from sourceKeyPath: KeyPath<Self, Value>)
        where Self: NSObject {
        self.observers.append( self.observe(sourceKeyPath, options: [.initial, .new]) {object, change in

            // FAILS: compiler failed due to signal: Segmentation fault: 11
            if( self[keyPath:targetKeyPath] != change.newValue ) {  
                self[keyPath: targetKeyPath] = change.newValue!
            }
        })
    }
}
Anh Pham
  • 2,108
  • 9
  • 18
  • 29
Jim Leask
  • 6,159
  • 5
  • 21
  • 31

1 Answers1

1

The problem is that you attempt to use != with the generic type Value, which does not necessarily have == and != implementations. Replacing <Value> by <Value: Equatable> solves it.

Having said that, the compiler crashing with a segmentation fault is always a bug, regardless of whether your code is correct or not. You should consider filing a bug report at https://bugs.swift.org if you have time.

Tim Vermeulen
  • 12,352
  • 9
  • 44
  • 63
  • 1
    I had tried but String isn't a Protocol or Class so that didn't work. Equatable is what I needed. I filed a bug for the segmentation fault as suggested: SR-5375 – Jim Leask Jul 05 '17 at 15:21