Skip to content

Commit 65afb13

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

File tree

5 files changed

+72
-10
lines changed

5 files changed

+72
-10
lines changed

compiler/rustc_hir_typeck/src/method/suggest.rs

Lines changed: 30 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2925,7 +2925,10 @@ 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((
2929+
(e.root_obligation.predicate, e.root_obligation.cause.span),
2930+
pred,
2931+
)),
29292932
_ => None,
29302933
}
29312934
}
@@ -2935,18 +2938,18 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
29352938

29362939
// Note for local items and foreign items respectively.
29372940
let (mut local_preds, mut foreign_preds): (Vec<_>, Vec<_>) =
2938-
preds.iter().partition(|&pred| {
2941+
preds.iter().partition(|&(_, pred)| {
29392942
if let ty::Adt(def, _) = pred.self_ty().kind() {
29402943
def.did().is_local()
29412944
} else {
29422945
false
29432946
}
29442947
});
29452948

2946-
local_preds.sort_by_key(|pred: &&ty::TraitPredicate<'_>| pred.trait_ref.to_string());
2949+
local_preds.sort_by_key(|(_, pred)| pred.trait_ref.to_string());
29472950
let local_def_ids = local_preds
29482951
.iter()
2949-
.filter_map(|pred| match pred.self_ty().kind() {
2952+
.filter_map(|(_, pred)| match pred.self_ty().kind() {
29502953
ty::Adt(def, _) => Some(def.did()),
29512954
_ => None,
29522955
})
@@ -2959,7 +2962,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
29592962
})
29602963
.collect::<Vec<_>>()
29612964
.into();
2962-
for pred in &local_preds {
2965+
for (_, pred) in &local_preds {
29632966
if let ty::Adt(def, _) = pred.self_ty().kind() {
29642967
local_spans.push_span_label(
29652968
self.tcx.def_span(def.did()),
@@ -2968,7 +2971,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
29682971
}
29692972
}
29702973
if local_spans.primary_span().is_some() {
2971-
let msg = if let [local_pred] = local_preds.as_slice() {
2974+
let msg = if let [(_, local_pred)] = local_preds.as_slice() {
29722975
format!(
29732976
"an implementation of `{}` might be missing for `{}`",
29742977
local_pred.trait_ref.print_trait_sugared(),
@@ -2986,10 +2989,10 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
29862989
err.span_note(local_spans, msg);
29872990
}
29882991

2989-
foreign_preds.sort_by_key(|pred: &&ty::TraitPredicate<'_>| pred.trait_ref.to_string());
2992+
foreign_preds.sort_by_key(|(_, pred)| pred.trait_ref.to_string());
29902993
let foreign_def_ids = foreign_preds
29912994
.iter()
2992-
.filter_map(|pred| match pred.self_ty().kind() {
2995+
.filter_map(|(_, pred)| match pred.self_ty().kind() {
29932996
ty::Adt(def, _) => Some(def.did()),
29942997
_ => None,
29952998
})
@@ -3002,7 +3005,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
30023005
})
30033006
.collect::<Vec<_>>()
30043007
.into();
3005-
for pred in &foreign_preds {
3008+
for (_, pred) in &foreign_preds {
30063009
if let ty::Adt(def, _) = pred.self_ty().kind() {
30073010
foreign_spans.push_span_label(
30083011
self.tcx.def_span(def.did()),
@@ -3011,7 +3014,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
30113014
}
30123015
}
30133016
if foreign_spans.primary_span().is_some() {
3014-
let msg = if let [foreign_pred] = foreign_preds.as_slice() {
3017+
let msg = if let [(_, foreign_pred)] = foreign_preds.as_slice() {
30153018
format!(
30163019
"the foreign item type `{}` doesn't implement `{}`",
30173020
foreign_pred.self_ty(),
@@ -3027,6 +3030,23 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
30273030
)
30283031
};
30293032
err.span_note(foreign_spans, msg);
3033+
3034+
tracing::warn!("{foreign_preds:?}");
3035+
if let Some(span) = foreign_preds.iter().find_map(|&((root_pred, span), pred)| {
3036+
tracing::warn!("{pred:?}");
3037+
match root_pred.kind().skip_binder() {
3038+
ty::PredicateKind::Clause(ty::ClauseKind::Trait(root_pred))
3039+
if let Some(root_adt) = root_pred.self_ty().ty_adt_def()
3040+
&& self.tcx.is_diagnostic_item(sym::HashSet, root_adt.did())
3041+
&& self.tcx.is_diagnostic_item(sym::BuildHasher, pred.def_id()) =>
3042+
{
3043+
Some(span)
3044+
}
3045+
_ => None,
3046+
}
3047+
}) {
3048+
err.span_help(span, "you might have intended to use a HashMap instead");
3049+
}
30303050
}
30313051

30323052
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: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
WARN rustc_hir_typeck::method::suggest [((Binder { value: TraitPredicate(<std::collections::HashSet<std::string::String, std::string::String> as std::cmp::PartialEq>, polarity:Positive), bound_vars: [] }, $DIR/hashset_generics.rs:8:5: 8:44 (#4)), TraitPredicate(<std::string::String as std::hash::BuildHasher>, polarity:Positive))]
2+
WARN rustc_hir_typeck::method::suggest TraitPredicate(<std::string::String as std::hash::BuildHasher>, polarity:Positive)
3+
error[E0369]: binary operation `==` cannot be applied to type `HashSet<String, String>`
4+
--> $DIR/hashset_generics.rs:8:5
5+
|
6+
LL | #[derive(PartialEq)]
7+
| --------- in this derive macro expansion
8+
...
9+
LL | pub parameters: HashSet<String, String>,
10+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
11+
|
12+
note: the foreign item type `String` doesn't implement `BuildHasher`
13+
--> $SRC_DIR/alloc/src/string.rs:LL:COL
14+
|
15+
= note: not implement `BuildHasher`
16+
help: you might have intended to use a HashMap instead
17+
--> $DIR/hashset_generics.rs:8:5
18+
|
19+
LL | #[derive(PartialEq)]
20+
| --------- in this derive macro expansion
21+
...
22+
LL | pub parameters: HashSet<String, String>,
23+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
24+
25+
error: aborting due to 1 previous error
26+
27+
For more information about this error, try `rustc --explain E0369`.

0 commit comments

Comments
 (0)