From 49888e5127de8b6312eb9b0367460273432649c0 Mon Sep 17 00:00:00 2001 From: r0cky Date: Wed, 5 Jun 2024 12:22:19 +0800 Subject: [PATCH] Using index's span when inference of recv failed when calling method on result of index op --- compiler/rustc_hir_typeck/src/expr.rs | 9 +++++++- ...thod-on-result-of-index-op-issue-125924.rs | 18 +++++++++++++++ ...-on-result-of-index-op-issue-125924.stderr | 22 +++++++++++++++++++ 3 files changed, 48 insertions(+), 1 deletion(-) create mode 100644 tests/ui/inference/need_type_info/calling-method-on-result-of-index-op-issue-125924.rs create mode 100644 tests/ui/inference/need_type_info/calling-method-on-result-of-index-op-issue-125924.stderr diff --git a/compiler/rustc_hir_typeck/src/expr.rs b/compiler/rustc_hir_typeck/src/expr.rs index 4cc936eed08c5..c960f2a37655f 100644 --- a/compiler/rustc_hir_typeck/src/expr.rs +++ b/compiler/rustc_hir_typeck/src/expr.rs @@ -12,6 +12,7 @@ use crate::errors::{ FieldMultiplySpecifiedInInitializer, FunctionalRecordUpdateOnNonStruct, HelpUseLatestEdition, YieldExprOutsideOfCoroutine, }; +use crate::expr_use_visitor::TypeInformationCtxt; use crate::fatally_break_rust; use crate::type_error_struct; use crate::CoroutineTypes; @@ -1329,7 +1330,13 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { ) -> Ty<'tcx> { let rcvr_t = self.check_expr(rcvr); // no need to check for bot/err -- callee does that - let rcvr_t = self.structurally_resolve_type(rcvr.span, rcvr_t); + let rcvr_t = if let ExprKind::Index(_, index, _) = rcvr.kind + && self.typeck_results().expr_ty(index).is_ty_var() + { + self.structurally_resolve_type(index.span, rcvr_t) + } else { + self.structurally_resolve_type(rcvr.span, rcvr_t) + }; let method = match self.lookup_method(rcvr_t, segment, segment.ident.span, expr, rcvr, args) { diff --git a/tests/ui/inference/need_type_info/calling-method-on-result-of-index-op-issue-125924.rs b/tests/ui/inference/need_type_info/calling-method-on-result-of-index-op-issue-125924.rs new file mode 100644 index 0000000000000..ccd7daa6d6d3e --- /dev/null +++ b/tests/ui/inference/need_type_info/calling-method-on-result-of-index-op-issue-125924.rs @@ -0,0 +1,18 @@ +struct Foo; + +impl Foo { + fn foo(&self) {} +} + +fn _bar() { + let x = vec![]; //~ ERROR type annotations needed for `Vec<_>` + x[0usize].foo(); +} + +fn main() { + let thing: Vec = vec![]; + + let foo = |i| { + thing[i].foo(); //~ ERROR type annotations needed + }; +} diff --git a/tests/ui/inference/need_type_info/calling-method-on-result-of-index-op-issue-125924.stderr b/tests/ui/inference/need_type_info/calling-method-on-result-of-index-op-issue-125924.stderr new file mode 100644 index 0000000000000..b07ad1d5becf8 --- /dev/null +++ b/tests/ui/inference/need_type_info/calling-method-on-result-of-index-op-issue-125924.stderr @@ -0,0 +1,22 @@ +error[E0282]: type annotations needed for `Vec<_>` + --> $DIR/calling-method-on-result-of-index-op-issue-125924.rs:8:9 + | +LL | let x = vec![]; + | ^ +LL | x[0usize].foo(); + | --------- type must be known at this point + | +help: consider giving `x` an explicit type, where the placeholders `_` are specified + | +LL | let x: Vec<_> = vec![]; + | ++++++++ + +error[E0282]: type annotations needed + --> $DIR/calling-method-on-result-of-index-op-issue-125924.rs:16:15 + | +LL | thing[i].foo(); + | ^ cannot infer type + +error: aborting due to 2 previous errors + +For more information about this error, try `rustc --explain E0282`.