Skip to content

dyn Trait<'a> shouldn't carry 'a lifetime #104684

Open
@yshui

Description

I tried this code:

fn check<'a, T: Trait<'a> + ?Sized>() {}
trait Trait<'a>: 'static {}

fn check2<'a>() {
    check::<dyn Trait<'a> + 'static>();
}

I expected to see this happen: It compiles

dyn Trait<'a> should have an lifetime independent from 'a. i.e. dyn Trait<'a> + 'b should have lifetime 'b. in this particular case, 'b is 'static.

Instead, this happened:

error: lifetime may not live long enough
 --> src/lib.rs:5:5
  |
4 | fn check2<'a>() {
  |           -- lifetime `'a` defined here
5 |     check::<dyn Trait<'a> + 'static>();
  |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ requires that `'a` must outlive `'static`

Related

#88904. Since this is another way of creating dyn Trait: !Trait, except in this case, I think the unsized coercion should be allowed, and dyn Trait does impl Trait.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

No one assigned

    Labels

    A-lifetimesArea: Lifetimes / regionsA-trait-objectsArea: trait objects, vtable layoutC-discussionCategory: Discussion or questions that doesn't represent real issues.T-typesRelevant to the types 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