Point turbofish inference errors at the uninferred generic arg

This commit is contained in:
arferreira
2026-03-12 17:39:29 -04:00
parent 801bc19d60
commit e485708b84
6 changed files with 42 additions and 19 deletions
@@ -1260,21 +1260,33 @@ fn visit_expr(&mut self, expr: &'tcx Expr<'tcx>) {
have_turbofish,
} = args;
let generics = tcx.generics_of(generics_def_id);
if let Some(mut argument_index) = generics
if let Some((argument_index, _)) = generics
.own_args(args)
.iter()
.position(|&arg| self.generic_arg_contains_target(arg))
.enumerate()
.find(|&(_, &arg)| self.generic_arg_contains_target(arg))
{
if generics.has_own_self() {
argument_index += 1;
}
let args = self.tecx.resolve_vars_if_possible(args);
let generic_args =
&generics.own_args_no_defaults(tcx, args)[generics.own_counts().lifetimes..];
let span = match expr.kind {
ExprKind::MethodCall(path, ..) => path.ident.span,
ExprKind::MethodCall(segment, ..)
if have_turbofish
&& let Some(hir_args) = segment.args
&& let Some(idx) =
argument_index.checked_sub(generics.own_counts().lifetimes)
&& let Some(arg) =
hir_args.args.get(hir_args.num_lifetime_params() + idx) =>
{
arg.span()
}
ExprKind::MethodCall(segment, ..) => segment.ident.span,
_ => expr.span,
};
let mut argument_index = argument_index;
if generics.has_own_self() {
argument_index += 1;
}
self.update_infer_source(InferSource {
span,
@@ -1,8 +1,8 @@
error[E0282]: type annotations needed
--> $DIR/issue-105608.rs:13:22
--> $DIR/issue-105608.rs:13:28
|
LL | Combination::<0>.and::<_>().and::<_>();
| ^^^ cannot infer type of the type parameter `M` declared on the method `and`
| ^ cannot infer type of the type parameter `M` declared on the method `and`
error: aborting due to 1 previous error
@@ -4,8 +4,8 @@
// rewriting them — the suggestion just rewrites user syntax into
// fully-qualified form without resolving anything.
//
// The span still points at the method name rather than the unresolved `_`;
// fixing that is left as future work.
// When a turbofish is present, the diagnostic points at the specific
// uninferred generic argument rather than the method name.
struct S;
@@ -15,11 +15,16 @@ fn f<A, B>(self, _a: A) -> B {
}
}
fn with_turbofish() {
fn turbofish_second_arg() {
S.f::<u32, _>(42);
//~^ ERROR type annotations needed
}
fn turbofish_first_arg() {
S.f::<_, _>(42);
//~^ ERROR type annotations needed
}
fn without_turbofish() {
S.f(42);
//~^ ERROR type annotations needed
@@ -1,11 +1,17 @@
error[E0282]: type annotations needed
--> $DIR/useless-turbofish-suggestion.rs:19:7
--> $DIR/useless-turbofish-suggestion.rs:19:16
|
LL | S.f::<u32, _>(42);
| ^ cannot infer type of the type parameter `B` declared on the method `f`
| ^ cannot infer type of the type parameter `B` declared on the method `f`
error[E0282]: type annotations needed
--> $DIR/useless-turbofish-suggestion.rs:24:7
--> $DIR/useless-turbofish-suggestion.rs:24:14
|
LL | S.f::<_, _>(42);
| ^ cannot infer type of the type parameter `B` declared on the method `f`
error[E0282]: type annotations needed
--> $DIR/useless-turbofish-suggestion.rs:29:7
|
LL | S.f(42);
| ^ cannot infer type of the type parameter `B` declared on the method `f`
@@ -15,6 +21,6 @@ help: consider specifying the generic arguments
LL | S.f::<i32, B>(42);
| ++++++++++
error: aborting due to 2 previous errors
error: aborting due to 3 previous errors
For more information about this error, try `rustc --explain E0282`.
+2 -2
View File
@@ -1,8 +1,8 @@
error[E0282]: type annotations needed
--> $DIR/issue-23041.rs:6:7
--> $DIR/issue-23041.rs:6:22
|
LL | b.downcast_ref::<fn(_)->_>();
| ^^^^^^^^^^^^ cannot infer type of the type parameter `T` declared on the method `downcast_ref`
| ^^^^^^^^ cannot infer type of the type parameter `T` declared on the method `downcast_ref`
error: aborting due to 1 previous error
@@ -12,10 +12,10 @@ LL | let x: Option<_> = None::<T>;
| +++++
error[E0282]: type annotations needed
--> $DIR/issue-42234-unknown-receiver-type.rs:12:10
--> $DIR/issue-42234-unknown-receiver-type.rs:12:16
|
LL | .sum::<_>()
| ^^^ cannot infer type of the type parameter `S` declared on the method `sum`
| ^ cannot infer type of the type parameter `S` declared on the method `sum`
error: aborting due to 2 previous errors