Skip to content

existential impl Trait gets seriously confused about lifetimes, "impl T + 'static" does not satisfy static lifetime #53791

Closed
@khuey

Description

@khuey

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).

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsA-impl-traitArea: `impl Trait`. Universally / existentially quantified anonymous types with static dispatch.A-lifetimesArea: Lifetimes / regionsNLL-diagnosticsWorking towards the "diagnostic parity" goal

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions