pharo-project / pharo

Pharo is a dynamic reflective pure object-oriented language supporting live programming inspired by Smalltalk.
http://pharo.org
Other
1.21k stars 356 forks source link

Dictionary Associations can be directly modified, leading to bizarre KeyNotFound errors #13353

Open sdasda7777 opened 1 year ago

sdasda7777 commented 1 year ago

Bug description I was seriously confused for the last two hours or so, because every time I did dict at: string on my Dictionaries, I would get a KeyNotFound error. However I could see the key was in there, and even dict keys includes: string would come back as True. Following are pictures that show the two strings byte per byte.

image

image

The reason was that I was doing dict associationsDo: [ :pair | pair key: (fun (pair key))], which changed the key, but I assume it didn't change where it was in the underlying tree structure, or something like that.

In short, I don't think keys should be exposed to modification in this way. I would have never figured it out if I didn't have some knowlege of how Dictionaries usually work, which I think common person doesn't. If you need to know how abstraction works under the hood to use it properly, that's a bad abstraction.

To Reproduce Create a dictionary, iterate associations, directly modify some keys. The tree will be built on outdated information, and you will only find anything if you're very lucky.

welcome[bot] commented 1 year ago

Thanks for opening your first issue! Please check the CONTRIBUTING documents for some tips about which information should be provided. You can find information of how to do a Pull Request here: https://github.com/pharo-project/pharo/wiki/Contribute-a-fix-to-Pharo

GitHub
Contribute a fix to Pharo
Pharo is a dynamic reflective pure object-oriented language supporting live programming inspired by Smalltalk. - pharo-project/pharo
svenvc commented 1 year ago

Hi,

Thanks for the feedback.

You are correct but it has always been like that and it unlikely to change soon.

Yes, associations are part of the internal structure of the dictionary and should never be changed in place.

Maybe the key could be made readonly once the association is created, I am not sure that is possible at that granularity.

Sven