Skip to content

Commit 1dbaf61

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

File tree

3 files changed

+45
-1
lines changed

3 files changed

+45
-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,18 @@
1+
struct Foo;
2+
3+
impl Foo {
4+
fn foo(&self) {}
5+
}
6+
7+
fn _bar() {
8+
let x = vec![]; //~ ERROR type annotations needed for `Vec<_>`
9+
x[0usize].foo();
10+
}
11+
12+
fn main() {
13+
let thing: Vec<Foo> = vec![];
14+
15+
let foo = |i| {
16+
thing[i].foo(); //~ ERROR type annotations needed
17+
};
18+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
error[E0282]: type annotations needed for `Vec<_>`
2+
--> $DIR/calling-method-on-result-of-index-op-issue-125924.rs:8:9
3+
|
4+
LL | let x = vec![];
5+
| ^
6+
LL | x[0usize].foo();
7+
| ------ type must be known at this point
8+
|
9+
help: consider giving `x` an explicit type, where the placeholders `_` are specified
10+
|
11+
LL | let x: Vec<_> = vec![];
12+
| ++++++++
13+
14+
error[E0282]: type annotations needed
15+
--> $DIR/calling-method-on-result-of-index-op-issue-125924.rs:16:15
16+
|
17+
LL | thing[i].foo();
18+
| ^ cannot infer type
19+
20+
error: aborting due to 2 previous errors
21+
22+
For more information about this error, try `rustc --explain E0282`.

0 commit comments

Comments
 (0)