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

Can't modify value after temporarily dropping reference in loop when using Drop #92984

Closed
AzureMarker opened this issue Jan 16, 2022 · 5 comments
Labels
A-borrow-checker Area: The borrow checker A-MIR Area: Mid-level IR (MIR) - https://blog.rust-lang.org/2016/04/19/MIR.html C-bug Category: This is a bug. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@AzureMarker
Copy link
Member

I tried this code:

struct Console<'gfx>(&'gfx usize);

impl<'gfx> Drop for Console<'gfx> {
    fn drop(&mut self) {}
}

fn main() {
    let mut gfx = 0;
    let mut console = Console(&gfx);

    for _ in 0..5 {
        println!("{gfx}");
        
        if gfx == 0 {
            drop(console);
            
            gfx += 1;
            
            console = Console(&gfx);
        }
    }
}

I expected to see this happen: Compile

Instead, this happened: Fails with error:

error[E0506]: cannot assign to `gfx` because it is borrowed
  --> src/main.rs:17:13
   |
9  |     let mut console = Console(&gfx);
   |                               ---- borrow of `gfx` occurs here
...
17 |             gfx += 1;
   |             ^^^^^^^^ assignment to borrowed `gfx` occurs here
18 |             
19 |             console = Console(&gfx);
   |             ------- borrow might be used here, when `console` is dropped and runs the `Drop` code for type `Console`

For more information about this error, try `rustc --explain E0506`.

It works though if you remove the Drop impl for Console, or if you remove the loop. I would expect it to work with Drop because the drop call happens before gfx is modified, and there shouldn't be any drop call immediately after it like the diagnostic suggests.

Meta

rustc --version --verbose:

rustc 1.58.0 (02072b482 2022-01-11)
binary: rustc
commit-hash: 02072b482a8b5357f7fb5e5637444ae30e423c40
commit-date: 2022-01-11
host: x86_64-unknown-linux-gnu
release: 1.58.0
LLVM version: 13.0.0

Tested in playground and in a local dev rustc (based on commit 1b3a5f2, from 3 weeks ago).

@AzureMarker
Copy link
Member Author

AzureMarker commented Jan 17, 2022

I think we need to call record_operands_moved when lowering the assignment. Otherwise it inserts a drop for the temporary RHS value. This drop eventually gets culled by a later pass (elaborate drops I think) but since the borrow checker runs before that, it fails the build.

I'm still not sure why it works when outside of a loop. Haven't investigated that part yet. Currently trying to find the right way to call record_operands_moved so it doesn't add the drop.

@AzureMarker
Copy link
Member Author

I think this would also require a change to the borrow checker. After adding a call to record_operands_moved the drop call for the temporary goes away in the MIR, but the error stays. Checking the borrow check dataflow graph, I see the borrow is still there even after dropping the borrower:
image

@AzureMarker
Copy link
Member Author

This is the relevant MIR and borrowck graph if you remove the loop. This time the borrow gets killed when the moved borrower's storage gets killed (StorageDead). Not sure why this doesn't happen in the loop case.
image

@AzureMarker
Copy link
Member Author

This is the relevant MIR and borrowck graph if you comment out the Drop impl for Console (but keeping the loop). This is the kind of borrowing that I expect. Maybe the borrow checker doesn't fully understand TerminatorKind::DropAndReplace?
image

@fmease
Copy link
Member

fmease commented Jan 26, 2024

This now compiles successfully on stable and master. I presume #107421 fixed this, it made MIR drop tracking the default.

@fmease fmease closed this as completed Jan 26, 2024
@fmease fmease added A-borrow-checker Area: The borrow checker A-MIR Area: Mid-level IR (MIR) - https://blog.rust-lang.org/2016/04/19/MIR.html T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. and removed needs-triage-legacy labels Jan 26, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-borrow-checker Area: The borrow checker A-MIR Area: Mid-level IR (MIR) - https://blog.rust-lang.org/2016/04/19/MIR.html C-bug Category: This is a bug. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

3 participants