How to add trait bounds to generic associated type (GAT)?

Hi

I'm playing around with generic associated types (GAT) and need some help. In the playground example I have two traits:
The ProducerTrait which has a generic associated type Product

 
 trait ProducerTrait {
     type Product<'p>: ProductTrait;
 
     fn produce(&self, name: &str) -> Self::Product<'_>;
 }

and the ProducerUserTrait which adds the Display trait to the bound of the ProducerTrait's associated type

trait ProducerUserTrait<P: ProducerTrait>
    where for<'p> P::Product<'p>: Display
{
    fn producer(&self) -> P;

    [...]
}

Unfortunately it does not work. I'm getting the following error:

<CarFactory as ProducerTrait>::Product<'p>` doesn't implement `std::fmt::Display

Car which is the Product of the CarFactory implements Display (see line 32), but the compiler claims that it does not.

Am I making a mistake here or is this a problem with GAT which still is in development? An answer would be much appreciated!

I feel like I've seen a similar problem before in some other URLO thread. It clearly seems like a problem with the current GAT implementation (or perhaps more precisely with the trait solver and how it handles GATs) and I suppose its something that should be fixed eventually in the compiler. For the time being there actually are workarounds for the problem, but I'm on my phone, so I cannot demonstrate any right now.

In case you're interested in the workaround, you can provide a more complete example that shows why the where bound on ProducerUserTrait involving P::Product is even needed (probably some default implementation of a method?) and I can give you some code featuring the workaround tomorrow.

1 Like

I think the fix is this PR.

1 Like

Thanks! Ok, good to know that it's not a mistake on my side. No need for the workarounds as I'm just playing around.

Good to know. Thank you!

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.