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

Improve upvar analysis for deref of child capture #138517

Merged
merged 1 commit into from
Mar 17, 2025

Conversation

compiler-errors
Copy link
Member

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

Two fixes to the heuristic I implemented in #123660. As I noted in the code:

Luckily, if this function 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.

This indeed fixes unnecessary borrowck errors.

r? oli-obk


The heuristic is only valid if we deref a &T, not a &mut T or Box<T>, so make sure to check the type. This fixes:

struct Foo { precise: i32 }

fn mut_ref_inside_mut(f: &mut Foo) {
    let x: impl AsyncFn() = async move || {
        let y = &f.precise;
    };
}

Since the capture from f to &f.precise needs to be treated as a lending borrow from the parent coroutine-closure to the child coroutine.


The heuristic is also valid if any deref projection in the child capture's projections is a &T, but we were only looking at the last one. This ensures that this function is considered not to be lending:

struct Foo { precise: i32 }

fn ref_inside_mut(f: &mut &Foo) {
    let x: impl Fn() -> _ = async move || {
        let y = &f.precise;
    };
}

(Specifically, checking that impl Fn() -> _ is satisfied is exercising that the coroutine is not considered to be lending.)

@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
@oli-obk
Copy link
Contributor

oli-obk commented Mar 17, 2025

This was clearly (to me) always intended to work and imo doesn't need an additional FCP. It's just a bugfix for behaviour that was approved along with the original feature. cc @rust-lang/types for being aware of "code that didn't compile before now compiles".

@bors r+

@bors
Copy link
Contributor

bors commented Mar 17, 2025

📌 Commit ae4a479 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 17, 2025
bors added a commit to rust-lang-ci/rust that referenced this pull request Mar 17, 2025
Rollup of 9 pull requests

Successful merges:

 - rust-lang#136355 (Add `*_value` methods to proc_macro lib)
 - rust-lang#137621 (Add std support to cygwin target)
 - rust-lang#137793 (Stablize anonymous pipe)
 - rust-lang#138341 (std: Mention clone-on-write mutation in Arc<T>)
 - rust-lang#138517 (Improve upvar analysis for deref of child capture)
 - rust-lang#138584 (Update Rust Foundation links in Readme)
 - rust-lang#138586 (Document `#![register_tool]`)
 - rust-lang#138590 (Flatten and simplify some control flow 🫓)
 - rust-lang#138592 (update change entry for rust-lang#137147)

r? `@ghost`
`@rustbot` modify labels: rollup
@bors bors merged commit e9f6e01 into rust-lang:master Mar 17, 2025
6 checks passed
@rustbot rustbot added this to the 1.87.0 milestone Mar 17, 2025
rust-timer added a commit to rust-lang-ci/rust that referenced this pull request Mar 17, 2025
Rollup merge of rust-lang#138517 - compiler-errors:better-child-capture, r=oli-obk

Improve upvar analysis for deref of child capture

Two fixes to the heuristic I implemented in rust-lang#123660. As I noted in the code:

> Luckily, if this function 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.

This indeed fixes unnecessary borrowck errors.

r? oli-obk

---

The heuristic is only valid if we deref a `&T`, not a `&mut T` or `Box<T>`, so make sure to check the type. This fixes:

```rust
struct Foo { precise: i32 }

fn mut_ref_inside_mut(f: &mut Foo) {
    let x: impl AsyncFn() = async move || {
        let y = &f.precise;
    };
}
```

Since the capture from `f` to `&f.precise` needs to be treated as a lending borrow from the parent coroutine-closure to the child coroutine.

---

The heuristic is also valid if *any* deref projection in the child capture's projections is a `&T`, but we were only looking at the last one. This ensures that this function is considered not to be lending:

```rust
struct Foo { precise: i32 }

fn ref_inside_mut(f: &mut &Foo) {
    let x: impl Fn() -> _ = async move || {
        let y = &f.precise;
    };
}
```

(Specifically, checking that `impl Fn() -> _` is satisfied is exercising that the coroutine is not considered to be lending.)
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.

4 participants