-
Notifications
You must be signed in to change notification settings - Fork 12.9k
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
The clear method in std::oldmap::HashMap now takes &mut self - and this leads to illegal borrow unless pure errors #4870
Comments
Hopefully this clarifies why I think HashMap.clear taking This is a program that, before the change, compiled and ran:
Since the change, rustc now complains due to the illegal borrow it contains calling ms.map.clear():
I tried to fix the issue by making map a
I can't see any way to avoid both illegal borrow errors at the same time. Does this make sense to anyone else? @thestinger ? |
@Dretch: In order to mutate an object, the owner needs to be a mutable variable or The use case for mutable fields is hidden internal state that cannot be observed externally like locks. Using them the way The methods that don't use explicit self are unsound and should be updated, but it's a lot of work to update the calling code. As a whole |
If I understand you correctly, then in future idiomatic rust, methods will have to expose in their type signature whether or not they mutate the externally-observable-internal-state of the self value? |
@Dretch: They will always be able to use unsafe to break the rules like that. I can't think of very many actual use cases though other than locks - which already have to use unsafe code. |
Closing since oldmap is going to be removed. If I misunderstood something, please let me know! |
docs: add msvc note to manual Added note for Windows users to have the latest MSVC to minimize setup issues. Closes rust-lang#4870
std::oldmap::HashMap now takes &mut self:
I am finding this rather awkward because HashMap also has methods like insert and remove that do not take mutable self, but are also non-pure. What this means is that I can't make my field of type std::oldmap::HashMap mut, because then rustc complains about
illegal borrow unless pure
when calling insert/remove, and I can't make it non-mut because then I can't call clear on it...Does it make sense for HashMap to implement Mutable? AFAICT HashMap doesn't need mutable self because it mutates by changing its internal fields, not by creating a new instance of itself.
The text was updated successfully, but these errors were encountered: