# A lifetime problem vs. associated type (or generic parameter)

**URL:** <https://users.rust-lang.org/t/a-lifetime-problem-vs-associated-type-or-generic-parameter/111545>\
**Category:** help\
**Created:** [May 17, 2024, 9:47am UTC](https://users.rust-lang.org/t/a-lifetime-problem-vs-associated-type-or-generic-parameter/111545 "2024-05-17T09:47:17Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Tom47](https://avatars.discourse-cdn.com/v4/letter/t/ccd318/32.png) [@Tom47](https://users.rust-lang.org/u/Tom47)\
**Post date:** [May 17, 2024, 9:47am UTC](https://users.rust-lang.org/t/a-lifetime-problem-vs-associated-type-or-generic-parameter/111545/1 "2024-05-17T09:47:17Z")

</div>

Hi,

I have a problem with lifetime bounds that I was not able to resolve.

The simplified code:

```rust
trait Bar { 
    type Assoc;
}

impl<'a, T, A> Bar for (T, fn(T) -> Box<dyn Bar<Assoc = A> + 'a>) {
    type Assoc = A;
}

fn wrap<'a, T: 'a, A>(value: T, func: fn(T) -> Box<dyn Bar<Assoc = A> + 'a>) -> Box<dyn Bar<Assoc = A> + 'a> {
    Box::new((value, func))
}

```

The error:

```rust
error[E0309]: the parameter type `A` may not live long enough
  --> src/main.rs:22:5
   |
21 | fn wrap<'a, T: 'a, A>(value: T, func: fn(T) -> Box<dyn Bar<Assoc = A> + 'a>) -> Box<dyn Bar<Assoc = A> + 'a> {
   | -- the parameter type `A` must be valid for the lifetime `'a` as defined here...
22 | Box::new((value, func))
   | ^^^^^^^^^^^^^^^^^^^^^^^ ...so that the type `A` will meet its required lifetime bounds
   |
help: consider adding an explicit lifetime bound
   |
21 | fn wrap<'a, T: 'a, A: 'a>(value: T, func: fn(T) -> Box<dyn Bar<Assoc = A> + 'a>) -> Box<dyn Bar<Assoc = A> + 'a> {
   | ++++

For more information about this error, try `rustc --explain E0309`.

```

I do not understand where does the lifetime requirement come from. In my actual code there are more associated types (like `Bar::Assoc`), some are used as input to associated functions, some as output, some are used only for their associated items and some are there only to carry the type information. In any way there should be (i.e. that is my intention) no relation between `A` and `'a`, at least not in this fragment of code.

So ... how can I make it work without the `A: 'a` bound? Or is there something I am missing?

---

<div class="post-metadata">

**Author:** ![Tom47](https://avatars.discourse-cdn.com/v4/letter/t/ccd318/32.png) [@Tom47](https://users.rust-lang.org/u/Tom47)\
**Post date:** [May 17, 2024, 10:22am UTC](https://users.rust-lang.org/t/a-lifetime-problem-vs-associated-type-or-generic-parameter/111545/2 "2024-05-17T10:22:14Z")

</div>

... I mean ... everything works (so far) when I just add the bound as the compiler suggests, but it seems completely arbitrary and does not make sense to me ...

---

<div class="post-metadata">

**Author:** ![nerditation](https://avatars.discourse-cdn.com/v4/letter/n/e79b87/32.png) [@nerditation](https://users.rust-lang.org/u/nerditation)\
**Post date:** [May 17, 2024, 11:10am UTC](https://users.rust-lang.org/t/a-lifetime-problem-vs-associated-type-or-generic-parameter/111545/3 "2024-05-17T11:10:37Z")

</div>

> [@Tom47](#):
>
> everything works (so far) when I just add the bound as the compiler suggests, but it seems completely arbitrary

I'm not sure I understand it correctly, but let me try:

first, if you examine the type signature a bit, you'll find there are "logically" different levels of references (hence different lifetimes) involved:

```rust
fn wrap<'a, 'b, T, A>(
    value: T,
    func: fn(T) -> Box<dyn Bar<Assoc = A> + 'a>,
) -> Box<dyn Bar<Assoc = A> + 'b>;

```

then, because you want to return the tuple `(value, func)`, they must have lifetime bound `'b` as required by the return type, which means both `value` and `func` must outlive `'b`, translate to types:

- for `value`, the type `T: 'b`
- for `func`, the function pointer type `fn(T) -> Box<dyn ... + 'a>: 'b`, which in turn requires:
  - `dyn Bar<Assoc = A>: 'b`
  - `'a: 'b`

observe that because Box is covariant, you can replace the `'a` with `'b`, so the type of `func` can be `fn(T) -> Box<dyn Bar<...> + 'b`, and you'll see `'a` is not used anymore so just remove it, you get back the original type signature with a single lifetime.

the compiler suggest `A: 'a` is actually derived from `dyn Bar<Assoc = A>: 'a`. I believe associated types are invariant with regard to `Self`, but I'm not an expert and could well be wrong, don't quote me on this.

you can make it more obvious if you write the bound explicitly

```rust
fn wrap<'a, T, A>(
    value: T,
    func: fn(T) -> Box<dyn Bar<Assoc = A> + 'a>,
) -> Box<dyn Bar<Assoc = A> + 'a>
where
    T: 'a,
    dyn Bar<Assoc = A>: 'a,
{
    Box::new((value, func))
}

```

or, equivalently, you can give the trait object an explict name (alias) too:

```rust
type DynBar<'a, A> = dyn Bar<Assoc = A> + 'a;

fn wrap<'a, T, A>(value: T, func: fn(T) -> Box<DynBar<'a, A>>) -> Box<DynBar<'a, A>>
where
    T: 'a,
    DynBar<'a, A>: 'a,
{
    Box::new((value, func))
}

```

---

<div class="post-metadata">

**Author:** ![Tom47](https://avatars.discourse-cdn.com/v4/letter/t/ccd318/32.png) [@Tom47](https://users.rust-lang.org/u/Tom47)\
**Post date:** [May 17, 2024, 2:29pm UTC](https://users.rust-lang.org/t/a-lifetime-problem-vs-associated-type-or-generic-parameter/111545/4 "2024-05-17T14:29:26Z")

</div>

Thanks for the analysis, I mostly agree with it, but:

> [@nerditation](#):
>
> associated types are invariant with regard to `Self`

- where does the invariance come from?
- what does it have to do with variance anyway? As far as I understand, variance affects coercion, not bounds propagation. And the only coercion happening here is the one from `<(T, fn(...)) as Bar<Assoc = A>>` to `dyn Bar<Assoc = A> + 'a` which happens on the type constructor, not its argument.
- I have more uses of `dyn Bar<Assoc = A> + 'a` in my code, but this error happened only after I introduced the `fn(T) -> Box<...>` into the mix.
- After I added the lifetime bounds they did _not_ bubble up through the rest of my generic code, so maybe there _is_ some relation between an associated type and its implementor?

Now I feel like my Rust understanding is full of 'maybe's, 'I think's, 'it kind of makes sense's etc. I would like to see some more grounded description/explanation.

---

<div class="post-metadata">

**Author:** ![quinedot](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/quinedot/32/17693_2.png) [@quinedot](https://users.rust-lang.org/u/quinedot)\
**Post date:** [May 17, 2024, 7:26pm UTC](https://users.rust-lang.org/t/a-lifetime-problem-vs-associated-type-or-generic-parameter/111545/5 "2024-05-17T19:26:22Z")

</div>

> [@Tom47](#):
>
> what does it have to do with variance anyway? As far as I understand, variance affects coercion, not bounds propagation.

This is true; bounds don't care about variance, and variance doesn't matter in the OP.

> [@Tom47](#):
>
> I would like to see some more grounded description/explanation.

I'll give it as shot.

* * *

In order to type erase a base type `X` into a `dyn Trait + 'x`, the bound `X: 'x` must be met. In `wrap`, you're trying to type erase a `(T, fn(T) -> Box<dyn Bar<Assoc = A> + 'a>)` into a `dyn ... + 'a`.

The outlives bound can be broken down syntactically. Here, the required bound and it's implications are:

```rust
(T, fn(T) -> Box<dyn Bar<Assoc = A> + 'a>): 'a
    T: 'a
    fn(T) -> Box<dyn Bar<Assoc = A> + 'a>: 'a
        T: 'a
        Box<dyn Bar<Assoc = A> + 'a>: 'a
            'a: 'a
            dyn Bar<Assoc = A>: 'a
                A: 'a

```

`wrap` didn't require `A: 'a`,\[1\] so the type erasure was not allowed.

And that's really it as far as the error goes. The main potential confusions I can anticipate are

- [Why are outlives bounds defined syntactically?](https://rust-lang.github.io/rfcs/1214-projections-lifetimes-and-wf.html#motivation)
- Why doesn't `dyn Bar<Assoc = A>: 'a` imply `A: 'a` (like `&'a A` does)?

I don't have a good answer for the last one. If the trait itself has generic parameters, it can be the case that the associated type doesn't outlive `'a`; however when there are none (as in this example), [it shouldn't be possible.](https://rust-lang.github.io/rfcs/1214-projections-lifetimes-and-wf.html#outlives-for-projections)

> [@Tom47](#):
>
> In any way there should be (i.e. that is my intention) no relation between `A` and `'a`, at least not in this fragment of code.

If you want to type erase something with `A` in the type to `dyn ...+ 'a`, the `A: 'a` relationship must hold. That would be true even if `A: 'a` was implied (so you didn't have to state it explicitly).

* * *

1. and that bound isn't implied by anything else

---

<div class="post-metadata">

**Author:** ![Tom47](https://avatars.discourse-cdn.com/v4/letter/t/ccd318/32.png) [@Tom47](https://users.rust-lang.org/u/Tom47)\
**Post date:** [May 18, 2024, 2:03pm UTC](https://users.rust-lang.org/t/a-lifetime-problem-vs-associated-type-or-generic-parameter/111545/6 "2024-05-18T14:03:41Z")

</div>

Thank you for thorough explanation and references.

I still feel not 100% sold on syntactical approach to outlives bounds, but the more I think of it, it kind of starts to make sense.

---

<div class="post-metadata">

**Author:** ![system](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/system/32/51372_2.png) [@system](https://users.rust-lang.org/u/system)\
**Post date:** [August 16, 2024, 2:03pm UTC](https://users.rust-lang.org/t/a-lifetime-problem-vs-associated-type-or-generic-parameter/111545/7 "2024-08-16T14:03:44Z")

</div>

Closed on August 16, 2024, 2:03pm UTC

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.
