jnermut
(Jon Nermut)
June 15, 2017, 1:50am
1
In trying to replicate another issue I found that this code crashes the compiler:
open class SynchronizedDictionary
{
var dict: Dictionary
public init()
{
self.dict = Dictionary()
}
// many wrappers ommitted
open func getAndUpdate( forKey key: Key, _ updateFunc: @escaping (Value?) -> (Value?) ) -> Value?
{
// locking omitted
let current = dict[key]
let newVal = updateFunc(current)
dict[key] = newVal
return newVal
}
}
open class Subscription
{
var msg = "hello"
}
let m = SynchronizedDictionary<Int64, Subscription>()
print(m.dict[0])
m.getAndUpdate(forKey: 0)
{
currentVal in
return Subscription()
}
print(m.dict[0])
In Fire 2151 Gives
: error E0: Internal error: Object reference not set to an instance of an object
jnermut
(Jon Nermut)
June 15, 2017, 2:02am
2
So if you change the getAndUpdate func to this, you can replace the NRE with a type error:
This is incorrect behaviour as assigning nil to a dictionary should remove the value from the dictionary:
‘If you assign nil as the value for the given key, the dictionary removes that key and its associated value.’
jnermut
(Jon Nermut)
June 15, 2017, 2:11am
3
I just found that I got different results and the NRE went away if I typed dict as Swift.Dictionary<Key,Value> instead of Dictionary<Key,Value> - that was kind of unexpected…
Something to do with the new RTL?
Thanks, logged as bugs://77988
ck
(Carlo Kok)
June 16, 2017, 6:59am
5
Hi,
I can’t reproduce this with latest beta, tried on .NET Java and Cocoa.
bugs://77988 got closed with status cannotrepro.