Skip to content

impl Trait + 'static is not 'static if returned from a generic function #76882

Closed
@WaffleLapkin

Description

@WaffleLapkin

I tried this code:

trait Trait {}
impl Trait for () {}

fn get_trait<I>() -> impl Trait + 'static {
    ()
}

fn assert_static<A: 'static>(_: A) {}

fn test<T>(_: T) {
    assert_static(get_trait::<T>());
}

(playground)

I expected that rustc will compile this code. But it doesn't. Instead, I've got this error:

error[E0310]: the parameter type `T` may not live long enough
  --> src/lib.rs:11:5
   |
10 | fn test<T>(_: T) {
   |         - help: consider adding an explicit lifetime bound...: `T: 'static`
11 |     assert_static(get_trait::<T>());
   |     ^^^^^^^^^^^^^ ...so that the type `impl Trait` will meet its required lifetime bounds

The error seems weird because the return type of get_trait is explicitly marked as 'static and T/I is not used anywhere at all.

This may be related to #49431 though I'm not entirely sure.

Meta

Checked on 1.46.0 stable and 1.48.0-nightly (2020-09-17 f3c923a)

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-impl-traitArea: `impl Trait`. Universally / existentially quantified anonymous types with static dispatch.C-bugCategory: This is a bug.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions