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
Closed

async fn drop order for temporaries in return expression does not match sync #64512

nikomatsakis opened this issue Sep 16, 2019 · 3 comments · Fixed by #64525
Assignees
Labels
A-async-await Area: Async & Await AsyncAwait-Polish Async-await issues that are part of the "polish" area P-high High 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

@nikomatsakis
Copy link
Contributor

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 nikomatsakis added P-high High priority T-lang Relevant to the language team, which will review and decide on the PR/issue. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. A-async-await Area: Async & Await AsyncAwait-Polish Async-await issues that are part of the "polish" area labels Sep 16, 2019
@nikomatsakis
Copy link
Contributor Author

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
Copy link
Contributor Author

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
Copy link
Contributor Author

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
Labels
A-async-await Area: Async & Await AsyncAwait-Polish Async-await issues that are part of the "polish" area P-high High 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
None yet
Development

Successfully merging a pull request may close this issue.

1 participant