Description
The iteration order of a HashMap
depends on the Hash
value of the keys. In rustc, the Hash
value of a key can change between compilation sessions, even if the HashStable
value remains the same (e.g. DefId
, Symbol
, etc.). This means that iterating over a HashMap
can cause a query to produce different results given the same (as determined by HashStable
) input. This can be quite subtle, as demonstrated by #82272 (comment)
To eliminate this source of issues, we should make an internal rustc-only lint for iterating over a HashMap
in any way. This will need to cover explicit method calls (e.g. .keys()
and .values()
), as well as any usage of the IntoIterator
impl for HashMap
.
Note that we don't want to ban the usage of HashMap
s with unstable keys - as long as they're only used for lookup (and not iteration), it's much more efficient than repeatedly converting to the stable form of a key (e.g. DefId
-> DefPathHash
or Symbol
-> String
).