Skip to content

Commit 69634f2

Browse files
committed
Always point at index span on index obligation failure
Use more targetted span for index obligation failures by rewriting the obligation cause span. CC rust-lang#66023
1 parent d8dbf7c commit 69634f2

File tree

3 files changed

+22
-12
lines changed

3 files changed

+22
-12
lines changed

compiler/rustc_hir_typeck/src/expr.rs

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2877,7 +2877,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
28772877
// two-phase not needed because index_ty is never mutable
28782878
self.demand_coerce(idx, idx_t, index_ty, None, AllowTwoPhase::No);
28792879
self.select_obligations_where_possible(|errors| {
2880-
self.point_at_index_if_possible(errors, idx.span)
2880+
self.point_at_index(errors, idx.span)
28812881
});
28822882
element_ty
28832883
}
@@ -3036,18 +3036,8 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
30363036
.ok()
30373037
}
30383038

3039-
fn point_at_index_if_possible(
3040-
&self,
3041-
errors: &mut Vec<traits::FulfillmentError<'tcx>>,
3042-
span: Span,
3043-
) {
3039+
fn point_at_index(&self, errors: &mut Vec<traits::FulfillmentError<'tcx>>, span: Span) {
30443040
for error in errors {
3045-
match error.obligation.predicate.kind().skip_binder() {
3046-
ty::PredicateKind::Clause(ty::ClauseKind::Trait(predicate))
3047-
if self.tcx.is_diagnostic_item(sym::SliceIndex, predicate.trait_ref.def_id) => {
3048-
}
3049-
_ => continue,
3050-
}
30513041
error.obligation.cause.span = span;
30523042
}
30533043
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
fn main() {
2+
let a = std::collections::HashMap::<String,String>::new();
3+
let s = "hello";
4+
let _b = a[
5+
&s //~ ERROR E0277
6+
];
7+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
error[E0277]: the trait bound `String: Borrow<&str>` is not satisfied
2+
--> $DIR/point-at-index-for-obligation-failure.rs:5:9
3+
|
4+
LL | &s
5+
| ^^ the trait `Borrow<&str>` is not implemented for `String`
6+
|
7+
= help: the trait `Borrow<str>` is implemented for `String`
8+
= help: for that trait implementation, expected `str`, found `&str`
9+
= note: required for `HashMap<String, String>` to implement `Index<&&str>`
10+
11+
error: aborting due to previous error
12+
13+
For more information about this error, try `rustc --explain E0277`.

0 commit comments

Comments
 (0)