Skip to content

Commit 3d0f9fb

Browse files
committed
report ambiguous type parameters when their parents are impl or fn
fix ci error emit err for `impl_item`
1 parent 910979a commit 3d0f9fb

14 files changed

+97
-6
lines changed

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

+7-6
Original file line numberDiff line numberDiff line change
@@ -941,15 +941,16 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
941941
let mut inner = self.inner.borrow_mut();
942942
let ty_vars = &inner.type_variables();
943943
let var_origin = ty_vars.var_origin(ty_vid);
944-
if let TypeVariableOriginKind::TypeParameterDefinition(name, Some(def_id)) =
944+
if let TypeVariableOriginKind::TypeParameterDefinition(_, Some(def_id)) =
945945
var_origin.kind
946946
&& let Some(parent_def_id) = self.tcx.parent(def_id).as_local()
947-
&& let Some(hir::Node::Item(item)) = self.tcx.hir().find_by_def_id(parent_def_id)
948-
&& let hir::ItemKind::Impl(impl_item) = item.kind
949-
&& let Some(trait_ref) = &impl_item.of_trait
950-
&& let Some(did) = trait_ref.trait_def_id()
951-
&& self.tcx.generics_of(did).params.iter().any(|param| param.name == name)
947+
&& let Some(node) = self.tcx.hir().find_by_def_id(parent_def_id)
952948
{
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+
}
953954
err.span_help(self.tcx.def_span(def_id), "type parameter declared here");
954955
}
955956
}

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

+6
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

+12
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

+6
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

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

+10
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

+5
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

+5
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

+6
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

+6
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

+6
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

+11
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/type-inference/unbounded-type-param-in-fn-with-assoc-type.stderr

+6
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

+6
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

+5
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)