Skip to content

Lifetime error points to wrong reference in async fn return #75785

Closed

Description

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

Output (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 |     (&32, y)
  |     -------- returning this value requires that `*x` is borrowed for `'1`
5 | }
  | - return type of generator is (&'1 i32, &i32)

The '1 lifetime name should be given to the second reference in the tuple.

@rustbot modify labels: C-bug A-async-await A-diagnostics A-lifetimes T-compiler

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

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.D-incorrectDiagnostics: A diagnostic that is giving misleading or incorrect information.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