Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Async await mulitple lifetimes issue with Box #68759

Closed
95th opened this issue Feb 2, 2020 · 3 comments
Closed

Async await mulitple lifetimes issue with Box #68759

95th opened this issue Feb 2, 2020 · 3 comments
Assignees
Labels
A-async-await AsyncAwait-Triaged C-feature-request T-lang

Comments

@95th
Copy link
Contributor

@95th 95th commented Feb 2, 2020

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.

@95th 95th changed the title Async await mulitple lifetimes issue Async await mulitple lifetimes issue with Box Feb 2, 2020
@95th
Copy link
Contributor Author

@95th 95th commented Feb 2, 2020

Note that non-async version of Box works:

pub trait A {}

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

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

@matthewjasper matthewjasper self-assigned this Feb 2, 2020
@jonas-schievink jonas-schievink added C-feature-request T-lang A-async-await labels Feb 2, 2020
@tmandry tmandry added AsyncAwait-OnDeck AsyncAwait-Triaged labels Feb 11, 2020
@tmandry tmandry added this to High priority in wg-async-foundations triage Feb 11, 2020
@tmandry tmandry added this to To do in wg-async work Feb 11, 2020
@tmandry
Copy link
Contributor

@tmandry tmandry commented Mar 3, 2020

This looks like a duplicate of #68950 (which may have the same cause as #64552)
EDIT: This one came first, so technically #68950 duplicates this issue

@tmandry
Copy link
Contributor

@tmandry tmandry commented Mar 3, 2020

Closing as a duplicate of #64552, but noting on that issue that we should add a test with the code from this issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-async-await AsyncAwait-Triaged C-feature-request T-lang
Projects
Development

No branches or pull requests

4 participants