Question about lifetime bounds at ATs and GATs for trait objects

Hey folks,

this example from here has been chosen to show that lifetime elision for trait objects ignores bounds from ATs and GATs.

What I don't understand in this example is why should type BA: 'x + ?Sized affect the lifetime of the inner trait object? For my understanding the BA type and it's lifetime bound are about the box and not the inner trait object so I wonder why this bound should affect the lifetime of the inner value?

Don't get me wrong I understand that bounds from ATs and GATs are ignored for dyn Trait (see here), but what confuses me is why the lifetime bound in the example should affect the inner value of the box?

Regards
keks

2 Likes

I agree with your interpretation. Using Box doesn’t make too much sense for the demonstration, as with dyn Trait used inside of the Box, only lifetime arguments of Box, and constraints set by the definitions of Trait and Box, should affect the elision, anyways, as documented … well at least hinted at … e.g. in the reference:

Note that the innermost object sets the bound, so &'a Box<dyn Foo> is still &'a Box<dyn Foo + 'static>.

trait BoundedAssoc<'x> {
    type BA: 'x + ?Sized;
}

// Still `Box<dyn Trait + 'static>`
impl<'x> BoundedAssoc<'x> for () { type BA = Box<dyn Trait>; }

// Fails
fn bib<'a>(obj: Box<dyn Trait + 'a>) {
    let obj: <() as BoundedAssoc<'a>>::BA = obj;
}

cc @quinedot, re-writing this from type BA = Box<dyn Trait>; to type BA = dyn Trait; the way @keks993 does in their last playground link would make sense, no? It’s also notably already defined as a ?Sized associated type; perhaps you initially intended to do type BA = dyn Trait;, anyways? In which case, either you accidentally switched to Box later, or deliberately for some reason not apparent to me at the moment.

2 Likes

Thanks, I'll take a closer look soon. (The examples are generally just selections from experiments with tons of trials including the unboxed case.)

2 Likes

Thanks for spotting this. I'll push a fix soon.

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.