Always point at index span on index obligation failure

Use more targetted span for index obligation failures by rewriting the
obligation cause span.

CC #66023
This commit is contained in:
Esteban Küber 2023-11-13 00:52:10 +00:00
parent d8dbf7ca0e
commit 69634f2077
3 changed files with 22 additions and 12 deletions

View File

@ -2877,7 +2877,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
// 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(|errors| {
self.point_at_index_if_possible(errors, idx.span)
self.point_at_index(errors, idx.span)
});
element_ty
}
@ -3036,18 +3036,8 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
.ok()
}
fn point_at_index_if_possible(
&self,
errors: &mut Vec<traits::FulfillmentError<'tcx>>,
span: Span,
) {
fn point_at_index(&self, errors: &mut Vec<traits::FulfillmentError<'tcx>>, span: Span) {
for error in errors {
match error.obligation.predicate.kind().skip_binder() {
ty::PredicateKind::Clause(ty::ClauseKind::Trait(predicate))
if self.tcx.is_diagnostic_item(sym::SliceIndex, predicate.trait_ref.def_id) => {
}
_ => continue,
}
error.obligation.cause.span = span;
}
}

View File

@ -0,0 +1,7 @@
fn main() {
let a = std::collections::HashMap::<String,String>::new();
let s = "hello";
let _b = a[
&s //~ ERROR E0277
];
}

View File

@ -0,0 +1,13 @@
error[E0277]: the trait bound `String: Borrow<&str>` is not satisfied
--> $DIR/point-at-index-for-obligation-failure.rs:5:9
|
LL | &s
| ^^ the trait `Borrow<&str>` is not implemented for `String`
|
= help: the trait `Borrow<str>` is implemented for `String`
= help: for that trait implementation, expected `str`, found `&str`
= note: required for `HashMap<String, String>` to implement `Index<&&str>`
error: aborting due to previous error
For more information about this error, try `rustc --explain E0277`.