Skip to content

Remove fake borrows of refs that are converted into non-refs in MakeByMoveBody #138514

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Merged
merged 1 commit into from
Mar 15, 2025

Conversation

compiler-errors
Copy link
Member

@compiler-errors compiler-errors commented Mar 14, 2025

Remove fake borrows of closure captures if that capture has been replaced with a by-move version of that capture.

For example, given an async closure that looks like:

let f: Foo;
let c = async move || {
    match f { ... }
};

... in this pair of coroutine-closure + coroutine, we capture Foo in the parent and &Foo in the child. We will emit two fake borrows like:

_2 = &fake shallow (*(_1.0: &Foo));
_3 = &fake shallow (_1.0: &Foo);

However, since the by-move-body transform is responsible for replacing _1.0: &Foo with _1.0: Foo (since the AsyncFnOnce coroutine will own Foo by value), that makes the second fake borrow obsolete since we never have an upvar of type &Foo, and we should replace it with a nop.

As a side-note, we don't actually even care about fake borrows here at all since they're fully a MIR borrowck artifact, and we don't need to borrowck by-move MIR bodies. But it's best to preserve as much as we can between these two bodies :)

Fixes #138501

r? oli-obk

@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 Mar 14, 2025
@rustbot
Copy link
Collaborator

rustbot commented Mar 14, 2025

Some changes occurred to MIR optimizations

cc @rust-lang/wg-mir-opt

@compiler-errors compiler-errors force-pushed the fake-borrow-ref-to-value branch from 621bb79 to e54bde6 Compare March 14, 2025 19:38
@oli-obk
Copy link
Contributor

oli-obk commented Mar 15, 2025

@bors r+ rollup

@bors
Copy link
Collaborator

bors commented Mar 15, 2025

📌 Commit e54bde6 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 Mar 15, 2025
bors added a commit to rust-lang-ci/rust that referenced this pull request Mar 15, 2025
…iaskrgr

Rollup of 5 pull requests

Successful merges:

 - rust-lang#138283 (Enforce type of const param correctly in MIR typeck)
 - rust-lang#138439 (feat: check ARG_MAX on Unix platforms)
 - rust-lang#138502 (resolve: Avoid some unstable iteration)
 - rust-lang#138514 (Remove fake borrows of refs that are converted into non-refs in `MakeByMoveBody`)
 - rust-lang#138524 (Mark myself as unavailable for reviews temporarily)

r? `@ghost`
`@rustbot` modify labels: rollup
@bors bors merged commit 81ba557 into rust-lang:master Mar 15, 2025
6 checks passed
@rustbot rustbot added this to the 1.87.0 milestone Mar 15, 2025
rust-timer added a commit to rust-lang-ci/rust that referenced this pull request Mar 15, 2025
Rollup merge of rust-lang#138514 - compiler-errors:fake-borrow-ref-to-value, r=oli-obk

Remove fake borrows of refs that are converted into non-refs in `MakeByMoveBody`

Remove fake borrows of closure captures if that capture has been replaced with a by-move version of that capture.

For example, given an async closure that looks like:

```
let f: Foo;
let c = async move || {
    match f { ... }
};
```

... in this pair of coroutine-closure + coroutine, we capture `Foo` in the parent and `&Foo` in the child. We will emit two fake borrows like:

```
_2 = &fake shallow (*(_1.0: &Foo));
_3 = &fake shallow (_1.0: &Foo);
```

However, since the by-move-body transform is responsible for replacing `_1.0: &Foo` with `_1.0: Foo` (since the `AsyncFnOnce` coroutine will own `Foo` by value), that makes the second fake borrow obsolete since we never have an upvar of type `&Foo`, and we should replace it with a `nop`.

As a side-note, we don't actually even care about fake borrows here at all since they're fully a MIR borrowck artifact, and we don't need to borrowck by-move MIR bodies. But it's best to preserve as much as we can between these two bodies :)

Fixes rust-lang#138501

r? oli-obk
# for free to join this conversation on GitHub. Already have an account? # 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.

by_move_body.rs:275:21: There should be at least a single deref for an upvar local initialization, found []
4 participants