You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
async-block diagnostics do not suggest async move when it may be needed #66107
//* f hits lifetime errorsfnf(a:&usize) -> (impl'_ + Future<Output = ()>,&usize){(async{*a;}, a)}// */// g compiles as expectedfng(a:&usize) -> (impl'_ + Future<Output = ()>,&usize){asyncfnh(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
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.
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
Activity
nikomatsakis commentedon 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 borrowingain place -- so when we try to return, it has a reference toaasais 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:
It seems like we should adapt the
async { }case to be closer to the closure wording.nikomatsakis commentedon Nov 12, 2019
Marking this as OnDeck since it seems like this might be a common mistake, and I think the fix should be relatively straightforward.
[-]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[/+]WildCryptoFox commentedon Nov 13, 2019
Ah thanks @nikomatsakis this makes sense.
gilescope commentedon Dec 10, 2019
@rustbot claim
tmandry commentedon Mar 3, 2020
Ping from triage. @gilescope are you working on this?
tmandry commentedon Mar 17, 2020
Ping. @gilescope are you interested in continuing to work on this, or should I un-assign?
nikomatsakis commentedon Mar 31, 2020
@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 commentedon Apr 3, 2020
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
gizmondo commentedon Apr 7, 2020
@rustbot claim
nikomatsakis commentedon Apr 8, 2020
@gizmondo thanks!!
Rollup merge of rust-lang#70906 - gizmondo:66107, r=estebank