Skip to content

Commit 140044c

Browse files
committed
Auto merge of #149085 - matthiaskrgr:rollup-f8ia15e, r=matthiaskrgr
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
2 parents a591113 + fd88e61 commit 140044c

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+875
-416
lines changed

compiler/rustc_hir_typeck/src/method/suggest.rs

Lines changed: 50 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1754,6 +1754,20 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
17541754
err.note(note);
17551755
}
17561756

1757+
if let ty::Adt(adt_def, _) = rcvr_ty.kind() {
1758+
unsatisfied_predicates.iter().find(|(pred, _parent, _cause)| {
1759+
if let ty::PredicateKind::Clause(ty::ClauseKind::Trait(pred)) =
1760+
pred.kind().skip_binder()
1761+
{
1762+
self.suggest_hashmap_on_unsatisfied_hashset_buildhasher(
1763+
err, &pred, *adt_def,
1764+
)
1765+
} else {
1766+
false
1767+
}
1768+
});
1769+
}
1770+
17571771
*suggested_derive = self.suggest_derive(err, unsatisfied_predicates);
17581772
*unsatisfied_bounds = true;
17591773
}
@@ -2990,7 +3004,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
29903004
.filter_map(|e| match e.obligation.predicate.kind().skip_binder() {
29913005
ty::PredicateKind::Clause(ty::ClauseKind::Trait(pred)) => {
29923006
match pred.self_ty().kind() {
2993-
ty::Adt(_, _) => Some(pred),
3007+
ty::Adt(_, _) => Some((e.root_obligation.predicate, pred)),
29943008
_ => None,
29953009
}
29963010
}
@@ -3000,18 +3014,18 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
30003014

30013015
// Note for local items and foreign items respectively.
30023016
let (mut local_preds, mut foreign_preds): (Vec<_>, Vec<_>) =
3003-
preds.iter().partition(|&pred| {
3017+
preds.iter().partition(|&(_, pred)| {
30043018
if let ty::Adt(def, _) = pred.self_ty().kind() {
30053019
def.did().is_local()
30063020
} else {
30073021
false
30083022
}
30093023
});
30103024

3011-
local_preds.sort_by_key(|pred: &&ty::TraitPredicate<'_>| pred.trait_ref.to_string());
3025+
local_preds.sort_by_key(|(_, pred)| pred.trait_ref.to_string());
30123026
let local_def_ids = local_preds
30133027
.iter()
3014-
.filter_map(|pred| match pred.self_ty().kind() {
3028+
.filter_map(|(_, pred)| match pred.self_ty().kind() {
30153029
ty::Adt(def, _) => Some(def.did()),
30163030
_ => None,
30173031
})
@@ -3024,7 +3038,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
30243038
})
30253039
.collect::<Vec<_>>()
30263040
.into();
3027-
for pred in &local_preds {
3041+
for (_, pred) in &local_preds {
30283042
if let ty::Adt(def, _) = pred.self_ty().kind() {
30293043
local_spans.push_span_label(
30303044
self.tcx.def_span(def.did()),
@@ -3033,7 +3047,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
30333047
}
30343048
}
30353049
if local_spans.primary_span().is_some() {
3036-
let msg = if let [local_pred] = local_preds.as_slice() {
3050+
let msg = if let [(_, local_pred)] = local_preds.as_slice() {
30373051
format!(
30383052
"an implementation of `{}` might be missing for `{}`",
30393053
local_pred.trait_ref.print_trait_sugared(),
@@ -3051,9 +3065,10 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
30513065
err.span_note(local_spans, msg);
30523066
}
30533067

3054-
foreign_preds.sort_by_key(|pred: &&ty::TraitPredicate<'_>| pred.trait_ref.to_string());
3068+
foreign_preds
3069+
.sort_by_key(|(_, pred): &(_, ty::TraitPredicate<'_>)| pred.trait_ref.to_string());
30553070

3056-
for pred in foreign_preds {
3071+
for (_, pred) in &foreign_preds {
30573072
let ty = pred.self_ty();
30583073
let ty::Adt(def, _) = ty.kind() else { continue };
30593074
let span = self.tcx.def_span(def.did());
@@ -3066,6 +3081,17 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
30663081
mspan,
30673082
format!("`{ty}` does not implement `{}`", pred.trait_ref.print_trait_sugared()),
30683083
);
3084+
3085+
foreign_preds.iter().find(|&(root_pred, pred)| {
3086+
if let ty::PredicateKind::Clause(ty::ClauseKind::Trait(root_pred)) =
3087+
root_pred.kind().skip_binder()
3088+
&& let Some(root_adt) = root_pred.self_ty().ty_adt_def()
3089+
{
3090+
self.suggest_hashmap_on_unsatisfied_hashset_buildhasher(err, pred, root_adt)
3091+
} else {
3092+
false
3093+
}
3094+
});
30693095
}
30703096

30713097
let preds: Vec<_> = errors
@@ -4388,6 +4414,22 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
43884414

43894415
self.autoderef(span, rcvr_ty).silence_errors().any(|(ty, _)| is_local(ty))
43904416
}
4417+
4418+
fn suggest_hashmap_on_unsatisfied_hashset_buildhasher(
4419+
&self,
4420+
err: &mut Diag<'_>,
4421+
pred: &ty::TraitPredicate<'_>,
4422+
adt: ty::AdtDef<'_>,
4423+
) -> bool {
4424+
if self.tcx.is_diagnostic_item(sym::HashSet, adt.did())
4425+
&& self.tcx.is_diagnostic_item(sym::BuildHasher, pred.def_id())
4426+
{
4427+
err.help("you might have intended to use a HashMap instead");
4428+
true
4429+
} else {
4430+
false
4431+
}
4432+
}
43914433
}
43924434

43934435
#[derive(Copy, Clone, Debug)]

compiler/rustc_lint/src/shadowed_into_iter.rs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,11 @@ impl<'tcx> LateLintPass<'tcx> for ShadowedIntoIter {
124124
return;
125125
};
126126

127+
// This check needs to avoid ICE from when `receiver_arg` is from macro expansion
128+
// Which leads to empty span in span arithmetic below
129+
// cc: https://github.com/rust-lang/rust/issues/147408
130+
let span = receiver_arg.span.find_ancestor_in_same_ctxt(expr.span);
131+
127132
// If this expression comes from the `IntoIter::into_iter` inside of a for loop,
128133
// we should just suggest removing the `.into_iter()` or changing it to `.iter()`
129134
// to disambiguate if we want to iterate by-value or by-ref.
@@ -134,14 +139,15 @@ impl<'tcx> LateLintPass<'tcx> for ShadowedIntoIter {
134139
&& let hir::ExprKind::Call(path, [_]) = &arg.kind
135140
&& let hir::ExprKind::Path(qpath) = path.kind
136141
&& cx.tcx.qpath_is_lang_item(qpath, LangItem::IntoIterIntoIter)
142+
&& let Some(span) = span
137143
{
138144
Some(ShadowedIntoIterDiagSub::RemoveIntoIter {
139-
span: receiver_arg.span.shrink_to_hi().to(expr.span.shrink_to_hi()),
145+
span: span.shrink_to_hi().to(expr.span.shrink_to_hi()),
140146
})
141-
} else if can_suggest_ufcs {
147+
} else if can_suggest_ufcs && let Some(span) = span {
142148
Some(ShadowedIntoIterDiagSub::UseExplicitIntoIter {
143149
start_span: expr.span.shrink_to_lo(),
144-
end_span: receiver_arg.span.shrink_to_hi().to(expr.span.shrink_to_hi()),
150+
end_span: span.shrink_to_hi().to(expr.span.shrink_to_hi()),
145151
})
146152
} else {
147153
None

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/Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ optimize_for_size = []
2323
# Make `RefCell` store additional debugging information, which is printed out when
2424
# a borrow error occurs
2525
debug_refcell = []
26+
llvm_enzyme = []
2627

2728
[lints.rust.unexpected_cfgs]
2829
level = "warn"
@@ -38,4 +39,6 @@ check-cfg = [
3839
'cfg(target_has_reliable_f16_math)',
3940
'cfg(target_has_reliable_f128)',
4041
'cfg(target_has_reliable_f128_math)',
42+
'cfg(llvm_enzyme)',
43+
4144
]

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.

0 commit comments

Comments
 (0)