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 fn drop order for temporaries in return expression does not match sync #64512

Closed
nikomatsakis opened this issue Sep 16, 2019 · 3 comments · Fixed by #64525

Comments

@nikomatsakis
Copy link
Contributor

commented Sep 16, 2019

This asynchronous function:

async fn foo_async(x: D, y: D) {
    helper_async(&D()).await
    //                ^^^ temporary D
}

async fn helper_async(v: &D) { }

will drop first y, then x, then the temporary D. However an equivalent synchronous function (playground) would drop the temporary, then y, then x. The problem is our desugaring for async fn, which looks something like this:

// async fn foo_async($parameter_patterns) { $body }
fn foo_async(raw_parameters) {
  async move {
    let $parameter_patterns = raw-parameters;
    $body // body of the async fn
  }
}

here, the temporaries in $body wind up being dropped after the let-bound variables of the block. Proposed fix in a comment below.

@nikomatsakis

This comment has been minimized.

Copy link
Contributor Author

commented Sep 16, 2019

Proposed fix is to alter the desugaring to:

// async fn foo_async($parameter_patterns) { $body }
fn foo_async(raw_parameters) {
  async move {
    let $parameter_patterns = raw-parameters;
    return $body; // body of the async fn
  }
}

Changing to return $body; (note the semicolon!) will cause any temporaries in the tail expression of the body to be scoped to that final statement, and hence to be dropped before the parameters.

@nikomatsakis

This comment has been minimized.

Copy link
Contributor Author

commented Sep 16, 2019

I've got a branch in my repository that adds a testcase for this:

https://github.com/nikomatsakis/rust/tree/issue-64512-drop-order-tail-temp

@nikomatsakis

This comment has been minimized.

Copy link
Contributor Author

commented Sep 16, 2019

I'm working on a fix, so self-assigning.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
1 participant
You can’t perform that action at this time.