Compile time error: unable to infer enough type information about `_`


#1
trait Foo<T> {}

trait Bar<A> {
    fn method<B>(&self) where A: Foo<B>;
}

struct S;
struct K;
struct X;
struct Z;

impl Foo<S> for X {}
impl Foo<K> for Z {}

impl Bar<X> for isize {
    fn method<U>(&self) where X: Foo<U> {
    }   
}
impl Bar<Z> for isize {
    fn method<U>(&self) where Z: Foo<U> {
    }   
}

fn main() {
    1is.method::<S>();
}

compile time error:

where-clause-method-substituion.rs:35:9: 35:22 error: unable to infer enough type information      about `_`; type annotations required
where-clause-method-substituion.rs:35     1is.method::<S>();
                                          ^~~~~~~~~~~~~

#2

This is a valid error, the compiler can’t figure out if you want A = X or A = Z - maybe it could be clearer about that, but your example isn’t likely to ever compile. You can still call it using, e.g. Bar::<X>::method::<S>(&1is).


#3

It seems odd that the identifier _ would appear in the error message, given that it doesn’t appear in the original code. Is there an explanation for that? (Even if there is, that’s a misleading error message.)


#4

_ is a placeholder. It should probably be documented.


#5

_ isn’t an identifier, it’s a wildcard that means “inferred type”, for example, Vec<_> is a syntactically legal type.


#6

This should be inferred.


#7

The error message could be way more amazing.


#8

I believe the inference variable tracks its source, which in this case is a type parameter. I’m not sure how much work it would be, but it should be possible to have it say:

could not infer enough type information about this instantiation of type parameter A from trait foo::bar::Bar"


#9

Type inference variables do not, unfortunately, currently track their source.