Skip to content

Cannot coerce dyn Trait<'short> + 'long to dyn Trait<'long> + 'long #139457

Closed as not planned
@Jules-Bertholet

Description

@Jules-Bertholet

I tried this code:

trait Trait<'a> {}

fn convert<'short, 'long: 'short>(
    obj: Box<dyn Trait<'short> + 'long>,
) -> Box<dyn Trait<'long> + 'long> {
    obj
}

I expected to see this happen: It compiles. The concrete type of the dyn Trait<'short> + 'long cannot mention 'short (as it lives for all of 'long), so it must implement Trait<'long> also.

Instead, this happened:

error: lifetime may not live long enough
 --> src/lib.rs:6:5
  |
3 | fn convert<'short, 'long: 'short>(
  |            ------  ----- lifetime `'long` defined here
  |            |
  |            lifetime `'short` defined here
...
6 |     obj
  |     ^^^ function was supposed to return data with lifetime `'long` but it is returning data with lifetime `'short`
  |
  = help: consider adding the following bound: `'short: 'long`

Meta

rustc --version:

1.86.0

@rustbot label A-lifetimes A-trait-objects T-types

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-dyn-traitArea: trait objects, vtable layoutA-lifetimesArea: Lifetimes / regionsC-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