Closed
Description
This is simplified from some real code I have.
use std::borrow::BorrowMut;
use std::marker::PhantomData;
trait T {}
impl<X> T for Box<X> where X: T {}
struct S<'a> {
phantom: PhantomData<&'a u32>,
}
struct U;
impl T for U {}
fn f1<'a, R>(_: R) -> impl T + 'static
where R: BorrowMut<S<'a>>
{
let f = Box::new(U);
f
}
fn f2<'a>(s: S<'a>) -> impl T + 'static {
f1::<S<'a>>(s)
}
fn f1_boxed<'a, R>(_: R) -> Box<T>
where R: BorrowMut<S<'a>>
{
let f = Box::new(U);
f
}
fn f2_boxed<'a>(s: S<'a>) -> Box<T> {
f1_boxed::<S<'a>>(s)
}
The boxed versions compile fine, but the impl Trait version fails with
error[E0477]: the type `impl T` does not fulfill the required lifetime
--> src/lib.rs:23:24
|
23 | fn f2<'a>(s: S<'a>) -> impl T + 'static {
| ^^^^^^^^^^^^^^^^
|
= note: type must satisfy the static lifetime
I see this on both stable and 1.30.0-nightly (7061b27 2018-08-28).