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

[wg-async-await] Drop async fn arguments in async block #59823

Merged
merged 10 commits into from
Apr 23, 2019

Conversation

davidtwco
Copy link
Member

Fixes #54716.

This PR modifies the HIR lowering (and some other places to make this work) so that unused arguments to a async function are always dropped inside the async move block and not at the end of the function body.

async fn foo(<pattern>: <type>) {
  async move {
  }
} // <-- dropped as you "exit" the fn

// ...becomes...
fn foo(__arg0: <ty>) {
  async move {
    let <pattern>: <ty> = __arg0;
  } // <-- dropped as you "exit" the async block
}

However, the exact ordering of drops is not the same as a regular function, as visible in this playground example - I believe this to be an unrelated issue. There is a Zulip topic for this.

r? @cramertj
cc @nikomatsakis

@rust-highfive rust-highfive added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Apr 9, 2019
@rust-highfive

This comment has been minimized.

@davidtwco
Copy link
Member Author

I believe I've resolved the test failures with this PR.

@rust-highfive

This comment has been minimized.

@bors

This comment has been minimized.

This tests that async functions drop parameters in the same order as
regular functions.
This will be used to keep track of the origin of a local in the AST. In
particular, it will be used by `async fn` lowering for the locals in
`let <pat>: <ty> = __arg0;` statements.
@rust-highfive

This comment has been minimized.

@davidtwco
Copy link
Member Author

I've updated the test to match what Travis expects, however it isn't the same as I've been seeing locally.

@rust-highfive

This comment has been minimized.

@rust-highfive

This comment has been minimized.

This commit adds an `AsyncArgument` struct to the AST that contains the
generated argument and statement that will be used in HIR lowering, name
resolution and def collection.
This commit takes advantage of `AsyncArgument` type that was added in a
previous commit to replace the arguments of the `async fn` in the HIR
and add statements to move the bindings from the new arguments to the
pattern from the old argument.

For example, the async function `foo` below:

    async fn foo((x, _y): (T, V)) {
        async move {
        }
    }

becomes:

    async fn foo(__arg0: (T, V)) {
        async move {
            let (x, _y) = __arg0;
        }
    }
This commit extends the previous commit to apply to trait methods as
well as free functions.
This avoids issues with `impl_trait_in_bindings` as the type from the
argument is normally used as the let binding, but `impl Trait` is
unstable in binding position.
This commit introduces an `ArgSource` enum that is lowered into the HIR
so that diagnostics can correctly refer to the argument pattern's
original name rather than the generated pattern.
This commit changes the order that arguments and bodies of async
functions are lowered so that when the body attempts to `lower_def` of a
upvar then the id has already been assigned by lowering the argument
first.
This commit displays the original pattern in generated documentation for
async functions rather than the synthesized pattern.
@cramertj
Copy link
Member

r=me with test fixups. This looks great, thanks so much!

This commit introduces a `assert_drop_order_after_poll` helper function
to the test case for this case to reduce repetitive noise and documents
what each function aims to test.
@cramertj
Copy link
Member

@bors r+

@bors
Copy link
Contributor

bors commented Apr 23, 2019

📌 Commit 119e67a has been approved by cramertj

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Apr 23, 2019
Centril added a commit to Centril/rust that referenced this pull request Apr 23, 2019
[wg-async-await] Drop `async fn` arguments in async block

Fixes rust-lang#54716.

This PR modifies the HIR lowering (and some other places to make this work) so that unused arguments to a async function are always dropped inside the async move block and not at the end of the function body.

```
async fn foo(<pattern>: <type>) {
  async move {
  }
} // <-- dropped as you "exit" the fn

// ...becomes...
fn foo(__arg0: <ty>) {
  async move {
    let <pattern>: <ty> = __arg0;
  } // <-- dropped as you "exit" the async block
}
```

However, the exact ordering of drops is not the same as a regular function, [as visible in this playground example](https://play.rust-lang.org/?version=stable&mode=debug&edition=2015&gist=be39af1a58e5d430be1eb3c722cb1ec3) - I believe this to be an unrelated issue. There is a [Zulip topic](https://rust-lang.zulipchat.com/#narrow/stream/187312-t-compiler.2Fwg-async-await/topic/.2354716.20drop.20order) for this.

r? @cramertj
cc @nikomatsakis
@bors
Copy link
Contributor

bors commented Apr 23, 2019

⌛ Testing commit 119e67a with merge 658f439060c837d65724c5902d1f6b3e89239214...

Centril added a commit to Centril/rust that referenced this pull request Apr 23, 2019
[wg-async-await] Drop `async fn` arguments in async block

Fixes rust-lang#54716.

This PR modifies the HIR lowering (and some other places to make this work) so that unused arguments to a async function are always dropped inside the async move block and not at the end of the function body.

```
async fn foo(<pattern>: <type>) {
  async move {
  }
} // <-- dropped as you "exit" the fn

// ...becomes...
fn foo(__arg0: <ty>) {
  async move {
    let <pattern>: <ty> = __arg0;
  } // <-- dropped as you "exit" the async block
}
```

However, the exact ordering of drops is not the same as a regular function, [as visible in this playground example](https://play.rust-lang.org/?version=stable&mode=debug&edition=2015&gist=be39af1a58e5d430be1eb3c722cb1ec3) - I believe this to be an unrelated issue. There is a [Zulip topic](https://rust-lang.zulipchat.com/#narrow/stream/187312-t-compiler.2Fwg-async-await/topic/.2354716.20drop.20order) for this.

r? @cramertj
cc @nikomatsakis
Centril added a commit to Centril/rust that referenced this pull request Apr 23, 2019
[wg-async-await] Drop `async fn` arguments in async block

Fixes rust-lang#54716.

This PR modifies the HIR lowering (and some other places to make this work) so that unused arguments to a async function are always dropped inside the async move block and not at the end of the function body.

```
async fn foo(<pattern>: <type>) {
  async move {
  }
} // <-- dropped as you "exit" the fn

// ...becomes...
fn foo(__arg0: <ty>) {
  async move {
    let <pattern>: <ty> = __arg0;
  } // <-- dropped as you "exit" the async block
}
```

However, the exact ordering of drops is not the same as a regular function, [as visible in this playground example](https://play.rust-lang.org/?version=stable&mode=debug&edition=2015&gist=be39af1a58e5d430be1eb3c722cb1ec3) - I believe this to be an unrelated issue. There is a [Zulip topic](https://rust-lang.zulipchat.com/#narrow/stream/187312-t-compiler.2Fwg-async-await/topic/.2354716.20drop.20order) for this.

r? @cramertj
cc @nikomatsakis
@Centril
Copy link
Contributor

Centril commented Apr 23, 2019

@bors retry

bors added a commit that referenced this pull request Apr 23, 2019
Rollup of 6 pull requests

Successful merges:

 - #59823 ([wg-async-await] Drop `async fn` arguments in async block )
 - #59839 (Warn on unused results for operation methods on nums)
 - #60146 (Update fonts used by rustdoc)
 - #60169 (Warn when ignore-tidy-linelength is present, but no lines are too long)
 - #60177 (Promote rust comments to rustdoc)
 - #60191 (Add f16c target_feature)

Failed merges:

r? @ghost
@rust-lang rust-lang deleted a comment from rust-highfive Apr 23, 2019
@bors bors merged commit 119e67a into rust-lang:master Apr 23, 2019
@davidtwco davidtwco deleted the issue-54716 branch April 24, 2019 19:47
davidtwco pushed a commit to davidtwco/rust that referenced this pull request Jun 3, 2019
Here follows the main reverts applied in order to make this commit:

Revert "Rollup merge of rust-lang#60676 - davidtwco:issue-60674, r=cramertj"

This reverts commit 45b0945, reversing
changes made to f6df1f6.

Revert "Rollup merge of rust-lang#60437 - davidtwco:issue-60236, r=nikomatsakis"

This reverts commit 16939a5, reversing
changes made to 12bf981.

Revert "Rollup merge of rust-lang#59823 - davidtwco:issue-54716, r=cramertj"

This reverts commit 62d1574, reversing
changes made to 4eff852.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Unused arguments to async fn are dropped too early
5 participants