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
Labels
A-async-await Area: Async & Await

Comments

@Matthias247
Copy link
Contributor

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.

@Centril Centril added the A-async-await Area: Async & Await label Mar 3, 2019
@nikomatsakis
Copy link
Contributor

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
Labels
A-async-await Area: Async & Await
Projects
None yet
Development

No branches or pull requests

3 participants