Skip to content

Commit 48c4887

Browse files
committed
recommend using a HashMap if a HashSet's second generic parameter doesn't implement BuildHasher
1 parent bbcbc78 commit 48c4887

File tree

5 files changed

+59
-10
lines changed

5 files changed

+59
-10
lines changed

compiler/rustc_hir_typeck/src/method/suggest.rs

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2925,7 +2925,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
29252925
.filter_map(|e| match e.obligation.predicate.kind().skip_binder() {
29262926
ty::PredicateKind::Clause(ty::ClauseKind::Trait(pred)) => {
29272927
match pred.self_ty().kind() {
2928-
ty::Adt(_, _) => Some(pred),
2928+
ty::Adt(_, _) => Some((e.root_obligation.predicate, pred)),
29292929
_ => None,
29302930
}
29312931
}
@@ -2935,18 +2935,18 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
29352935

29362936
// Note for local items and foreign items respectively.
29372937
let (mut local_preds, mut foreign_preds): (Vec<_>, Vec<_>) =
2938-
preds.iter().partition(|&pred| {
2938+
preds.iter().partition(|&(_, pred)| {
29392939
if let ty::Adt(def, _) = pred.self_ty().kind() {
29402940
def.did().is_local()
29412941
} else {
29422942
false
29432943
}
29442944
});
29452945

2946-
local_preds.sort_by_key(|pred: &&ty::TraitPredicate<'_>| pred.trait_ref.to_string());
2946+
local_preds.sort_by_key(|(_, pred)| pred.trait_ref.to_string());
29472947
let local_def_ids = local_preds
29482948
.iter()
2949-
.filter_map(|pred| match pred.self_ty().kind() {
2949+
.filter_map(|(_, pred)| match pred.self_ty().kind() {
29502950
ty::Adt(def, _) => Some(def.did()),
29512951
_ => None,
29522952
})
@@ -2959,7 +2959,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
29592959
})
29602960
.collect::<Vec<_>>()
29612961
.into();
2962-
for pred in &local_preds {
2962+
for (_, pred) in &local_preds {
29632963
if let ty::Adt(def, _) = pred.self_ty().kind() {
29642964
local_spans.push_span_label(
29652965
self.tcx.def_span(def.did()),
@@ -2968,7 +2968,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
29682968
}
29692969
}
29702970
if local_spans.primary_span().is_some() {
2971-
let msg = if let [local_pred] = local_preds.as_slice() {
2971+
let msg = if let [(_, local_pred)] = local_preds.as_slice() {
29722972
format!(
29732973
"an implementation of `{}` might be missing for `{}`",
29742974
local_pred.trait_ref.print_trait_sugared(),
@@ -2986,10 +2986,10 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
29862986
err.span_note(local_spans, msg);
29872987
}
29882988

2989-
foreign_preds.sort_by_key(|pred: &&ty::TraitPredicate<'_>| pred.trait_ref.to_string());
2989+
foreign_preds.sort_by_key(|(_, pred)| pred.trait_ref.to_string());
29902990
let foreign_def_ids = foreign_preds
29912991
.iter()
2992-
.filter_map(|pred| match pred.self_ty().kind() {
2992+
.filter_map(|(_, pred)| match pred.self_ty().kind() {
29932993
ty::Adt(def, _) => Some(def.did()),
29942994
_ => None,
29952995
})
@@ -3002,7 +3002,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
30023002
})
30033003
.collect::<Vec<_>>()
30043004
.into();
3005-
for pred in &foreign_preds {
3005+
for (_, pred) in &foreign_preds {
30063006
if let ty::Adt(def, _) = pred.self_ty().kind() {
30073007
foreign_spans.push_span_label(
30083008
self.tcx.def_span(def.did()),
@@ -3011,7 +3011,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
30113011
}
30123012
}
30133013
if foreign_spans.primary_span().is_some() {
3014-
let msg = if let [foreign_pred] = foreign_preds.as_slice() {
3014+
let msg = if let [(_, foreign_pred)] = foreign_preds.as_slice() {
30153015
format!(
30163016
"the foreign item type `{}` doesn't implement `{}`",
30173017
foreign_pred.self_ty(),
@@ -3027,6 +3027,21 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
30273027
)
30283028
};
30293029
err.span_note(foreign_spans, msg);
3030+
3031+
if let Some(span) =
3032+
foreign_preds.iter().find_map(|(o, p)| match o.kind().skip_binder() {
3033+
ty::PredicateKind::Clause(ty::ClauseKind::Trait(pred))
3034+
if let Some(a) = pred.self_ty().ty_adt_def()
3035+
&& self.tcx.is_diagnostic_item(sym::HashSet, a.did())
3036+
&& self.tcx.is_diagnostic_item(sym::BuildHasher, p.def_id()) =>
3037+
{
3038+
Some(self.tcx.def_span(p.def_id()))
3039+
}
3040+
_ => None,
3041+
})
3042+
{
3043+
err.span_help(span, "you might have intended to use a HashMap instead");
3044+
}
30303045
}
30313046

30323047
let preds: Vec<_> = errors

compiler/rustc_span/src/symbol.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,7 @@ symbols! {
191191
Borrow,
192192
BorrowMut,
193193
Break,
194+
BuildHasher,
194195
C,
195196
CStr,
196197
C_dash_unwind: "C-unwind",

library/core/src/hash/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -633,6 +633,7 @@ impl<H: Hasher + ?Sized> Hasher for &mut H {
633633
///
634634
/// [`build_hasher`]: BuildHasher::build_hasher
635635
/// [`HashMap`]: ../../std/collections/struct.HashMap.html
636+
#[cfg_attr(not(test), rustc_diagnostic_item = "BuildHasher")]
636637
#[stable(since = "1.7.0", feature = "build_hasher")]
637638
pub trait BuildHasher {
638639
/// Type of the hasher that will be created.
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
use std::collections::HashSet;
2+
3+
#[derive(PartialEq)]
4+
//~^ NOTE in this expansion of
5+
//~| NOTE in this expansion of
6+
//~| NOTE in this expansion of
7+
pub struct MyStruct {
8+
pub parameters: HashSet<String, String>,
9+
//~^ NOTE the foreign item type
10+
//~| ERROR binary operation
11+
}
12+
13+
fn main() {}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
error[E0369]: binary operation `==` cannot be applied to type `HashSet<String, String>`
2+
--> $DIR/hashset_generics.rs:8:5
3+
|
4+
LL | #[derive(PartialEq)]
5+
| --------- in this derive macro expansion
6+
...
7+
LL | pub parameters: HashSet<String, String>,
8+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
9+
|
10+
note: the foreign item type `String` doesn't implement `BuildHasher`
11+
--> $SRC_DIR/alloc/src/string.rs:LL:COL
12+
|
13+
= note: not implement `BuildHasher`
14+
help: you might have intended to use a HashMap instead
15+
--> $SRC_DIR/core/src/hash/mod.rs:LL:COL
16+
17+
error: aborting due to 1 previous error
18+
19+
For more information about this error, try `rustc --explain E0369`.

0 commit comments

Comments
 (0)