Skip to content

Commit 79ad10e

Browse files
authored
Rollup merge of rust-lang#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 ```
2 parents 605e3e1 + 43f7eaa commit 79ad10e

18 files changed

+52
-45
lines changed

compiler/rustc_hir_typeck/src/method/probe.rs

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,8 @@ use rustc_attr_parsing::is_doc_alias_attrs_contain_symbol;
77
use rustc_data_structures::fx::FxHashSet;
88
use rustc_data_structures::sso::SsoHashSet;
99
use rustc_errors::Applicability;
10-
use rustc_hir as hir;
11-
use rustc_hir::HirId;
1210
use rustc_hir::def::DefKind;
11+
use rustc_hir::{self as hir, ExprKind, HirId, Node};
1312
use rustc_hir_analysis::autoderef::{self, Autoderef};
1413
use rustc_infer::infer::canonical::{Canonical, OriginalQueryValues, QueryResponse};
1514
use rustc_infer::infer::{BoundRegionConversionTime, DefineOpaqueTypes, InferOk, TyCtxtInferExt};
@@ -486,13 +485,25 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
486485
let ty = self.resolve_vars_if_possible(ty.value);
487486
let guar = match *ty.kind() {
488487
ty::Infer(ty::TyVar(_)) => {
488+
// We want to get the variable name that the method
489+
// is being called on. If it is a method call.
490+
let err_span = match (mode, self.tcx.hir_node(scope_expr_id)) {
491+
(
492+
Mode::MethodCall,
493+
Node::Expr(hir::Expr {
494+
kind: ExprKind::MethodCall(_, recv, ..),
495+
..
496+
}),
497+
) => recv.span,
498+
_ => span,
499+
};
500+
489501
let raw_ptr_call = bad_ty.reached_raw_pointer
490502
&& !self.tcx.features().arbitrary_self_types();
491-
// FIXME: Ideally we'd use the span of the self-expr here,
492-
// not of the method path.
503+
493504
let mut err = self.err_ctxt().emit_inference_failure_err(
494505
self.body_id,
495-
span,
506+
err_span,
496507
ty.into(),
497508
TypeAnnotationNeeded::E0282,
498509
!raw_ptr_call,

tests/ui/autoref-autoderef/deref-ambiguity-becomes-nonambiguous.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ LL | let var_fn = Value::wrap();
55
| ^^^^^^
66
...
77
LL | let _ = var_fn.clone();
8-
| ----- type must be known at this point
8+
| ------ type must be known at this point
99
|
1010
help: consider giving `var_fn` an explicit type, where the placeholders `_` are specified
1111
|

tests/ui/closures/deduce-signature/obligation-with-leaking-placeholders.next.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ LL | needs_foo(|x| {
55
| ^
66
...
77
LL | x.to_string();
8-
| --------- type must be known at this point
8+
| - type must be known at this point
99
|
1010
help: consider giving this closure parameter an explicit type
1111
|

tests/ui/impl-trait/hidden-type-is-opaque-2.default.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ LL | Thunk::new(|mut cont| {
55
| ^^^^^^^^
66
LL |
77
LL | cont.reify_as();
8-
| -------- type must be known at this point
8+
| ---- type must be known at this point
99
|
1010
help: consider giving this closure parameter an explicit type
1111
|
@@ -19,7 +19,7 @@ LL | Thunk::new(|mut cont| {
1919
| ^^^^^^^^
2020
LL |
2121
LL | cont.reify_as();
22-
| -------- type must be known at this point
22+
| ---- type must be known at this point
2323
|
2424
help: consider giving this closure parameter an explicit type
2525
|

tests/ui/impl-trait/hidden-type-is-opaque-2.next.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ LL | Thunk::new(|mut cont| {
55
| ^^^^^^^^
66
LL |
77
LL | cont.reify_as();
8-
| -------- type must be known at this point
8+
| ---- type must be known at this point
99
|
1010
help: consider giving this closure parameter an explicit type
1111
|
@@ -19,7 +19,7 @@ LL | Thunk::new(|mut cont| {
1919
| ^^^^^^^^
2020
LL |
2121
LL | cont.reify_as();
22-
| -------- type must be known at this point
22+
| ---- type must be known at this point
2323
|
2424
help: consider giving this closure parameter an explicit type
2525
|

tests/ui/inference/need_type_info/incompat-call-after-qualified-path-0.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ error[E0282]: type annotations needed
22
--> $DIR/incompat-call-after-qualified-path-0.rs:21:6
33
|
44
LL | f(|a, b| a.cmp(b));
5-
| ^ --- type must be known at this point
5+
| ^ - type must be known at this point
66
|
77
help: consider giving this closure parameter an explicit type
88
|

tests/ui/inference/need_type_info/incompat-call-after-qualified-path-1.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ error[E0282]: type annotations needed
22
--> $DIR/incompat-call-after-qualified-path-1.rs:25:6
33
|
44
LL | f(|a, b| a.cmp(b));
5-
| ^ --- type must be known at this point
5+
| ^ - type must be known at this point
66
|
77
help: consider giving this closure parameter an explicit type
88
|

tests/ui/issues/issue-20261.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
error[E0282]: type annotations needed
2-
--> $DIR/issue-20261.rs:4:11
2+
--> $DIR/issue-20261.rs:4:9
33
|
44
LL | i.clone();
5-
| ^^^^^ cannot infer type
5+
| ^ cannot infer type
66

77
error: aborting due to 1 previous error
88

tests/ui/issues/issue-2151.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ error[E0282]: type annotations needed
44
LL | let x = panic!();
55
| ^
66
LL | x.clone();
7-
| ----- type must be known at this point
7+
| - type must be known at this point
88
|
99
help: consider giving `x` an explicit type
1010
|

tests/ui/lazy-type-alias-impl-trait/branches3.stderr

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ error[E0282]: type annotations needed
22
--> $DIR/branches3.rs:9:10
33
|
44
LL | |s| s.len()
5-
| ^ --- type must be known at this point
5+
| ^ - type must be known at this point
66
|
77
help: consider giving this closure parameter an explicit type
88
|
@@ -13,7 +13,7 @@ error[E0282]: type annotations needed
1313
--> $DIR/branches3.rs:18:10
1414
|
1515
LL | |s| s.len()
16-
| ^ --- type must be known at this point
16+
| ^ - type must be known at this point
1717
|
1818
help: consider giving this closure parameter an explicit type
1919
|
@@ -24,7 +24,7 @@ error[E0282]: type annotations needed
2424
--> $DIR/branches3.rs:26:10
2525
|
2626
LL | |s| s.len()
27-
| ^ --- type must be known at this point
27+
| ^ - type must be known at this point
2828
|
2929
help: consider giving this closure parameter an explicit type
3030
|
@@ -35,7 +35,7 @@ error[E0282]: type annotations needed
3535
--> $DIR/branches3.rs:33:10
3636
|
3737
LL | |s| s.len()
38-
| ^ --- type must be known at this point
38+
| ^ - type must be known at this point
3939
|
4040
help: consider giving this closure parameter an explicit type
4141
|

0 commit comments

Comments
 (0)