Skip to content

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

Closed

Description

@SNCPlay42
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

Activity

  1. added
    A-diagnosticsArea: Messages for errors, warnings, and lints
    A-lifetimesArea: Lifetimes / regions
    C-bugCategory: This is a bug.
    T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.
    on Aug 21, 2020
  2. SNCPlay42 commented on Aug 21, 2020

    @SNCPlay42
    ContributorAuthor

    On stable the output is different:

    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
    

    Per bisect-rustc, the commit that changed the output is f781bab - almost definitely #73806. @Aaron1011 is there something that can be done to improve this case?

    Bisect output

    searched nightlies: from nightly-2020-06-01 to nightly-2020-07-15
    regressed nightly: nightly-2020-07-02
    searched commits: from 16957bd to f781bab
    regressed commit: f781bab

    bisected with cargo-bisect-rustc v0.5.2

    Host triple: x86_64-unknown-linux-gnu
    Reproduce with:

    cargo bisect-rustc --regress success --script ./test.sh --start 2020-06-01 --end 2020-07-15 --preserve 
  3. Aaron1011 commented on Aug 21, 2020

    @Aaron1011
    Contributor

    Here's the output with async removed:

    pub fn async_fn(x: &mut i32) -> (&i32, &i32) {
        let y = &*x;
        *x += 1;
        (&32, y)
    }
    error[E0506]: cannot assign to `*x` because it is borrowed
     --> src/lib.rs:3:5
      |
    1 | pub fn async_fn(x: &mut i32) -> (&i32, &i32) {
      |                    - let's call the lifetime of this reference `'1`
    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`
    

    It looks like we're missing the "let's call the lifetime of this reference" note in the async case.

  4. SNCPlay42 commented on Aug 21, 2020

    @SNCPlay42
    ContributorAuthor

    It looks like we're missing the "let's call the lifetime of this reference" note in the async case.

    The current equivalent of that annotation is the note at the bottom: (see #74072)

    - return type of generator is (&'1 i32, &i32)
    

    I actually ran into this while testing a potential fix for that issue - it would make the output

    LL | pub async fn async_fn(x: &mut i32) -> (&i32, &i32) {
       |                                        - let's call the lifetime of this reference `'1`
    LL |     let y = &*x;
       |             --- borrow of `*x` occurs here
    LL |     *x += 1;
       |     ^^^^^^^ assignment to borrowed `*x` occurs here
    LL |     (&32, y)
       |     -------- returning this value requires that `*x` is borrowed for `'1`
    

    Which still points at the wrong reference.

  5. self-assigned this
    on Aug 21, 2020
  6. added
    D-incorrectDiagnostics: A diagnostic that is giving misleading or incorrect information.
    on Aug 25, 2020
  7. added
    AsyncAwait-TriagedAsync-await issues that have been triaged during a working group meeting.
    on Sep 1, 2020
  8. tmandry commented on Oct 20, 2020

    @tmandry
    Member

    @Aaron1011 Are you still planning to work on this?

  9. added a commit that references this issue on Dec 17, 2020
    419d3ae
  10. added a commit that references this issue on Dec 17, 2020
    720b694
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

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

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions