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

Make the computation of coroutine_captures_by_ref_ty more sophisticated #123660

Merged
merged 2 commits into from
Apr 11, 2024

Conversation

compiler-errors
Copy link
Member

@compiler-errors compiler-errors commented Apr 9, 2024

Currently, we treat all the by-(mut/)ref borrows of a coroutine-closure as having a "closure env" borrowed lifetime.

When we have the given code:

let x: &'a i32 = ...;
let c = async || {
    let _x = *x;
};

Then when we call:

c()
// which, because `AsyncFn` takes a `&self`, we insert an autoref:
(&c /* &'env {coroutine-closure} */)()

We will return a future whose captures contain &'env i32 instead of &'a i32, which is way more restrictive than necessary. We should be able to drop c while the future is alive since it's not actually borrowing any data originating from within the closure's captures, but since the capture has that 'env lifetime, this is not possible.

This wouldn't be true, for example, if the closure captured i32 instead of &'a i32, because the 'env lifetime is actually necessary since the data (i32) is owned by the closure.

This PR identifies two criteria where we need to take the borrow with the closure env lifetime:

  1. If the closure borrows data from inside the closure's captures. This is not true if the parent capture is by-ref, OR if the parent capture is by-move and the child capture begins with a deref projection. This is the example described above.
  2. If we're dealing with mutable references, since we cannot reborrow &'env mut &'a mut i32 into &'a mut i32, only &'env mut i32.

See the documentation on should_reborrow_from_env_of_parent_coroutine_closure for more info.

important: As disclaimer states on that function, luckily, if this heuristic is not correct, then the program is not unsound, since we still borrowck and validate the choices made from this function -- the only side-effect is that the user may receive unnecessary borrowck errors.

Fixes #123241

@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 Apr 9, 2024
@rust-log-analyzer

This comment has been minimized.

}

field_used_at_least_once = true;
break for_each((parent_field_idx, parent_capture), (child_field_idx, child_capture));
Copy link
Member Author

Choose a reason for hiding this comment

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

The only thing this fails to do is assert that the parents captures are all used at least once. I'm tempted to use a coroutine for this and just use iter::from_coroutine...

@rust-log-analyzer

This comment has been minimized.

@bors
Copy link
Contributor

bors commented Apr 10, 2024

☔ The latest upstream changes (presumably #123708) made this pull request unmergeable. Please resolve the merge conflicts.

@compiler-errors compiler-errors force-pushed the coroutine-closure-env branch 2 times, most recently from 3784660 to c5efc2d Compare April 10, 2024 15:50
@compiler-errors compiler-errors marked this pull request as ready for review April 10, 2024 16:14
@rustbot
Copy link
Collaborator

rustbot commented Apr 10, 2024

Some changes occurred to MIR optimizations

cc @rust-lang/wg-mir-opt

@compiler-errors
Copy link
Member Author

r? oli-obk

@rust-log-analyzer

This comment has been minimized.

@bors
Copy link
Contributor

bors commented Apr 10, 2024

☔ The latest upstream changes (presumably #123725) made this pull request unmergeable. Please resolve the merge conflicts.

@oli-obk
Copy link
Contributor

oli-obk commented Apr 11, 2024

Nice (and great docs, thanks!)

@bors r+ rollup

@bors
Copy link
Contributor

bors commented Apr 11, 2024

📌 Commit 599d456 has been approved by oli-obk

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 Apr 11, 2024
bors added a commit to rust-lang-ci/rust that referenced this pull request Apr 11, 2024
…iaskrgr

Rollup of 4 pull requests

Successful merges:

 - rust-lang#123660 (Make the computation of `coroutine_captures_by_ref_ty` more sophisticated)
 - rust-lang#123738 (Call lower_const_param instead of duplicating the code)
 - rust-lang#123774 (Fix typo MaybeUnit -> MaybeUninit)
 - rust-lang#123790 (correct the handling of `bootstrap-cache-path` option)

r? `@ghost`
`@rustbot` modify labels: rollup
@bors bors merged commit ee73660 into rust-lang:master Apr 11, 2024
11 checks passed
@rustbot rustbot added this to the 1.79.0 milestone Apr 11, 2024
rust-timer added a commit to rust-lang-ci/rust that referenced this pull request Apr 11, 2024
Rollup merge of rust-lang#123660 - compiler-errors:coroutine-closure-env, r=oli-obk

Make the computation of `coroutine_captures_by_ref_ty` more sophisticated

Currently, we treat all the by-(mut/)ref borrows of a coroutine-closure as having a "closure env" borrowed lifetime.

When we have the given code:
```rust
let x: &'a i32 = ...;
let c = async || {
    let _x = *x;
};
```

Then when we call:
```rust
c()
// which, because `AsyncFn` takes a `&self`, we insert an autoref:
(&c /* &'env {coroutine-closure} */)()
```

We will return a future whose captures contain `&'env i32` instead of `&'a i32`, which is way more restrictive than necessary. We should be able to drop `c` while the future is alive since it's not actually borrowing any data *originating from within* the closure's captures, but since the capture has that `'env` lifetime, this is not possible.

This wouldn't be true, for example, if the closure captured `i32` instead of `&'a i32`, because the `'env` lifetime is actually *necessary* since the data (`i32`) is owned by the closure.

This PR identifies two criteria where we *need* to take the borrow with the closure env lifetime:
1. If the closure borrows data from inside the closure's captures. This is not true if the parent capture is by-ref, OR if the parent capture is by-move and the child capture begins with a deref projection. This is the example described above.
2. If we're dealing with mutable references, since we cannot reborrow `&'env mut &'a mut i32` into `&'a mut i32`, *only* `&'env mut i32`.

See the documentation on `should_reborrow_from_env_of_parent_coroutine_closure` for more info.

**important:** As disclaimer states on that function, luckily, if this heuristic is not correct, then the program is not unsound, since we still borrowck and validate the choices made from this function -- the only side-effect is that the user may receive unnecessary borrowck errors.

Fixes rust-lang#123241
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this pull request May 22, 2024
… r=oli-obk

An async closure may implement `FnMut`/`Fn` if it has no self-borrows

There's no reason that async closures may not implement `FnMut` or `Fn` if they don't actually borrow anything with the closure's env lifetime. Specifically, rust-lang#123660 made it so that we don't always need to borrow captures from the closure's env.

See the doc comment on `should_reborrow_from_env_of_parent_coroutine_closure`:

https://github.com/rust-lang/rust/blob/c00957a3e269219413041a4e3565f33b1f9d0779/compiler/rustc_hir_typeck/src/upvar.rs#L1777-L1823

If there are no such borrows, then we are free to implement `FnMut` and `Fn` as permitted by our closure's inferred `ClosureKind`.

As far as I can tell, this change makes `async || {}` work in precisely the set of places they used to work before rust-lang#120361.
Fixes rust-lang#125247.

r? oli-obk
fmease added a commit to fmease/rust that referenced this pull request May 22, 2024
… r=oli-obk

An async closure may implement `FnMut`/`Fn` if it has no self-borrows

There's no reason that async closures may not implement `FnMut` or `Fn` if they don't actually borrow anything with the closure's env lifetime. Specifically, rust-lang#123660 made it so that we don't always need to borrow captures from the closure's env.

See the doc comment on `should_reborrow_from_env_of_parent_coroutine_closure`:

https://github.com/rust-lang/rust/blob/c00957a3e269219413041a4e3565f33b1f9d0779/compiler/rustc_hir_typeck/src/upvar.rs#L1777-L1823

If there are no such borrows, then we are free to implement `FnMut` and `Fn` as permitted by our closure's inferred `ClosureKind`.

As far as I can tell, this change makes `async || {}` work in precisely the set of places they used to work before rust-lang#120361.
Fixes rust-lang#125247.

r? oli-obk
rust-timer added a commit to rust-lang-ci/rust that referenced this pull request May 22, 2024
Rollup merge of rust-lang#125259 - compiler-errors:fn-mut-as-a-treat, r=oli-obk

An async closure may implement `FnMut`/`Fn` if it has no self-borrows

There's no reason that async closures may not implement `FnMut` or `Fn` if they don't actually borrow anything with the closure's env lifetime. Specifically, rust-lang#123660 made it so that we don't always need to borrow captures from the closure's env.

See the doc comment on `should_reborrow_from_env_of_parent_coroutine_closure`:

https://github.com/rust-lang/rust/blob/c00957a3e269219413041a4e3565f33b1f9d0779/compiler/rustc_hir_typeck/src/upvar.rs#L1777-L1823

If there are no such borrows, then we are free to implement `FnMut` and `Fn` as permitted by our closure's inferred `ClosureKind`.

As far as I can tell, this change makes `async || {}` work in precisely the set of places they used to work before rust-lang#120361.
Fixes rust-lang#125247.

r? oli-obk
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 closures are not allowed to reference all captured lifetimes if one of them is invariant
5 participants