Skip to content

Async await mulitple lifetimes issue with Box #68759

Closed
@95th

Description

@95th

Following code is not accepted ATM (play):

pub trait A {}

pub struct B {
    a: Box<dyn A>,
}

impl B {
    pub async fn new(a: Box<dyn A>, x: &(), y: &()) -> B {
        B { a }
    }
}

Error:

error[E0700]: hidden type for `impl Trait` captures lifetime that does not appear in bounds
  --> src/main.rs:10:56
   |
10 |     pub async fn new(a: Box<dyn A>, x: &(), y: &()) -> B {
   |                                                        ^
   |
note: hidden type `impl std::future::Future` captures the scope of call-site for function at 10:58
  --> src/main.rs:10:58
   |
10 |       pub async fn new(a: Box<dyn A>, x: &(), y: &()) -> B {
   |  __________________________________________________________^
11 | |         B { a }
12 | |     }
   | |_____^

However, the following code is accepted:

pub trait A {}

pub struct B<A> {
    a: A,
}

impl<X: A> B<X> {
    pub async fn new(a: X, x: &(), y: &()) -> B<X> {
        B { a }
    }
}

I know one can workaround this by using impl trait with explicit lifetimes, but this is an ergonomics issue.

Metadata

Metadata

Assignees

Labels

A-async-awaitArea: Async & AwaitAsyncAwait-TriagedAsync-await issues that have been triaged during a working group meeting.C-feature-requestCategory: A feature request, i.e: not implemented / a PR.T-langRelevant to the language team

Type

No type

Projects

Status

Done

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions