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: Potentially incorrect lifetime handling when structs with lifetimes are referenced #58884

Closed
Matthias247 opened this Issue Mar 3, 2019 · 1 comment

Comments

Projects
None yet
3 participants
@Matthias247
Copy link
Contributor

Matthias247 commented Mar 3, 2019

The following code (playground) produces a compiler error, even if there doesn't seem to be an obvious lifetime issue:

#![feature(futures_api, async_await, await_macro)]

struct Ctx<'a> {
    a: &'a str,
}

async fn read_loop<'a>(
    ctx: &'a mut Ctx<'a>,
) -> Result<(), ()>
{
    loop {
        await!(read_single(ctx))?;
    }
    Ok(())
}

async fn read_single<'a>(
    ctx: &'a mut Ctx<'a>,
) -> Result<(), ()>
{
    let _ = ctx.a;
    Ok(())
}

The error message is:

error[E0499]: cannot borrow `*ctx` as mutable more than once at a time
  --> src/lib.rs:12:28
   |
8  |     ctx: &'a mut Ctx<'a>,
   |     --- lifetime `'1` appears in the type of `ctx`
...
12 |         await!(read_single(ctx))?;
   |                ------------^^^-
   |                |           |
   |                |           mutable borrow starts here in previous iteration of loop
   |                argument requires that `*ctx` is borrowed for `'1`

That seems very confusing (or wrong), given that the function doesn't return anything with a lifetime. It works without the loop. An additional scope doesn't help.

I tried to work around it by introducing another lifetime for ctx, but doesn't work, since async/await doesn't support it yet.

I got another hint to write an explicit impl Future signature, which allows compiling this simplified example: https://play.rust-lang.org/?version=nightly&mode=debug&edition=2018&gist=f006c176dea64435e984cb46bcfef56d
But I feel the more basic variant should work too.

@nikomatsakis

This comment has been minimized.

Copy link
Contributor

nikomatsakis commented Mar 5, 2019

The problem here is because of the signature:

async fn read_loop<'a>(
    ctx: &'a mut Ctx<'a>,
) -> Result<(), ()>

This signature is too strong -- it forces the compiler to assume that when you call read_single, it may continue using *ctx for the remainder of 'a. (This is because the 'a appears under the mut qualifier, making it invariant.) The signature that we need is really:

async fn read_loop<'a>(
    ctx: &'a mut Ctx<'_>,
) -> Result<(), ()>

but I think this will cause problems because of the errors around having multiple lifetimes in an async fn (#56238).

I'm going to close this issue as expected behavior, and effectively a duplicate of #56238.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.