Skip to content

Commit 49888e5

Browse files
committed
Using index's span when inference of recv failed when calling method on result of index op
1 parent db8aca4 commit 49888e5

File tree

3 files changed

+48
-1
lines changed

3 files changed

+48
-1
lines changed

compiler/rustc_hir_typeck/src/expr.rs

+8-1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ use crate::errors::{
1212
FieldMultiplySpecifiedInInitializer, FunctionalRecordUpdateOnNonStruct, HelpUseLatestEdition,
1313
YieldExprOutsideOfCoroutine,
1414
};
15+
use crate::expr_use_visitor::TypeInformationCtxt;
1516
use crate::fatally_break_rust;
1617
use crate::type_error_struct;
1718
use crate::CoroutineTypes;
@@ -1329,7 +1330,13 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
13291330
) -> Ty<'tcx> {
13301331
let rcvr_t = self.check_expr(rcvr);
13311332
// no need to check for bot/err -- callee does that
1332-
let rcvr_t = self.structurally_resolve_type(rcvr.span, rcvr_t);
1333+
let rcvr_t = if let ExprKind::Index(_, index, _) = rcvr.kind
1334+
&& self.typeck_results().expr_ty(index).is_ty_var()
1335+
{
1336+
self.structurally_resolve_type(index.span, rcvr_t)
1337+
} else {
1338+
self.structurally_resolve_type(rcvr.span, rcvr_t)
1339+
};
13331340

13341341
let method = match self.lookup_method(rcvr_t, segment, segment.ident.span, expr, rcvr, args)
13351342
{
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)