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

Fix projections when parent capture is by-ref but child capture is by-value in the ByMoveBody pass #129101

Merged
merged 1 commit into from
Aug 15, 2024

Conversation

compiler-errors
Copy link
Member

This fixes a somewhat strange bug where we build the incorrect MIR in #129074. This one is weird, but I don't expect it to actually matter in practice since it almost certainly results in a move error in borrowck. However, let's not ICE.

Given the code:

#![feature(async_closure)]

// NOT copy.
struct Ty;

fn hello(x: &Ty) {
    let c = async || {
        *x;
        //~^ ERROR cannot move out of `*x` which is behind a shared reference
    };
}

fn main() {}

The parent coroutine-closure captures x: &Ty by-ref, resulting in an upvar of &&Ty. The child coroutine captures x by-value, resulting in an upvar of &Ty. When constructing the by-move body for the coroutine-closure, we weren't applying an additional deref projection to convert the parent capture into the child capture, resulting in an type error in assignment, which is a validation ICE.

As I said above, this only occurs (AFAICT) in code that eventually results in an error, because it is only triggered by HIR that attempts to move a non-copy value out of a ref. This doesn't occur if Ty is Copy, since we'd instead capture x by-ref in the child coroutine.

Fixes #129074

@rustbot
Copy link
Collaborator

rustbot commented Aug 14, 2024

r? @saethlin

rustbot has assigned @saethlin.
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 14, 2024
@rustbot
Copy link
Collaborator

rustbot commented Aug 14, 2024

Some changes occurred to MIR optimizations

cc @rust-lang/wg-mir-opt

@compiler-errors
Copy link
Member Author

r? lcnr

lol idk who else to request review from sorry

@rustbot rustbot assigned lcnr and unassigned saethlin Aug 14, 2024
@lcnr
Copy link
Contributor

lcnr commented Aug 15, 2024

@bors r+ rollup

This doesn't affect the child_capture.is_by_ref() && !parent_capture.is_by_ref() case, but only the !child_capture.is_by_ref() && parent_capture.is_by_ref() which as stated, only happens if the child moves something which the parent context borrowed, so it should always error.

While I don't fully understand the surrounding context, the change itself doesn't seem to make anything worse and I trust both @compiler-errors and MIR validation to catch any issues.

@lcnr lcnr closed this Aug 15, 2024
@lcnr lcnr reopened this Aug 15, 2024
@lcnr
Copy link
Contributor

lcnr commented Aug 15, 2024

@bors r+ rollup

@bors
Copy link
Contributor

bors commented Aug 15, 2024

📌 Commit 1e1d839 has been approved by lcnr

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 15, 2024
GuillaumeGomez added a commit to GuillaumeGomez/rust that referenced this pull request Aug 15, 2024
…-ref, r=lcnr

Fix projections when parent capture is by-ref but child capture is by-value in the `ByMoveBody` pass

This fixes a somewhat strange bug where we build the incorrect MIR in rust-lang#129074. This one is weird, but I don't expect it to actually matter in practice since it almost certainly results in a move error in borrowck. However, let's not ICE.

Given the code:

```
#![feature(async_closure)]

// NOT copy.
struct Ty;

fn hello(x: &Ty) {
    let c = async || {
        *x;
        //~^ ERROR cannot move out of `*x` which is behind a shared reference
    };
}

fn main() {}
```

The parent coroutine-closure captures `x: &Ty` by-ref, resulting in an upvar of `&&Ty`. The child coroutine captures `x` by-value, resulting in an upvar of `&Ty`. When constructing the by-move body for the coroutine-closure, we weren't applying an additional deref projection to convert the parent capture into the child capture, resulting in an type error in assignment, which is a validation ICE.

As I said above, this only occurs (AFAICT) in code that eventually results in an error, because it is only triggered by HIR that attempts to move a non-copy value out of a ref. This doesn't occur if `Ty` is `Copy`, since we'd instead capture `x` by-ref in the child coroutine.

Fixes rust-lang#129074
bors added a commit to rust-lang-ci/rust that referenced this pull request Aug 15, 2024
…llaumeGomez

Rollup of 8 pull requests

Successful merges:

 - rust-lang#128348 (Unconditionally allow shadow call-stack sanitizer for AArch64)
 - rust-lang#128922 (rust-analyzer: use in-tree `pattern_analysis` crate)
 - rust-lang#128935 (More work on `zstd` compression)
 - rust-lang#129072 (Infer async closure args from `Fn` bound even if there is no corresponding `Future` bound on return)
 - rust-lang#129101 (Fix projections when parent capture is by-ref but child capture is by-value in the `ByMoveBody` pass)
 - rust-lang#129106 (Remove redundant type ops: `Eq`/`Subtype`)
 - rust-lang#129122 (Remove duplicated `Rustdoc::output` method from `run-make-support` lib)
 - rust-lang#129124 (rustdoc-json: Use FxHashMap from rustdoc_json_types)

r? `@ghost`
`@rustbot` modify labels: rollup
bors added a commit to rust-lang-ci/rust that referenced this pull request Aug 15, 2024
…iaskrgr

Rollup of 8 pull requests

Successful merges:

 - rust-lang#128348 (Unconditionally allow shadow call-stack sanitizer for AArch64)
 - rust-lang#129065 (Use `impl PartialEq<TokenKind> for Token` more.)
 - rust-lang#129072 (Infer async closure args from `Fn` bound even if there is no corresponding `Future` bound on return)
 - rust-lang#129096 (Print more verbose error for commands that capture output)
 - rust-lang#129101 (Fix projections when parent capture is by-ref but child capture is by-value in the `ByMoveBody` pass)
 - rust-lang#129106 (Remove redundant type ops: `Eq`/`Subtype`)
 - rust-lang#129122 (Remove duplicated `Rustdoc::output` method from `run-make-support` lib)
 - rust-lang#129124 (rustdoc-json: Use FxHashMap from rustdoc_json_types)

r? `@ghost`
`@rustbot` modify labels: rollup
@bors bors merged commit 6c898d2 into rust-lang:master Aug 15, 2024
12 checks passed
@rustbot rustbot added this to the 1.82.0 milestone Aug 15, 2024
rust-timer added a commit to rust-lang-ci/rust that referenced this pull request Aug 15, 2024
Rollup merge of rust-lang#129101 - compiler-errors:deref-on-parent-by-ref, r=lcnr

Fix projections when parent capture is by-ref but child capture is by-value in the `ByMoveBody` pass

This fixes a somewhat strange bug where we build the incorrect MIR in rust-lang#129074. This one is weird, but I don't expect it to actually matter in practice since it almost certainly results in a move error in borrowck. However, let's not ICE.

Given the code:

```
#![feature(async_closure)]

// NOT copy.
struct Ty;

fn hello(x: &Ty) {
    let c = async || {
        *x;
        //~^ ERROR cannot move out of `*x` which is behind a shared reference
    };
}

fn main() {}
```

The parent coroutine-closure captures `x: &Ty` by-ref, resulting in an upvar of `&&Ty`. The child coroutine captures `x` by-value, resulting in an upvar of `&Ty`. When constructing the by-move body for the coroutine-closure, we weren't applying an additional deref projection to convert the parent capture into the child capture, resulting in an type error in assignment, which is a validation ICE.

As I said above, this only occurs (AFAICT) in code that eventually results in an error, because it is only triggered by HIR that attempts to move a non-copy value out of a ref. This doesn't occur if `Ty` is `Copy`, since we'd instead capture `x` by-ref in the child coroutine.

Fixes rust-lang#129074
@workingjubilee
Copy link
Member

forget about it, homu. the loop's over.

@bors r-

@bors bors added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. labels Aug 16, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. 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.

ICE: broken MIR in CoroutineKindShim
6 participants