Skip to content

Commit 3051885

Browse files
committed
Point turbofish inference errors at the uninferred generic arg
1 parent 801bc19 commit 3051885

5 files changed

Lines changed: 37 additions & 12 deletions

File tree

compiler/rustc_trait_selection/src/error_reporting/infer/need_type_info.rs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1272,7 +1272,21 @@ impl<'a, 'tcx> Visitor<'tcx> for FindInferSourceVisitor<'a, 'tcx> {
12721272
let generic_args =
12731273
&generics.own_args_no_defaults(tcx, args)[generics.own_counts().lifetimes..];
12741274
let span = match expr.kind {
1275-
ExprKind::MethodCall(path, ..) => path.ident.span,
1275+
ExprKind::MethodCall(segment, ..) => {
1276+
if have_turbofish
1277+
&& let Some(hir_args) = segment.args
1278+
&& let Some(idx) = argument_index.checked_sub(
1279+
generics.has_own_self() as usize
1280+
+ generics.own_counts().lifetimes,
1281+
)
1282+
&& let Some(arg) =
1283+
hir_args.args.iter().filter(|a| a.is_ty_or_const()).nth(idx)
1284+
{
1285+
arg.span()
1286+
} else {
1287+
segment.ident.span
1288+
}
1289+
}
12761290
_ => expr.span,
12771291
};
12781292

tests/ui/inference/useless-turbofish-suggestion.rs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
// rewriting them — the suggestion just rewrites user syntax into
55
// fully-qualified form without resolving anything.
66
//
7-
// The span still points at the method name rather than the unresolved `_`;
8-
// fixing that is left as future work.
7+
// When a turbofish is present, the diagnostic points at the specific
8+
// uninferred generic argument rather than the method name.
99

1010
struct S;
1111

@@ -15,11 +15,16 @@ impl S {
1515
}
1616
}
1717

18-
fn with_turbofish() {
18+
fn turbofish_second_arg() {
1919
S.f::<u32, _>(42);
2020
//~^ ERROR type annotations needed
2121
}
2222

23+
fn turbofish_first_arg() {
24+
S.f::<_, _>(42);
25+
//~^ ERROR type annotations needed
26+
}
27+
2328
fn without_turbofish() {
2429
S.f(42);
2530
//~^ ERROR type annotations needed
Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,17 @@
11
error[E0282]: type annotations needed
2-
--> $DIR/useless-turbofish-suggestion.rs:19:7
2+
--> $DIR/useless-turbofish-suggestion.rs:19:16
33
|
44
LL | S.f::<u32, _>(42);
5-
| ^ cannot infer type of the type parameter `B` declared on the method `f`
5+
| ^ cannot infer type of the type parameter `B` declared on the method `f`
6+
7+
error[E0282]: type annotations needed
8+
--> $DIR/useless-turbofish-suggestion.rs:24:14
9+
|
10+
LL | S.f::<_, _>(42);
11+
| ^ cannot infer type of the type parameter `B` declared on the method `f`
612

713
error[E0282]: type annotations needed
8-
--> $DIR/useless-turbofish-suggestion.rs:24:7
14+
--> $DIR/useless-turbofish-suggestion.rs:29:7
915
|
1016
LL | S.f(42);
1117
| ^ cannot infer type of the type parameter `B` declared on the method `f`
@@ -15,6 +21,6 @@ help: consider specifying the generic arguments
1521
LL | S.f::<i32, B>(42);
1622
| ++++++++++
1723

18-
error: aborting due to 2 previous errors
24+
error: aborting due to 3 previous errors
1925

2026
For more information about this error, try `rustc --explain E0282`.

tests/ui/issues/issue-23041.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-23041.rs:6:7
2+
--> $DIR/issue-23041.rs:6:22
33
|
44
LL | b.downcast_ref::<fn(_)->_>();
5-
| ^^^^^^^^^^^^ cannot infer type of the type parameter `T` declared on the method `downcast_ref`
5+
| ^^^^^^^^ cannot infer type of the type parameter `T` declared on the method `downcast_ref`
66

77
error: aborting due to 1 previous error
88

tests/ui/span/issue-42234-unknown-receiver-type.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@ LL | let x: Option<_> = None::<T>;
1212
| +++++
1313

1414
error[E0282]: type annotations needed
15-
--> $DIR/issue-42234-unknown-receiver-type.rs:12:10
15+
--> $DIR/issue-42234-unknown-receiver-type.rs:12:16
1616
|
1717
LL | .sum::<_>()
18-
| ^^^ cannot infer type of the type parameter `S` declared on the method `sum`
18+
| ^ cannot infer type of the type parameter `S` declared on the method `sum`
1919

2020
error: aborting due to 2 previous errors
2121

0 commit comments

Comments
 (0)