Uh oh!
There was an error while loading. Please reload this page.
recommend using a HashMap if a HashSet's second generic parameter doesn't implement BuildHasher - #147171
Conversation
rustbot
commented
Sep 30, 2025
r? @fee1-dead rustbot has assigned @fee1-dead. Use |
This comment has been minimized.
This comment has been minimized.
| note: the foreign item type `String` doesn't implement `BuildHasher` | ||
| --> $SRC_DIR/alloc/src/string.rs:LL:COL | ||
| | | ||
| = note: not implement `BuildHasher` |
There was a problem hiding this comment.
note to self, this probably needs improvement.
There was a problem hiding this comment.
Hmm. Could there be other tests that showcase when this help message could appear? For example when I try to insert, or when I try to create a new value of the HashSet type.
Also I think it might be better to put this under the less crowded tests/ui/hashmap directory.
fee1-dead
commented
Oct 7, 2025
Still waiting on other tests. Also, use |
Qelxiros
commented
Oct 15, 2025
I've added a test for the insert case. It seems pretty representative of most instances of this error, since the HashSet bounds are on methods. If we want to lint (not error) on HashSet construction with a second parameter that doesn't impl BuildHasher (and I'm not convinced we do), that's a separate conversation. @rustbot ready |
| help: you might have intended to use a HashMap instead | ||
| --> $DIR/hashset_generics.rs:8:5 | ||
| | | ||
| LL | #[derive(PartialEq)] | ||
| | --------- in this derive macro expansion | ||
| ... | ||
| LL | pub parameters: HashSet<String, String>, | ||
| | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ |
There was a problem hiding this comment.
Can we just use err.help() to avoid highlighting the same thing again?
bors
commented
Oct 22, 2025
☔ The latest upstream changes (presumably #147957) made this pull request unmergeable. Please resolve the merge conflicts. |
rustbot
commented
Oct 30, 2025
This PR was rebased onto a different master commit. Here's a range-diff highlighting what actually changed. Rebasing is a normal part of keeping PRs up to date, so no action is needed—this note is just to help reviewers. |
Qelxiros
commented
Oct 30, 2025
@rustbot ready |
Uh oh!
There was an error while loading. Please reload this page.
Qelxiros
commented
Nov 5, 2025
@rustbot ready |
| if foreign_preds.iter().any(|&(root_pred, pred)| { | ||
| if let ty::PredicateKind::Clause(ty::ClauseKind::Trait(root_pred)) = | ||
| root_pred.kind().skip_binder() | ||
| && let Some(root_adt) = root_pred.self_ty().ty_adt_def() | ||
| { | ||
| self.tcx.is_diagnostic_item(sym::HashSet, root_adt.did()) | ||
| && self.tcx.is_diagnostic_item(sym::BuildHasher, pred.def_id()) | ||
| } else { | ||
| false | ||
| } | ||
| }) { | ||
| err.help("you might have intended to use a HashMap instead"); | ||
| } |
There was a problem hiding this comment.
Might be nice to extract this to a helper function that deduplicates the logic between this one and the one above, maybe named something like "suggest_hashmap_on_unsatisfied_hashset_buildhasher"
…sn't implement BuildHasher
Qelxiros
commented
Nov 18, 2025
@rustbot ready |
fee1-dead
commented
Nov 18, 2025
@bors r+ rollup |
bors
commented
Nov 18, 2025
Rollup of 7 pull requests Successful merges: - #147171 (recommend using a HashMap if a HashSet's second generic parameter doesn't implement BuildHasher) - #147421 (Add check if span is from macro expansion) - #147521 (Make SIMD intrinsics available in `const`-contexts) - #148201 (Start documenting autodiff activities) - #148797 (feat: Add `bit_width` for unsigned `NonZero<T>`) - #148798 (Match <OsString as Debug>::fmt to that of str) - #149082 (autodiff: update formating, improve examples for the unstable-book) r? `@ghost` `@rustbot` modify labels: rollup
Uh oh!
There was an error while loading. Please reload this page.
Rollup merge of #147171 - Qelxiros:hashmap_diag, r=fee1-dead recommend using a HashMap if a HashSet's second generic parameter doesn't implement BuildHasher closes#147147 ~The suggestion span is wrong, but I'm not sure how to find the right one.~ fixed I'm relatively new to the diagnostics ecosystem, so I'm not sure if `span_help` is the right choice. `span_suggestion_*` might be better, but the output from `x test` looks weird in that case.
Rollup of 7 pull requests Successful merges: - rust-lang/rust#147171 (recommend using a HashMap if a HashSet's second generic parameter doesn't implement BuildHasher) - rust-lang/rust#147421 (Add check if span is from macro expansion) - rust-lang/rust#147521 (Make SIMD intrinsics available in `const`-contexts) - rust-lang/rust#148201 (Start documenting autodiff activities) - rust-lang/rust#148797 (feat: Add `bit_width` for unsigned `NonZero<T>`) - rust-lang/rust#148798 (Match <OsString as Debug>::fmt to that of str) - rust-lang/rust#149082 (autodiff: update formating, improve examples for the unstable-book) r? `@ghost` `@rustbot` modify labels: rollup
recommend using a HashMap if a HashSet's second generic parameter doesn't implement BuildHasher closesrust-lang#147147 ~The suggestion span is wrong, but I'm not sure how to find the right one.~ fixed I'm relatively new to the diagnostics ecosystem, so I'm not sure if `span_help` is the right choice. `span_suggestion_*` might be better, but the output from `x test` looks weird in that case.
closes#147147
The suggestion span is wrong, but I'm not sure how to find the right one.fixedI'm relatively new to the diagnostics ecosystem, so I'm not sure if
span_helpis the right choice.span_suggestion_*might be better, but the output fromx testlooks weird in that case.