Rollup merge of #147398 - Jamesbarford:fix/method-call-type-inference-error, r=chenyukang

Fix; correct placement of type inference error for method calls

Addresses a FIXME for displaying errors on method calls;

Before;
```
error[E0282]: type annotations needed
  --> /<location>/src/main.rs:48:15
   |
## |             e.is_conversion_error();
   |               ^^^^^^^^^^^^^^^^^^^ cannot infer type
```

After;

```
error[E0282]: type annotations needed
  --> /<location>/src/main.rs:48:15
   |
## |             e.is_conversion_error();
   |             ^ cannot infer type
```
This commit is contained in:
Matthias Krüger 2025-10-07 19:39:08 +02:00 committed by GitHub
commit 0174900c5d
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
18 changed files with 52 additions and 45 deletions

View file

@ -7,9 +7,8 @@ use rustc_attr_parsing::is_doc_alias_attrs_contain_symbol;
use rustc_data_structures::fx::FxHashSet;
use rustc_data_structures::sso::SsoHashSet;
use rustc_errors::Applicability;
use rustc_hir as hir;
use rustc_hir::HirId;
use rustc_hir::def::DefKind;
use rustc_hir::{self as hir, ExprKind, HirId, Node};
use rustc_hir_analysis::autoderef::{self, Autoderef};
use rustc_infer::infer::canonical::{Canonical, OriginalQueryValues, QueryResponse};
use rustc_infer::infer::{BoundRegionConversionTime, DefineOpaqueTypes, InferOk, TyCtxtInferExt};
@ -486,13 +485,25 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
let ty = self.resolve_vars_if_possible(ty.value);
let guar = match *ty.kind() {
ty::Infer(ty::TyVar(_)) => {
// We want to get the variable name that the method
// is being called on. If it is a method call.
let err_span = match (mode, self.tcx.hir_node(scope_expr_id)) {
(
Mode::MethodCall,
Node::Expr(hir::Expr {
kind: ExprKind::MethodCall(_, recv, ..),
..
}),
) => recv.span,
_ => span,
};
let raw_ptr_call = bad_ty.reached_raw_pointer
&& !self.tcx.features().arbitrary_self_types();
// FIXME: Ideally we'd use the span of the self-expr here,
// not of the method path.
let mut err = self.err_ctxt().emit_inference_failure_err(
self.body_id,
span,
err_span,
ty.into(),
TypeAnnotationNeeded::E0282,
!raw_ptr_call,

View file

@ -5,7 +5,7 @@ LL | let var_fn = Value::wrap();
| ^^^^^^
...
LL | let _ = var_fn.clone();
| ----- type must be known at this point
| ------ type must be known at this point
|
help: consider giving `var_fn` an explicit type, where the placeholders `_` are specified
|

View file

@ -5,7 +5,7 @@ LL | needs_foo(|x| {
| ^
...
LL | x.to_string();
| --------- type must be known at this point
| - type must be known at this point
|
help: consider giving this closure parameter an explicit type
|

View file

@ -5,7 +5,7 @@ LL | Thunk::new(|mut cont| {
| ^^^^^^^^
LL |
LL | cont.reify_as();
| -------- type must be known at this point
| ---- type must be known at this point
|
help: consider giving this closure parameter an explicit type
|
@ -19,7 +19,7 @@ LL | Thunk::new(|mut cont| {
| ^^^^^^^^
LL |
LL | cont.reify_as();
| -------- type must be known at this point
| ---- type must be known at this point
|
help: consider giving this closure parameter an explicit type
|

View file

@ -5,7 +5,7 @@ LL | Thunk::new(|mut cont| {
| ^^^^^^^^
LL |
LL | cont.reify_as();
| -------- type must be known at this point
| ---- type must be known at this point
|
help: consider giving this closure parameter an explicit type
|
@ -19,7 +19,7 @@ LL | Thunk::new(|mut cont| {
| ^^^^^^^^
LL |
LL | cont.reify_as();
| -------- type must be known at this point
| ---- type must be known at this point
|
help: consider giving this closure parameter an explicit type
|

View file

@ -2,7 +2,7 @@ error[E0282]: type annotations needed
--> $DIR/incompat-call-after-qualified-path-0.rs:21:6
|
LL | f(|a, b| a.cmp(b));
| ^ --- type must be known at this point
| ^ - type must be known at this point
|
help: consider giving this closure parameter an explicit type
|

View file

@ -2,7 +2,7 @@ error[E0282]: type annotations needed
--> $DIR/incompat-call-after-qualified-path-1.rs:25:6
|
LL | f(|a, b| a.cmp(b));
| ^ --- type must be known at this point
| ^ - type must be known at this point
|
help: consider giving this closure parameter an explicit type
|

View file

@ -1,8 +1,8 @@
error[E0282]: type annotations needed
--> $DIR/issue-20261.rs:4:11
--> $DIR/issue-20261.rs:4:9
|
LL | i.clone();
| ^^^^^ cannot infer type
| ^ cannot infer type
error: aborting due to 1 previous error

View file

@ -4,7 +4,7 @@ error[E0282]: type annotations needed
LL | let x = panic!();
| ^
LL | x.clone();
| ----- type must be known at this point
| - type must be known at this point
|
help: consider giving `x` an explicit type
|

View file

@ -2,7 +2,7 @@ error[E0282]: type annotations needed
--> $DIR/branches3.rs:9:10
|
LL | |s| s.len()
| ^ --- type must be known at this point
| ^ - type must be known at this point
|
help: consider giving this closure parameter an explicit type
|
@ -13,7 +13,7 @@ error[E0282]: type annotations needed
--> $DIR/branches3.rs:18:10
|
LL | |s| s.len()
| ^ --- type must be known at this point
| ^ - type must be known at this point
|
help: consider giving this closure parameter an explicit type
|
@ -24,7 +24,7 @@ error[E0282]: type annotations needed
--> $DIR/branches3.rs:26:10
|
LL | |s| s.len()
| ^ --- type must be known at this point
| ^ - type must be known at this point
|
help: consider giving this closure parameter an explicit type
|
@ -35,7 +35,7 @@ error[E0282]: type annotations needed
--> $DIR/branches3.rs:33:10
|
LL | |s| s.len()
| ^ --- type must be known at this point
| ^ - type must be known at this point
|
help: consider giving this closure parameter an explicit type
|

View file

@ -1,11 +1,10 @@
error[E0282]: type annotations needed
--> $DIR/call_method_unknown_pointee.rs:10:41
--> $DIR/call_method_unknown_pointee.rs:10:23
|
LL | let _a: i32 = (ptr as *const _).read();
| ^^^^
| |
| cannot infer type
| cannot call a method on a raw pointer with an unknown pointee type
| ^^^^^^^^^^^^^^^^^ ---- cannot call a method on a raw pointer with an unknown pointee type
| |
| cannot infer type
error[E0282]: type annotations needed for `*const _`
--> $DIR/call_method_unknown_pointee.rs:12:13
@ -22,13 +21,12 @@ LL | let b: *const _ = ptr as *const _;
| ++++++++++
error[E0282]: type annotations needed
--> $DIR/call_method_unknown_pointee.rs:21:39
--> $DIR/call_method_unknown_pointee.rs:21:23
|
LL | let _a: i32 = (ptr as *mut _).read();
| ^^^^
| |
| cannot infer type
| cannot call a method on a raw pointer with an unknown pointee type
| ^^^^^^^^^^^^^^^ ---- cannot call a method on a raw pointer with an unknown pointee type
| |
| cannot infer type
error[E0282]: type annotations needed for `*mut _`
--> $DIR/call_method_unknown_pointee.rs:23:13

View file

@ -1,14 +1,14 @@
error[E0282]: type annotations needed
--> $DIR/call_method_unknown_referent.rs:20:31
--> $DIR/call_method_unknown_referent.rs:20:19
|
LL | let _a: i32 = (ptr as &_).read();
| ^^^^ cannot infer type
| ^^^^^^^^^^^ cannot infer type
error[E0282]: type annotations needed
--> $DIR/call_method_unknown_referent.rs:26:37
--> $DIR/call_method_unknown_referent.rs:26:14
|
LL | let _b = (rc as std::rc::Rc<_>).read();
| ^^^^ cannot infer type
| ^^^^^^^^^^^^^^^^^^^^^^ cannot infer type
error[E0599]: no method named `read` found for struct `SmartPtr<T>` in the current scope
--> $DIR/call_method_unknown_referent.rs:46:35

View file

@ -21,10 +21,10 @@ note: the traits `Iterator` and `ToTokens` must be implemented
--> $SRC_DIR/core/src/iter/traits/iterator.rs:LL:COL
error[E0282]: type annotations needed
--> $DIR/not-repeatable.rs:11:13
--> $DIR/not-repeatable.rs:11:25
|
LL | let _ = quote! { $($ip)* };
| ^^^^^^^^^^^^^^^^^^ cannot infer type
| ^^ cannot infer type
error: aborting due to 2 previous errors

View file

@ -5,7 +5,7 @@ LL | let x = [Foo(PhantomData); 2];
| ^
LL |
LL | extract(x).max(2);
| --- type must be known at this point
| ---------- type must be known at this point
|
help: consider giving `x` an explicit type, where the placeholders `_` are specified
|

View file

@ -4,7 +4,7 @@ error[E0282]: type annotations needed
LL | let x: Option<_> = None;
| ^^^^ cannot infer type of the type parameter `T` declared on the enum `Option`
LL | x.unwrap().method_that_could_exist_on_some_type();
| ------------------------------------ type must be known at this point
| ---------- type must be known at this point
|
help: consider specifying the generic argument
|
@ -16,8 +16,6 @@ error[E0282]: type annotations needed
|
LL | .sum::<_>()
| ^^^ cannot infer type of the type parameter `S` declared on the method `sum`
LL | .to_string()
| --------- type must be known at this point
|
error: aborting due to 2 previous errors

View file

@ -2,7 +2,7 @@ error[E0282]: type annotations needed
--> $DIR/closures_in_branches.rs:8:10
|
LL | |x| x.len()
| ^ --- type must be known at this point
| ^ - type must be known at this point
|
help: consider giving this closure parameter an explicit type
|
@ -13,7 +13,7 @@ error[E0282]: type annotations needed
--> $DIR/closures_in_branches.rs:22:10
|
LL | |x| x.len()
| ^ --- type must be known at this point
| ^ - type must be known at this point
|
help: consider giving this closure parameter an explicit type
|

View file

@ -5,7 +5,7 @@ LL | let iv = S ^ index.into();
| ^^
LL |
LL | &iv.to_bytes_be();
| ----------- type must be known at this point
| -- type must be known at this point
|
help: consider giving `iv` an explicit type
|

View file

@ -18,10 +18,10 @@ LL | for node in graph.iter() {
| ^^^^ method not found in `&G`
error[E0282]: type annotations needed
--> $DIR/issue-13853.rs:28:14
--> $DIR/issue-13853.rs:28:9
|
LL | node.zomg();
| ^^^^ cannot infer type
| ^^^^ cannot infer type
error[E0308]: mismatched types
--> $DIR/issue-13853.rs:37:13