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

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() {

compile time error: 35:22 error: unable to infer enough type information      about `_`; type annotations required     1is.method::<S>();


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).


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.)


_ is a placeholder. It should probably be documented.


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


This should be inferred.


The error message could be way more amazing.


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"


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