I have following code:
trait T<GT> {
type AT;
fn foo(&self);
}
struct AbstractT<GT, AT> {
t: Box<dyn T<GT, AT = AT>>,
}
impl<GT, AT> T<GT> for AbstractT<GT, AT> {
type AT = AT;
fn foo(&self) {
self.t.foo();
}
}
fn boxed_abstract<GT, TT: T<GT> + 'static>(tt: TT) -> Box<dyn T<GT, AT = TT::AT>> {
Box::new(AbstractT { t: Box::new(tt) })
}
Which throws these errors:
error[E0310]: the associated type `<TT as T<GT>>::AT` may not live long enough
--> src/lib.rs:20:5
|
20 | Box::new(AbstractT { t: Box::new(tt) })
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= help: consider adding an explicit lifetime bound `<TT as T<GT>>::AT: 'static`...
note: ...so that the type `AbstractT<GT, <TT as T<GT>>::AT>` will meet its required lifetime bounds
--> src/lib.rs:20:5
|
20 | Box::new(AbstractT { t: Box::new(tt) })
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
error[E0310]: the parameter type `GT` may not live long enough
--> src/lib.rs:20:5
|
19 | fn boxed_abstract<GT, TT: T<GT> + 'static>(tt: TT) -> Box<dyn T<GT, AT = TT::AT>> {
| -- help: consider adding an explicit lifetime bound...: `GT: 'static`
20 | Box::new(AbstractT { t: Box::new(tt) })
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
note: ...so that the type `AbstractT<GT, <TT as T<GT>>::AT>` will meet its required lifetime bounds
--> src/lib.rs:20:5
|
20 | Box::new(AbstractT { t: Box::new(tt) })
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
If I remove GT
from everywhere, it compiles just fine, but with GT
it fails with a bunch of lifetime errors. It seems that existence of GT
should not affect lifetimes of either dyn T
or T::AT
(because it's not used in them), but it apparently does. Likewise, lifetime of dyn T
should not depend lifetimes of GT
or AT
but it apparently does.
Am I missing something or is it a lifetime inference problem?