-
Notifications
You must be signed in to change notification settings - Fork 161
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
OccupiedEntry::key() gives the new key instead of the old one #169
Comments
Hmm, it's been that way forever (638ec3b), but I agree it would be better to follow I worry whether such a behavior change should be considered breaking though... |
Makes sense to change this. Did this cause a bug on your end or something (so that it's actually something some users would notice?) |
It is something that can have an observable effect, which did cause a bug for me in my case. I have these types: // something with source code location attached
pub struct Spanned<T> {
span: Span, // not included in Eq/PartialEq/Hash. Used in diagnostics
value: T,
}
pub enum Value {
Array(Vec<Spanned<Value>>),
Object(IndexMap<Spanned<Ident>, Spanned<Value>>),
// other variants...
} When deserializing the let new_span = key.span;
match map.entry(key) {
Entry::Vacant(entry) => { entry.insert(value); }
Entry::Occupied(entry) => {
// create an error that points to new_span as a duplicate,
// and entry.key().span as the original definition...
}
} and so with the current behavior my error messages had both spans pointing to the same thing. But that also of course means that this would in some way be a breaking change. I'm not sure what the case would be for having it return the new key (at least in borrowed form; I do see a use for recovering ownership of the new key), but I can't deny that there may be code somewhere out there depending on it. |
I think it's a sort of grey area where we can argue that this is a bug fix, since the intent has been to follow the API of the standard One argument with an eye toward the future -- |
output:
Whereas if you instead
use std::collections::hash_map::{HashMap as Map, Entry};
then it printsold
. This difference in behavior seems surprising.The text was updated successfully, but these errors were encountered: