Skip to content

Commit 76f662c

Browse files
authored
Rollup merge of #97109 - TaKO8Ki:fix-misleading-cannot-infer-type-for-type-parameter-error, r=oli-obk
Fix misleading `cannot infer type for type parameter` error closes #93198
2 parents 22ee395 + 3d0f9fb commit 76f662c

17 files changed

+150
-0
lines changed

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

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -866,6 +866,7 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
866866
}
867867
}
868868

869+
self.report_ambiguous_type_parameter(&mut err, arg);
869870
err.span_label(
870871
span,
871872
arg_data.cannot_infer_msg(use_diag.filter(|d| d.applies_to(span))),
@@ -933,6 +934,28 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
933934
}
934935
}
935936

937+
fn report_ambiguous_type_parameter(&self, err: &mut Diagnostic, arg: GenericArg<'tcx>) {
938+
if let GenericArgKind::Type(ty) = arg.unpack()
939+
&& let ty::Infer(ty::TyVar(ty_vid)) = *ty.kind()
940+
{
941+
let mut inner = self.inner.borrow_mut();
942+
let ty_vars = &inner.type_variables();
943+
let var_origin = ty_vars.var_origin(ty_vid);
944+
if let TypeVariableOriginKind::TypeParameterDefinition(_, Some(def_id)) =
945+
var_origin.kind
946+
&& let Some(parent_def_id) = self.tcx.parent(def_id).as_local()
947+
&& let Some(node) = self.tcx.hir().find_by_def_id(parent_def_id)
948+
{
949+
match node {
950+
hir::Node::Item(item) if matches!(item.kind, hir::ItemKind::Impl(_) | hir::ItemKind::Fn(..)) => (),
951+
hir::Node::ImplItem(impl_item) if matches!(impl_item.kind, hir::ImplItemKind::Fn(..)) => (),
952+
_ => return,
953+
}
954+
err.span_help(self.tcx.def_span(def_id), "type parameter declared here");
955+
}
956+
}
957+
}
958+
936959
pub fn need_type_info_err_in_generator(
937960
&self,
938961
kind: hir::GeneratorKind,

src/test/ui/const-generics/issues/issue-83249.stderr

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,12 @@ LL | let _ = foo([0; 1]);
55
| - ^^^ cannot infer type for type parameter `T` declared on the function `foo`
66
| |
77
| consider giving this pattern a type
8+
|
9+
help: type parameter declared here
10+
--> $DIR/issue-83249.rs:12:8
11+
|
12+
LL | fn foo<T: Foo>(_: [u8; T::N]) -> T {
13+
| ^
814

915
error: aborting due to previous error
1016

src/test/ui/consts/issue-64662.stderr

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,24 @@ error[E0282]: type annotations needed
33
|
44
LL | A = foo(),
55
| ^^^ cannot infer type for type parameter `T` declared on the function `foo`
6+
|
7+
help: type parameter declared here
8+
--> $DIR/issue-64662.rs:6:14
9+
|
10+
LL | const fn foo<T>() -> isize {
11+
| ^
612

713
error[E0282]: type annotations needed
814
--> $DIR/issue-64662.rs:3:9
915
|
1016
LL | B = foo(),
1117
| ^^^ cannot infer type for type parameter `T` declared on the function `foo`
18+
|
19+
help: type parameter declared here
20+
--> $DIR/issue-64662.rs:6:14
21+
|
22+
LL | const fn foo<T>() -> isize {
23+
| ^
1224

1325
error: aborting due to 2 previous errors
1426

src/test/ui/error-codes/E0401.stderr

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,12 @@ error[E0282]: type annotations needed
3737
|
3838
LL | bfnr(x);
3939
| ^^^^ cannot infer type for type parameter `U` declared on the function `bfnr`
40+
|
41+
help: type parameter declared here
42+
--> $DIR/E0401.rs:4:13
43+
|
44+
LL | fn bfnr<U, V: Baz<U>, W: Fn()>(y: T) {
45+
| ^
4046

4147
error: aborting due to 4 previous errors
4248

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
use std::collections::HashMap;
2+
3+
trait Store<K, V> {
4+
fn get_raw(&self, key: &K) -> Option<()>;
5+
}
6+
7+
struct InMemoryStore;
8+
9+
impl<K> Store<String, HashMap<K, String>> for InMemoryStore {
10+
fn get_raw(&self, key: &String) -> Option<()> {
11+
None
12+
}
13+
}
14+
15+
fn main() {
16+
InMemoryStore.get_raw(&String::default()); //~ ERROR type annotations needed
17+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
error[E0282]: type annotations needed
2+
--> $DIR/ambiguous_type_parameter.rs:16:19
3+
|
4+
LL | InMemoryStore.get_raw(&String::default());
5+
| ^^^^^^^ cannot infer type for type parameter `K`
6+
|
7+
help: type parameter declared here
8+
--> $DIR/ambiguous_type_parameter.rs:9:6
9+
|
10+
LL | impl<K> Store<String, HashMap<K, String>> for InMemoryStore {
11+
| ^
12+
13+
error: aborting due to previous error
14+
15+
For more information about this error, try `rustc --explain E0282`.

src/test/ui/inference/erase-type-params-in-label.stderr

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@ LL | let foo = foo(1, "");
66
| |
77
| consider giving `foo` the explicit type `Foo<_, _, W, Z>`, where the type parameter `W` is specified
88
|
9+
help: type parameter declared here
10+
--> $DIR/erase-type-params-in-label.rs:25:14
11+
|
12+
LL | fn foo<T, K, W: Default, Z: Default>(t: T, k: K) -> Foo<T, K, W, Z> {
13+
| ^
914
= note: cannot satisfy `_: Default`
1015
note: required by a bound in `foo`
1116
--> $DIR/erase-type-params-in-label.rs:25:17
@@ -25,6 +30,11 @@ LL | let bar = bar(1, "");
2530
| |
2631
| consider giving `bar` the explicit type `Bar<_, _, Z>`, where the type parameter `Z` is specified
2732
|
33+
help: type parameter declared here
34+
--> $DIR/erase-type-params-in-label.rs:14:14
35+
|
36+
LL | fn bar<T, K, Z: Default>(t: T, k: K) -> Bar<T, K, Z> {
37+
| ^
2838
= note: cannot satisfy `_: Default`
2939
note: required by a bound in `bar`
3040
--> $DIR/erase-type-params-in-label.rs:14:17

src/test/ui/inference/issue-86162-1.stderr

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@ error[E0283]: type annotations needed
44
LL | foo(gen()); //<- Do not suggest `foo::<impl Clone>()`!
55
| ^^^ cannot infer type for type parameter `impl Clone` declared on the function `foo`
66
|
7+
help: type parameter declared here
8+
--> $DIR/issue-86162-1.rs:3:11
9+
|
10+
LL | fn foo(x: impl Clone) {}
11+
| ^^^^^^^^^^
712
= note: cannot satisfy `_: Clone`
813
note: required by a bound in `foo`
914
--> $DIR/issue-86162-1.rs:3:16

src/test/ui/inference/issue-86162-2.stderr

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@ error[E0283]: type annotations needed
44
LL | Foo::bar(gen()); //<- Do not suggest `Foo::bar::<impl Clone>()`!
55
| ^^^^^^^^ cannot infer type for type parameter `impl Clone` declared on the associated function `bar`
66
|
7+
help: type parameter declared here
8+
--> $DIR/issue-86162-2.rs:8:15
9+
|
10+
LL | fn bar(x: impl Clone) {}
11+
| ^^^^^^^^^^
712
= note: cannot satisfy `_: Clone`
813
note: required by a bound in `Foo::bar`
914
--> $DIR/issue-86162-2.rs:8:20

src/test/ui/issues/issue-6458.stderr

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,12 @@ error[E0282]: type annotations needed
33
|
44
LL | foo(TypeWithState(marker::PhantomData));
55
| ^^^ cannot infer type for type parameter `State` declared on the function `foo`
6+
|
7+
help: type parameter declared here
8+
--> $DIR/issue-6458.rs:6:12
9+
|
10+
LL | pub fn foo<State>(_: TypeWithState<State>) {}
11+
| ^^^^^
612

713
error: aborting due to previous error
814

src/test/ui/missing/missing-items/missing-type-parameter.stderr

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,12 @@ error[E0282]: type annotations needed
33
|
44
LL | foo();
55
| ^^^ cannot infer type for type parameter `X` declared on the function `foo`
6+
|
7+
help: type parameter declared here
8+
--> $DIR/missing-type-parameter.rs:1:8
9+
|
10+
LL | fn foo<X>() { }
11+
| ^
612

713
error: aborting due to previous error
814

src/test/ui/suggestions/fn-needing-specified-return-type-param.stderr

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,12 @@ LL | let _ = f;
55
| - ^ cannot infer type for type parameter `A` declared on the function `f`
66
| |
77
| consider giving this pattern the explicit type `fn() -> A`, where the type parameter `A` is specified
8+
|
9+
help: type parameter declared here
10+
--> $DIR/fn-needing-specified-return-type-param.rs:1:6
11+
|
12+
LL | fn f<A>() -> A { unimplemented!() }
13+
| ^
814

915
error: aborting due to previous error
1016

src/test/ui/traits/multidispatch-convert-ambig-dest.stderr

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,24 @@ error[E0282]: type annotations needed
33
|
44
LL | test(22, std::default::Default::default());
55
| ^^^^ cannot infer type for type parameter `U` declared on the function `test`
6+
|
7+
help: type parameter declared here
8+
--> $DIR/multidispatch-convert-ambig-dest.rs:20:11
9+
|
10+
LL | fn test<T,U>(_: T, _: U)
11+
| ^
612

713
error[E0283]: type annotations needed
814
--> $DIR/multidispatch-convert-ambig-dest.rs:26:5
915
|
1016
LL | test(22, std::default::Default::default());
1117
| ^^^^ cannot infer type for type parameter `U` declared on the function `test`
1218
|
19+
help: type parameter declared here
20+
--> $DIR/multidispatch-convert-ambig-dest.rs:20:11
21+
|
22+
LL | fn test<T,U>(_: T, _: U)
23+
| ^
1324
note: multiple `impl`s satisfying `i32: Convert<_>` found
1425
--> $DIR/multidispatch-convert-ambig-dest.rs:8:1
1526
|

src/test/ui/traits/not-suggest-non-existing-fully-qualified-path.stderr

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,11 @@ LL | a.method();
1616
| | cannot infer type for type parameter `U`
1717
| this method call resolves to `U`
1818
|
19+
help: type parameter declared here
20+
--> $DIR/not-suggest-non-existing-fully-qualified-path.rs:12:9
21+
|
22+
LL | impl<T, U> V<U> for A<T>
23+
| ^
1924
note: multiple `impl`s satisfying `B: I<_>` found
2025
--> $DIR/not-suggest-non-existing-fully-qualified-path.rs:5:1
2126
|

src/test/ui/type-inference/unbounded-type-param-in-fn-with-assoc-type.stderr

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,12 @@ error[E0282]: type annotations needed
33
|
44
LL | foo();
55
| ^^^ cannot infer type for type parameter `T` declared on the function `foo`
6+
|
7+
help: type parameter declared here
8+
--> $DIR/unbounded-type-param-in-fn-with-assoc-type.rs:3:8
9+
|
10+
LL | fn foo<T, U = u64>() -> (T, U) {
11+
| ^
612

713
error: aborting due to previous error
814

src/test/ui/type-inference/unbounded-type-param-in-fn.stderr

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,12 @@ error[E0282]: type annotations needed
33
|
44
LL | foo();
55
| ^^^ cannot infer type for type parameter `T` declared on the function `foo`
6+
|
7+
help: type parameter declared here
8+
--> $DIR/unbounded-type-param-in-fn.rs:1:8
9+
|
10+
LL | fn foo<T>() -> T {
11+
| ^
612

713
error: aborting due to previous error
814

src/test/ui/type/type-annotation-needed.stderr

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@ error[E0283]: type annotations needed
44
LL | foo(42);
55
| ^^^ cannot infer type for type parameter `T` declared on the function `foo`
66
|
7+
help: type parameter declared here
8+
--> $DIR/type-annotation-needed.rs:1:8
9+
|
10+
LL | fn foo<T: Into<String>>(x: i32) {}
11+
| ^
712
= note: cannot satisfy `_: Into<String>`
813
note: required by a bound in `foo`
914
--> $DIR/type-annotation-needed.rs:1:11

0 commit comments

Comments
 (0)