1
Fork 0

Rollup merge of #99671 - TaKO8Ki:suggest-dereferencing-index, r=compiler-errors

Suggest dereferencing index when trying to use a reference of usize as index

fixes #96678
This commit is contained in:
Yuki Okushi 2022-07-30 07:39:50 +09:00 committed by GitHub
commit 4a44efae14
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
12 changed files with 111 additions and 32 deletions

View file

@ -2648,6 +2648,9 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
Some((index_ty, element_ty)) => {
// two-phase not needed because index_ty is never mutable
self.demand_coerce(idx, idx_t, index_ty, None, AllowTwoPhase::No);
self.select_obligations_where_possible(false, |errors| {
self.point_at_index_if_possible(errors, idx.span)
});
element_ty
}
None => {
@ -2691,6 +2694,22 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
}
}
fn point_at_index_if_possible(
&self,
errors: &mut Vec<traits::FulfillmentError<'tcx>>,
span: Span,
) {
for error in errors {
match error.obligation.predicate.kind().skip_binder() {
ty::PredicateKind::Trait(predicate)
if self.tcx.is_diagnostic_item(sym::SliceIndex, predicate.trait_ref.def_id) => {
}
_ => continue,
}
error.obligation.cause.span = span;
}
}
fn check_expr_yield(
&self,
value: &'tcx hir::Expr<'tcx>,