rust-lang / rust-lang/rust

Suggest dropping before reassignment of a variable with a mutable reference and a destructor

Open
#126,743 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-diagnostics T-compiler
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

Description

Code
struct Bar<'num>(&'num mut i32);

impl Drop for Bar<'_> {
    fn drop(&mut self) {
    }
}

fn foo(mut_ref: &mut i32) {

    let mut baz = Bar(mut_ref);
    // drop(baz);
    baz = Bar(mut_ref);
}
Current output
warning: variable `baz` is assigned to, but never used
  --> src/main.rs:10:13
   |
10 |     let mut baz = Bar(mut_ref);
   |             ^^^
   |
   = note: consider using `_baz` instead
   = note: `#[warn(unused_variables)]` on by default

warning: value assigned to `baz` is never read
  --> src/main.rs:12:5
   |
12 |     baz = Bar(mut_ref);
   |     ^^^
   |
   = help: maybe it is overwritten before being read?
   = note: `#[warn(unused_assignments)]` on by default

error[E0499]: cannot borrow `*mut_ref` as mutable more than once at a time
  --> src/main.rs:12:15
   |
10 |     let mut baz = Bar(mut_ref);
   |                       ------- first mutable borrow occurs here
11 |     // drop(baz);
12 |     baz = Bar(mut_ref);
   |     ---       ^^^^^^^ second mutable borrow occurs here
   |     |
   |     first borrow might be used here, when `baz` is dropped and runs the `Drop` code for type `Bar`

error: aborting due to 1 previous error; 2 warnings emitted

For more information about this error, try `rustc --explain E0499`.
Desired output
I have no idea how the diagnostic syntax is for suggesting a new item:

warning: variable `baz` is assigned to, but never used
  --> src/main.rs:10:13
   |
10 |     let mut baz = Bar(mut_ref);
   |             ^^^
   |
   = note: consider using `_baz` instead
   = note: `#[warn(unused_variables)]` on by default

warning: value assigned to `baz` is never read
  --> src/main.rs:12:5
   |
12 |     baz = Bar(mut_ref);
   |     ^^^
   |
   = help: maybe it is overwritten before being read?
   = note: `#[warn(unused_assignments)]` on by default

error[E0499]: cannot borrow `*mut_ref` as mutable more than once at a time
  --> src/main.rs:12:15
   |
10 |     let mut baz = Bar(mut_ref);
   |                       ------- first mutable borrow occurs here
11 |     // drop(baz);
12 |     baz = Bar(mut_ref);
   |     ---       ^^^^^^^ second mutable borrow occurs here
   |     |
   |     first borrow might be used here, when `baz` is dropped and runs the `Drop` code for type `Bar`
   = help: try dropping before the assignment: `drop(baz);`

error: aborting due to 1 previous error; 2 warnings emitted

For more information about this error, try `rustc --explain E0499`.
Rationale and extra context

When adding drop(baz); before the assignment the code works and at least to me, it's not immediately obvious that a drop would fix it or even work between assignments.

Other cases

No response

Rust Version
$ rustc --version --verbose
rustc 1.77.2 (25ef9e3d8 2024-04-09) (built from a source tarball)
binary: rustc
commit-hash: 25ef9e3d85d934b27d9dada2f9dd52b1dc63bb04
commit-date: 2024-04-09
host: x86_64-unknown-linux-gnu
release: 1.77.2
LLVM version: 17.0.6
Anything else?

No response

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the reproducer in src/main.rs and run it to inspect the E0499 diagnostic. Investigate the compiler diagnostic path for this mutable-borrow reassignment case; done means the error includes a help suggestion to insert drop(baz); before the assignment, without changing the existing warnings.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.