HashSet<K, V> has confusing errors thanks to lax BuildHasher bounds #123018
Labels
A-diagnostics
Area: Messages for errors, warnings, and lints
T-compiler
Relevant to the compiler team, which will review and decide on the PR/issue.
Code
Writing
HashSet
instead ofHashMap
does not cause immediate error, becauseHashSet
is allowed to have two type arguments, and very few methods require it to have a specific type.Current output
playground
Desired output
Ideally Rust should check early that the second type in
HashSet
is supposed to be aBuildHasher
, and warn about that as soon asHashSet<_, InvalidType>
is used.Rationale and extra context
Lack of bound on
HashSet
itself + type inference make incorrect the type compile in many cases, producing either surprising behavior or confusing errors far away from the mistake.Other cases
Similar to #87015
It's not possible to add actual trait bounds on
HashSet
, but maybe the compiler could be somehow informed that these bounds are always expected to be satisfied, and warn whenever they're not.Rust Version
rustc 1.79.0-nightly (3c85e56 2024-03-18)
The text was updated successfully, but these errors were encountered: