1
Fork 0

check if T is slice

fix msg
This commit is contained in:
Takayuki Maeda 2022-07-29 14:12:37 +09:00
parent 06f89b7924
commit 3ae669d758
2 changed files with 4 additions and 3 deletions

View file

@ -2943,14 +2943,15 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
) { ) {
if let ObligationCauseCode::ImplDerivedObligation(_) = obligation.cause.code() if let ObligationCauseCode::ImplDerivedObligation(_) = obligation.cause.code()
&& self.tcx.is_diagnostic_item(sym::SliceIndex, trait_pred.skip_binder().trait_ref.def_id) && self.tcx.is_diagnostic_item(sym::SliceIndex, trait_pred.skip_binder().trait_ref.def_id)
&& let ty::Slice(_) = trait_pred.skip_binder().trait_ref.substs.type_at(1).kind()
&& let ty::Ref(_, inner_ty, _) = trait_pred.skip_binder().self_ty().kind() && let ty::Ref(_, inner_ty, _) = trait_pred.skip_binder().self_ty().kind()
&& let ty::Uint(ty::UintTy::Usize) = inner_ty.kind() && let ty::Uint(ty::UintTy::Usize) = inner_ty.kind()
{ {
err.span_suggestion_verbose( err.span_suggestion_verbose(
obligation.cause.span.shrink_to_lo(), obligation.cause.span.shrink_to_lo(),
"consider dereferencing here", "dereference this index",
'*', '*',
Applicability::MaybeIncorrect, Applicability::MachineApplicable,
); );
} }
} }

View file

@ -7,7 +7,7 @@ LL | let one_item_please: i32 = [1, 2, 3][i];
= help: the trait `SliceIndex<[{integer}]>` is not implemented for `&usize` = help: the trait `SliceIndex<[{integer}]>` is not implemented for `&usize`
= help: the trait `SliceIndex<[T]>` is implemented for `usize` = help: the trait `SliceIndex<[T]>` is implemented for `usize`
= note: required because of the requirements on the impl of `Index<&usize>` for `[{integer}]` = note: required because of the requirements on the impl of `Index<&usize>` for `[{integer}]`
help: consider dereferencing here help: dereference this index
| |
LL | let one_item_please: i32 = [1, 2, 3][*i]; LL | let one_item_please: i32 = [1, 2, 3][*i];
| + | +