Annotating impl traits

I recently ran into the fact that you can't nest impl Trait, e.g. &impl Foo<impl Bar>. Googling why lead me to the github discussion of why it was banned and this code example showing how it could create an inference ambiguity.. After reading the code, I agree it creates a situation where which type the user wants is ambiguous, BUT that's tolerated all over the place. Everytime you write something like collect<Vec<T>>() you're helping the compiler because there are multiple possibilities and you need to express what you want.

It sounds like what distinguishes this case is that rust doesn't give a way to disambiguate here. Is there any reason Rust couldn't be extended so you could write?:

foobar(S1: impl Quux<Assoc=impl Foo<S3>>);

It seems consistent with the fact that you can leave some types out of an annotation with _ to only introduce some trait constraints.

You can write that. (I also added a pub to silence an unrelated "private in public" lint.)

1 Like

Oh wait -- did you mean at the call site? That's RFC 0803 which got removed due to stalling out and lacking a champion.

1 Like

I did mean at the call site, and I assumed it already worked for regular types at call sites for consistency sake and just needed to support impl Trait :sweat_smile:

Gotcha, yeah, just took me a second to travel from the error on the declaration to what you were asking about at the call site in my head.

I do hope expression and/or pattern annotations come back in some form, but it sadly seems a ways off now. At least now, having an impl Trait in argument position doesn't preclude using turbofish, so the example can be rewritten as

pub fn foobar<B: Bar>(_: impl Quux<Assoc=impl Foo<B>>) {}

fn main() {

although even the fully explicit generic form you had to use before wasn't that bad IMO [1]:

pub fn foobar<Q: Quux, B: Bar>(_: Q)
    Q::Assoc: Foo<B>,

fn main() {
    foobar::<_, S3>(S1);

  1. but I do acknowledge <> throws me off a lot less than it seems to others ↩ī¸Ž

1 Like

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.