Skip to content

Commit 4101d26

Browse files
committed
Using index's span when inference of recv failed when calling method on result of index op
1 parent 1689a5a commit 4101d26

File tree

3 files changed

+27
-1
lines changed

3 files changed

+27
-1
lines changed

compiler/rustc_hir_typeck/src/expr.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -1330,7 +1330,11 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
13301330
) -> Ty<'tcx> {
13311331
let rcvr_t = self.check_expr(rcvr);
13321332
// no need to check for bot/err -- callee does that
1333-
let rcvr_t = self.structurally_resolve_type(rcvr.span, rcvr_t);
1333+
let rcvr_t = if let ExprKind::Index(_, index, _) = rcvr.kind {
1334+
self.structurally_resolve_type(index.span, rcvr_t)
1335+
} else {
1336+
self.structurally_resolve_type(rcvr.span, rcvr_t)
1337+
};
13341338
let span = segment.ident.span;
13351339

13361340
let method = match self.lookup_method(rcvr_t, segment, span, expr, rcvr, args) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
struct Foo;
2+
3+
impl Foo {
4+
fn foo(&self) {}
5+
}
6+
7+
fn main() {
8+
let thing: Vec<Foo> = vec![];
9+
10+
let foo = |i| {
11+
thing[i].foo(); //~ ERROR type annotations needed
12+
};
13+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
error[E0282]: type annotations needed
2+
--> $DIR/calling-method-on-result-of-index-op-issue-125924.rs:11:15
3+
|
4+
LL | thing[i].foo();
5+
| ^ cannot infer type
6+
7+
error: aborting due to 1 previous error
8+
9+
For more information about this error, try `rustc --explain E0282`.

0 commit comments

Comments
 (0)