-
Notifications
You must be signed in to change notification settings - Fork 13.2k
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
Variables not always moved into async move
block
#78633
Comments
async move
blockasync move
block
Slightly minimized (playground): async fn f() {
let arena = 0;
async move { &arena }.await;
}
|
Actually that's not quite right because it returns a reference. I think https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=51d3946643a798e189b33af33ec4e88f is closer. |
I think the issue is that this is a self-referential struct: the future stores both |
Yeah, if you remove |
That works because if you I think this fix to my initial playground, adding Edit: |
It breaks without drop, too, that was just to make it more clear. I guess it didn't work 😆
That's because you're pushing to a new, non-captured variable now - it's no longer stored in the future. If you capture after the push it still breaks: https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=4d3a959ef9a3cae41bcde778822bedda |
The errors are correct but confusing. In the |
Er, I explained that slightly wrong. For the |
This also isn't specific to async, we can see the same behavior with regular closures: fn main() {
let arena = 0;
let mut track = Vec::new();
(move || {
//let mut track = track;
track.push(&arena)
})();
} in this case uncommenting the commented line, or removing
is different from the error message in the async case:
...and honestly, I think both of them could use some improvement. The async one actually seems a little better to me personally. |
As discussed in the wg-async-foundations meeting, we could support this in the async case at least, since async generators are allowed to be self-borrowing. But since we handle their captures the same as we handle closures, we don't support it today. |
Playground:
rustc v1.47.0 stable
Error:
Which is because
track
is captured by reference and not by value, whilearena
is captured by value.By submitting this issue, I assume that the intended semantics for
move
is:which would make this a bug.
The text was updated successfully, but these errors were encountered: