Skip to content

async-block diagnostics do not suggest async move when it may be needed #66107

Closed

Description

@WildCryptoFox

(playground)

//* f hits lifetime errors
fn f(a: &usize) -> (impl '_ + Future<Output = ()>, &usize) {
    (async { *a; }, a)
}
// */

// g compiles as expected
fn g(a: &usize) -> (impl '_ + Future<Output = ()>, &usize) {
    async fn h(a: &usize) { *a; }
    (h(a), a)
}
error[E0515]: cannot return value referencing function parameter `a`
 --> src/main.rs:5:5
  |
5 |     (async { *a; }, a)
  |     ^^^^^^^^^^-^^^^^^^
  |     |         |
  |     |         `a` is borrowed here
  |     returns a value referencing data owned by the current function

This issue has been assigned to @gizmondo via this comment.

Activity

  1. added
    C-bugCategory: This is a bug.
    T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.
    T-langRelevant to the language team
    on Nov 5, 2019
  2. nikomatsakis commented on Nov 12, 2019

    @nikomatsakis
    Contributor

    The error is correct, so what we are looking for here is a diagnostic improvement. The problem is that async { *a }, much like a closure, defaults to borrowing a in place -- so when we try to return, it has a reference to a as a is going out of scope.

    To fix the error, you need async move (playground).

    The situation is exactly analogous to closures and move closures. Note that in the analogous closure example (playground), we give a different error, complete with a suggestion:

    error[E0373]: closure may outlive the current function, but it borrows `a`, which is owned by the current function
     --> src/main.rs:5:6
      |
    5 |     (|| { *a; }, a)
      |      ^^    - `a` is borrowed here
      |      |
      |      may outlive borrowed value `a`
      |
    note: closure is returned here
     --> src/main.rs:5:5
      |
    5 |     (|| { *a; }, a)
      |     ^^^^^^^^^^^^^^^
    help: to force the closure to take ownership of `a` (and any other referenced variables), use the `move` keyword
      |
    5 |     (move || { *a; }, a)
      |      ^^^^^^^
    

    It seems like we should adapt the async { } case to be closer to the closure wording.

  3. nikomatsakis commented on Nov 12, 2019

    @nikomatsakis
    Contributor

    Marking this as OnDeck since it seems like this might be a common mistake, and I think the fix should be relatively straightforward.

  4. changed the title [-]async-block lifetime issue with shared reference in the return type (async-fn works as expected)[/-] [+]async-block diagnostics do not suggest async move when it may be needed[/+] on Nov 12, 2019
  5. WildCryptoFox commented on Nov 13, 2019

    @WildCryptoFox
    Author

    Ah thanks @nikomatsakis this makes sense.

  6. added
    C-enhancementCategory: An issue proposing an enhancement or a PR with one.
    and removed
    C-bugCategory: This is a bug.
    on Nov 13, 2019
  7. gilescope commented on Dec 10, 2019

    @gilescope
    Contributor

    @rustbot claim

  8. tmandry commented on Mar 3, 2020

    @tmandry
    Member

    Ping from triage. @gilescope are you working on this?

  9. tmandry commented on Mar 17, 2020

    @tmandry
    Member

    Ping. @gilescope are you interested in continuing to work on this, or should I un-assign?

  10. nikomatsakis commented on Mar 31, 2020

    @nikomatsakis
    Contributor

    @rustbot claim

    I'm going to "claim" this issue, but only to write mentoring instructions! If anybody is interested in taking a crack at it, I'd be happy to help.

  11. gilescope commented on Apr 3, 2020

    @gilescope
    Contributor

    Sorry - if anyone else wants it that’s cool. I seem to have fingers in quite a few pies at the moment.

    Sent with GitHawk

  12. gizmondo commented on Apr 7, 2020

    @gizmondo
    Contributor

    @rustbot claim

  13. nikomatsakis commented on Apr 8, 2020

    @nikomatsakis
    Contributor

    @gizmondo thanks!!

  14. added a commit that references this issue on Apr 8, 2020
    6f8fc4d
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 lintsAsyncAwait-TriagedAsync-await issues that have been triaged during a working group meeting.C-enhancementCategory: An issue proposing an enhancement or a PR with one.P-mediumMedium priorityT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.T-langRelevant to the language team

Type

No type

Projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions