Skip to content

Commit b904ce9

Browse files
committed
account for self type when looking for source of unsolved ty var
1 parent 480068c commit b904ce9

File tree

5 files changed

+52
-5
lines changed

5 files changed

+52
-5
lines changed

Diff for: compiler/rustc_infer/src/infer/error_reporting/need_type_info.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -1191,11 +1191,14 @@ impl<'a, 'tcx> Visitor<'tcx> for FindInferSourceVisitor<'a, 'tcx> {
11911191
have_turbofish,
11921192
} = args;
11931193
let generics = tcx.generics_of(generics_def_id);
1194-
if let Some(argument_index) = generics
1194+
if let Some(mut argument_index) = generics
11951195
.own_substs(substs)
11961196
.iter()
11971197
.position(|&arg| self.generic_arg_contains_target(arg))
11981198
{
1199+
if generics.parent.is_none() && generics.has_self {
1200+
argument_index += 1;
1201+
}
11991202
let substs = self.infcx.resolve_vars_if_possible(substs);
12001203
let generic_args = &generics.own_substs_no_defaults(tcx, substs)
12011204
[generics.own_counts().lifetimes..];

Diff for: tests/ui/inference/need_type_info/concrete-impl.rs

+3
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,13 @@ struct Two;
77
struct Struct;
88

99
impl Ambiguous<One> for Struct {}
10+
//~^ NOTE multiple `impl`s satisfying `Struct: Ambiguous<_>` found
1011
impl Ambiguous<Two> for Struct {}
1112

1213
fn main() {
1314
<Struct as Ambiguous<_>>::method();
1415
//~^ ERROR type annotations needed
16+
//~| NOTE cannot infer type of the type parameter `A`
1517
//~| ERROR type annotations needed
18+
//~| NOTE infer type of the type parameter `A`
1619
}

Diff for: tests/ui/inference/need_type_info/concrete-impl.stderr

+5-4
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,21 @@
11
error[E0282]: type annotations needed
2-
--> $DIR/concrete-impl.rs:13:5
2+
--> $DIR/concrete-impl.rs:14:5
33
|
44
LL | <Struct as Ambiguous<_>>::method();
5-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ cannot infer type of the type parameter `Self` declared on the trait `Ambiguous`
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ cannot infer type of the type parameter `A` declared on the trait `Ambiguous`
66

77
error[E0283]: type annotations needed
8-
--> $DIR/concrete-impl.rs:13:5
8+
--> $DIR/concrete-impl.rs:14:5
99
|
1010
LL | <Struct as Ambiguous<_>>::method();
11-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ cannot infer type of the type parameter `Self` declared on the trait `Ambiguous`
11+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ cannot infer type of the type parameter `A` declared on the trait `Ambiguous`
1212
|
1313
note: multiple `impl`s satisfying `Struct: Ambiguous<_>` found
1414
--> $DIR/concrete-impl.rs:9:1
1515
|
1616
LL | impl Ambiguous<One> for Struct {}
1717
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
18+
LL |
1819
LL | impl Ambiguous<Two> for Struct {}
1920
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2021

Diff for: tests/ui/inference/need_type_info/issue-109905.rs

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// Test that we show the correct type parameter that couldn't be inferred and that we don't
2+
// end up stating nonsense like "type parameter `'a`" which we used to do.
3+
4+
trait Trait<'a, T> {
5+
fn m(self);
6+
}
7+
8+
impl<'a, A> Trait<'a, A> for () {
9+
fn m(self) {}
10+
}
11+
12+
fn qualified() {
13+
<() as Trait<'static, _>>::m(());
14+
//~^ ERROR type annotations needed
15+
//~| NOTE cannot infer type of the type parameter `T`
16+
17+
}
18+
19+
fn unqualified() {
20+
Trait::<'static, _>::m(());
21+
//~^ ERROR type annotations needed
22+
//~| NOTE cannot infer type of the type parameter `T`
23+
}
24+
25+
fn main() {}
+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
error[E0282]: type annotations needed
2+
--> $DIR/issue-109905.rs:13:5
3+
|
4+
LL | <() as Trait<'static, _>>::m(());
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ cannot infer type of the type parameter `T` declared on the trait `Trait`
6+
7+
error[E0282]: type annotations needed
8+
--> $DIR/issue-109905.rs:20:5
9+
|
10+
LL | Trait::<'static, _>::m(());
11+
| ^^^^^^^^^^^^^^^^^^^^^^ cannot infer type of the type parameter `T` declared on the trait `Trait`
12+
13+
error: aborting due to 2 previous errors
14+
15+
For more information about this error, try `rustc --explain E0282`.

0 commit comments

Comments
 (0)