-
Notifications
You must be signed in to change notification settings - Fork 13.4k
Interior mutability can cause inconsistency in collections #23327
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
This is a dupe of #13438, which was previously closed for reasons in the thread itself. I believe the gist of the conversion was that yes, this can happen. Our implementations should ensure that this does not cause memory unsafety, but otherwise these sorts of "odd assertions" are ok to happen as this should be quite a rare occurrence anyway. |
I still think the collection documentation needs to identify this problem. On Friday, March 13, 2015, Alex Crichton [email protected] wrote:
|
Hm good point! I've added an appropriate tag. |
@alexcrichton I'm working on a patch to document this. |
Collections that perform lookups based on a given property (e.g. a hash or an ordering) can be put into an inconsistent state when the property changes while its corresponding item is in the collection. This is normally not a problem, because such collections (at least those in
std
) do not provide mutable access to the type from which the property is derived (keys in the case of maps, items in the case of sets), but types with interior mutability can induce this behavior in safe code:Output:
This is obvious from the definition of interior mutability, but it's definitely a footgun. If the use of such keys cannot be prevented with the type system, the collections should at least document that the property must remain constant for the duration of the item's presence in the collection.
For example, Java's
Map
interface documentation contains the following note:The text was updated successfully, but these errors were encountered: