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

Skip over args when determining if async-closure's inner coroutine consumes its upvars #128520

Merged
merged 1 commit into from
Aug 8, 2024

Conversation

compiler-errors
Copy link
Member

@compiler-errors compiler-errors commented Aug 1, 2024

#125306 implements a strategy for when we have an async move || async-closure that is inferred to be async FnOnce, it will force the inner coroutine to also be move, since we cannot borrow any upvars from the parent async-closure (since FnOnce is not lending):

// There are several curious situations with coroutine-closures where
// analysis is too aggressive with borrows when the coroutine-closure is
// marked `move`. Specifically:
//
// 1. If the coroutine-closure was inferred to be `FnOnce` during signature
// inference, then it's still possible that we try to borrow upvars from
// the coroutine-closure because they are not used by the coroutine body
// in a way that forces a move. See the test:
// `async-await/async-closures/force-move-due-to-inferred-kind.rs`.
//
// 2. If the coroutine-closure is forced to be `FnOnce` due to the way it
// uses its upvars, but not *all* upvars would force the closure to `FnOnce`.
// See the test: `async-await/async-closures/force-move-due-to-actually-fnonce.rs`.
//
// This would lead to an impossible to satisfy situation, since `AsyncFnOnce`
// coroutine bodies can't borrow from their parent closure. To fix this,
// we force the inner coroutine to also be `move`. This only matters for
// coroutine-closures that are `move` since otherwise they themselves will
// be borrowing from the outer environment, so there's no self-borrows occuring.

However, when this strategy was implemented, it reused the ExprUseVisitor data from visiting the whole coroutine, which includes additional statements due to async-specific argument desugaring:

// Async function parameters are lowered into the closure body so that they are
// captured and so that the drop order matches the equivalent non-async functions.
//
// from:
//
// async fn foo(<pattern>: <ty>, <pattern>: <ty>, <pattern>: <ty>) {
// <body>
// }
//
// into:
//
// fn foo(__arg0: <ty>, __arg1: <ty>, __arg2: <ty>) {
// async move {
// let __arg2 = __arg2;
// let <pattern> = __arg2;
// let __arg1 = __arg1;
// let <pattern> = __arg1;
// let __arg0 = __arg0;
// let <pattern> = __arg0;
// drop-temps { <body> } // see comments later in fn for details
// }
// }
//
// If `<pattern>` is a simple ident, then it is lowered to a single
// `let <pattern> = <pattern>;` statement as an optimization.
//
// Note that the body is embedded in `drop-temps`; an
// equivalent desugaring would be `return { <body>
// };`. The key point is that we wish to drop all the
// let-bound variables and temporaries created in the body
// (and its tail expression!) before we drop the
// parameters (c.f. rust-lang/rust#64512).

Well, it turns out that we don't care about these argument desugaring parameters, because arguments to the async-closure are not the async-closure's captures -- they exist for only one invocation of the closure, and they're always consumed by construction (see the argument desugaring above), so they will force the coroutine's inferred kind to FnOnce. (Unless they're Copy, because we never consider Copy types to be consumed):

/// The value found at `place` is being copied.
/// `diag_expr_id` is the id used for diagnostics (see `consume` for more details).
fn copy(&mut self, place_with_id: &PlaceWithHirId<'tcx>, diag_expr_id: HirId) {
// In most cases, copying data from `x` is equivalent to doing `*&x`, so by default
// we treat a copy of `x` as a borrow of `x`.
self.borrow(place_with_id, diag_expr_id, ty::BorrowKind::ImmBorrow)
}

However, since we were visiting these arg exprs, this resulted in us too-aggressively applying move to the inner coroutine, resulting in regressions. For example, this PR fixes #128516. Fascinatingly, the note above about how we never consume Copy types is why this only regressed when the argument types weren't all Copy.

I tried to leave some comments inline to make this more clear :)

@rustbot
Copy link
Collaborator

rustbot commented Aug 1, 2024

r? @BoxyUwU

rustbot has assigned @BoxyUwU.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Aug 1, 2024
@compiler-errors compiler-errors changed the title Skip over args when determining if coroutine-closure's inner coroutine consumes its upvars Skip over args when determining if async-closure's inner coroutine consumes its upvars Aug 1, 2024
Comment on lines +9 to +11
fn wrapper(f: impl Fn(String)) -> impl async Fn(String) {
async move |s| f(s)
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So, the closure captures f by value, and then the future captures the upvar by ref?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep.

@BoxyUwU
Copy link
Member

BoxyUwU commented Aug 8, 2024

r=me unless I'm wrong about why that code compiles lol

@BoxyUwU
Copy link
Member

BoxyUwU commented Aug 8, 2024

@bors r+ rollup

@bors
Copy link
Contributor

bors commented Aug 8, 2024

📌 Commit 5138586 has been approved by BoxyUwU

It is now in the queue for this repository.

@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 Aug 8, 2024
bors added a commit to rust-lang-ci/rust that referenced this pull request Aug 8, 2024
Rollup of 7 pull requests

Successful merges:

 - rust-lang#128520 (Skip over args when determining if async-closure's inner coroutine consumes its upvars)
 - rust-lang#128552 (Emit an error for invalid use of the `#[no_sanitize]` attribute)
 - rust-lang#128691 (Update `compiler-builtins` to 0.1.117)
 - rust-lang#128702 (Add -Zmetrics-dir=PATH to save diagnostic metadata to disk)
 - rust-lang#128797 (Fuchsia Test Runner: enable ffx repository server)
 - rust-lang#128798 (refactor(rustc_expand::mbe): Don't require full ExtCtxt when not necessary)
 - rust-lang#128800 (Add tracking issue to core-pattern-type)

r? `@ghost`
`@rustbot` modify labels: rollup
@bors bors merged commit 2a177c2 into rust-lang:master Aug 8, 2024
6 checks passed
@rustbot rustbot added this to the 1.82.0 milestone Aug 8, 2024
rust-timer added a commit to rust-lang-ci/rust that referenced this pull request Aug 8, 2024
Rollup merge of rust-lang#128520 - compiler-errors:more-precisely-force-move, r=BoxyUwU

Skip over args when determining if async-closure's inner coroutine consumes its upvars

rust-lang#125306 implements a strategy for when we have an `async move ||` async-closure that is inferred to be `async FnOnce`, it will force the inner coroutine to also be `move`, since we cannot borrow any upvars from the parent async-closure (since `FnOnce` is not lending):

https://github.com/rust-lang/rust/blob/8e86c9567154dc5a9ada15ab196d23eae2bd7d89/compiler/rustc_hir_typeck/src/upvar.rs#L211-L229

However, when this strategy was implemented, it reused the `ExprUseVisitor` data from visiting the whole coroutine, which includes additional statements due to `async`-specific argument desugaring:

https://github.com/rust-lang/rust/blob/8e86c9567154dc5a9ada15ab196d23eae2bd7d89/compiler/rustc_ast_lowering/src/item.rs#L1197-L1228

Well, it turns out that we don't care about these argument desugaring parameters, because arguments to the async-closure are not the *async-closure*'s captures -- they exist for only one invocation of the closure, and they're always consumed by construction (see the argument desugaring above), so they will force the coroutine's inferred kind to `FnOnce`. (Unless they're `Copy`, because we never consider `Copy` types to be consumed):

https://github.com/rust-lang/rust/blob/8e86c9567154dc5a9ada15ab196d23eae2bd7d89/compiler/rustc_hir_typeck/src/expr_use_visitor.rs#L60-L66

However, since we *were* visiting these arg exprs, this resulted in us too-aggressively applying `move` to the inner coroutine, resulting in regressions. For example, this PR fixes rust-lang#128516. Fascinatingly, the note above about how we never consume `Copy` types is why this only regressed when the argument types weren't all `Copy`.

I tried to leave some comments inline to make this more clear :)
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. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

async move closure owning Fn and calling it
4 participants