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-block diagnostics do not suggest async move when it may be needed #66107

Closed
WildCryptoFox opened this issue Nov 5, 2019 · 10 comments · Fixed by #70906
Closed

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

WildCryptoFox opened this issue Nov 5, 2019 · 10 comments · Fixed by #70906
Assignees
Labels
A-async-await Area: Async & Await A-diagnostics Area: Messages for errors, warnings, and lints AsyncAwait-Triaged Async-await issues that have been triaged during a working group meeting. C-enhancement Category: An issue proposing an enhancement or a PR with one. P-medium Medium priority T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-lang Relevant to the language team, which will review and decide on the PR/issue.

Comments

@WildCryptoFox
Copy link
Contributor

WildCryptoFox commented Nov 5, 2019

(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.

@jonas-schievink jonas-schievink added A-async-await Area: Async & Await C-bug Category: This is a bug. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-lang Relevant to the language team, which will review and decide on the PR/issue. labels Nov 5, 2019
@nikomatsakis
Copy link
Contributor

nikomatsakis commented Nov 12, 2019

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.

@nikomatsakis nikomatsakis added AsyncAwait-OnDeck AsyncAwait-Triaged Async-await issues that have been triaged during a working group meeting. labels Nov 12, 2019
@nikomatsakis
Copy link
Contributor

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

@nikomatsakis nikomatsakis added the A-diagnostics Area: Messages for errors, warnings, and lints label Nov 12, 2019
@nikomatsakis nikomatsakis 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 Nov 12, 2019
@WildCryptoFox
Copy link
Contributor Author

Ah thanks @nikomatsakis this makes sense.

@jonas-schievink jonas-schievink added C-enhancement Category: An issue proposing an enhancement or a PR with one. and removed C-bug Category: This is a bug. labels Nov 13, 2019
@gilescope
Copy link
Contributor

@rustbot claim

@tmandry tmandry added this to To do in wg-async work Feb 11, 2020
@tmandry tmandry moved this from To do to In progress in wg-async work Mar 3, 2020
@tmandry
Copy link
Member

tmandry commented Mar 3, 2020

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

@tmandry tmandry moved this from In progress to Assigned in wg-async work Mar 3, 2020
@tmandry
Copy link
Member

tmandry commented Mar 17, 2020

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

@tmandry tmandry moved this from Claimed to To do in wg-async work Mar 24, 2020
@tmandry tmandry added the P-medium Medium priority label Mar 24, 2020
@nikomatsakis
Copy link
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.

@gilescope
Copy link
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

@tmandry tmandry moved this from On deck to Claimed in wg-async work Apr 7, 2020
@gizmondo
Copy link
Contributor

gizmondo commented Apr 7, 2020

@rustbot claim

@rustbot rustbot assigned rustbot and unassigned nikomatsakis Apr 7, 2020
@nikomatsakis
Copy link
Contributor

@gizmondo thanks!!

@tmandry tmandry moved this from Claimed to In progress in wg-async work Apr 9, 2020
@bors bors closed this as completed in 6f8fc4d Apr 9, 2020
wg-async work automation moved this from In progress to Done Apr 9, 2020
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 A-diagnostics Area: Messages for errors, warnings, and lints AsyncAwait-Triaged Async-await issues that have been triaged during a working group meeting. C-enhancement Category: An issue proposing an enhancement or a PR with one. P-medium Medium priority T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-lang Relevant to the language team, which will review and decide on the PR/issue.
Projects
Archived in project
Development

Successfully merging a pull request may close this issue.

7 participants