Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

diagnostics: account for self type when looking for source of unsolved type variable #109957

Merged
merged 1 commit into from
Apr 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -1191,11 +1191,14 @@ impl<'a, 'tcx> Visitor<'tcx> for FindInferSourceVisitor<'a, 'tcx> {
have_turbofish,
} = args;
let generics = tcx.generics_of(generics_def_id);
if let Some(argument_index) = generics
if let Some(mut argument_index) = generics
.own_substs(substs)
.iter()
.position(|&arg| self.generic_arg_contains_target(arg))
{
if generics.parent.is_none() && generics.has_self {
argument_index += 1;
}
let substs = self.infcx.resolve_vars_if_possible(substs);
let generic_args = &generics.own_substs_no_defaults(tcx, substs)
[generics.own_counts().lifetimes..];
Expand Down
3 changes: 3 additions & 0 deletions tests/ui/inference/need_type_info/concrete-impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,13 @@ struct Two;
struct Struct;

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

fn main() {
<Struct as Ambiguous<_>>::method();
//~^ ERROR type annotations needed
//~| NOTE cannot infer type of the type parameter `A`
//~| ERROR type annotations needed
//~| NOTE infer type of the type parameter `A`
}
9 changes: 5 additions & 4 deletions tests/ui/inference/need_type_info/concrete-impl.stderr
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
error[E0282]: type annotations needed
--> $DIR/concrete-impl.rs:13:5
--> $DIR/concrete-impl.rs:14:5
|
LL | <Struct as Ambiguous<_>>::method();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ cannot infer type of the type parameter `Self` declared on the trait `Ambiguous`
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ cannot infer type of the type parameter `A` declared on the trait `Ambiguous`

error[E0283]: type annotations needed
--> $DIR/concrete-impl.rs:13:5
--> $DIR/concrete-impl.rs:14:5
|
LL | <Struct as Ambiguous<_>>::method();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ cannot infer type of the type parameter `Self` declared on the trait `Ambiguous`
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ cannot infer type of the type parameter `A` declared on the trait `Ambiguous`
|
note: multiple `impl`s satisfying `Struct: Ambiguous<_>` found
--> $DIR/concrete-impl.rs:9:1
|
LL | impl Ambiguous<One> for Struct {}
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
LL |
LL | impl Ambiguous<Two> for Struct {}
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Expand Down
25 changes: 25 additions & 0 deletions tests/ui/inference/need_type_info/issue-109905.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// Test that we show the correct type parameter that couldn't be inferred and that we don't
// end up stating nonsense like "type parameter `'a`" which we used to do.

trait Trait<'a, T> {
fn m(self);
}

impl<'a, A> Trait<'a, A> for () {
fn m(self) {}
}

fn qualified() {
<() as Trait<'static, _>>::m(());
//~^ ERROR type annotations needed
//~| NOTE cannot infer type of the type parameter `T`

}

fn unqualified() {
Trait::<'static, _>::m(());
//~^ ERROR type annotations needed
//~| NOTE cannot infer type of the type parameter `T`
}

fn main() {}
15 changes: 15 additions & 0 deletions tests/ui/inference/need_type_info/issue-109905.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
error[E0282]: type annotations needed
--> $DIR/issue-109905.rs:13:5
|
LL | <() as Trait<'static, _>>::m(());
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ cannot infer type of the type parameter `T` declared on the trait `Trait`

error[E0282]: type annotations needed
--> $DIR/issue-109905.rs:20:5
|
LL | Trait::<'static, _>::m(());
| ^^^^^^^^^^^^^^^^^^^^^^ cannot infer type of the type parameter `T` declared on the trait `Trait`

error: aborting due to 2 previous errors

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