Skip to content

Error message in async fn refers to lifetime '1 without defining it #74072

Closed

Description

pub async fn f(x: &mut i32) -> &i32 {
    let y = &*x;
    *x += 1;
    y
}

Output on 1.46.0-nightly (2020-07-04 0cd7ff7) (Playground)

   Compiling playground v0.0.1 (/playground)
error[E0506]: cannot assign to `*x` because it is borrowed
 --> src/lib.rs:3:5
  |
2 |     let y = &*x;
  |             --- borrow of `*x` occurs here
3 |     *x += 1;
  |     ^^^^^^^ assignment to borrowed `*x` occurs here
4 |     y
  |     - returning this value requires that `*x` is borrowed for `'1`
5 | }
  | - return type of generator is &'1 i32

error: aborting due to previous error

For more information about this error, try `rustc --explain E0506`.
error: could not compile `playground`.

To learn more, run the command again with --verbose.

Without async, a note like this is emitted, and should also be emitted with async:

1 | pub fn f(x: &mut i32) -> &i32 {
  |             - let's call the lifetime of this reference `'1`

Output on stable 1.44.1 (with async) is different:

   Compiling playground v0.0.1 (/playground)
error[E0506]: cannot assign to `*x` because it is borrowed
 --> src/lib.rs:3:5
  |
2 |     let y = &*x;
  |             --- borrow of `*x` occurs here
3 |     *x += 1;
  |     ^^^^^^^ assignment to borrowed `*x` occurs here

error: aborting due to previous error

For more information about this error, try `rustc --explain E0506`.
error: could not compile `playground`.

To learn more, run the command again with --verbose.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

No one assigned

    Labels

    A-async-awaitArea: Async & AwaitA-diagnosticsArea: Messages for errors, warnings, and lintsA-lifetimesArea: Lifetimes / regionsAsyncAwait-TriagedAsync-await issues that have been triaged during a working group meeting.C-bugCategory: This is a bug.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    • Status

      Done

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions