rust-lang / rust-lang/rust

E0382: Add help for fixing a partially moved struct by manual assignment

Open
#129,253 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
#[derive(Debug)]
struct A {
    a: String,
    b: String,
}


fn main(){
    let mut a: A = A { a: "test1".to_string(), b: "test2".to_string()};
    std::mem::drop(a.b);
    // A is now partially moved
    // a.b = "fix".to_string(); // fixing the partial move
    dbg!(a); // no error
}
Current output
error[E0382]: use of partially moved value: `a`
  --> src/main.rs:13:5
   |
10 |     std::mem::drop(a.b);
   |                    --- value partially moved here
...
13 |     dbg!(a); // no error
   |     ^^^^^^^ value used here after partial move
   |
   = note: partial move occurs because `a.b` has type `String`, which does not implement the `Copy` trait
Desired output
error[E0382]: use of partially moved value: `a`
  --> src/main.rs:13:5
   |
10 |     std::mem::drop(a.b);
   |                    --- value partially moved here
...
13 |     dbg!(a); // no error
   |     ^^^^^^^ value used here after partial move
   |
   = note: partial move occurs because `a.b` has type `String`, which does not implement the `Copy` trait
help: reassign a.b to a new value
12 |     a.b = String::default();
   |     ++++++++++++++++++++++++ assigning to a new value returns the struct to a useable state
Rationale and extra context

I was not aware for a long time this was possible. It is not explained in user-facing docs where it would be nice to have, and is often enough since often only the other fields are necessary, or the new owner returns a new value you could replace into this.

Its a more natural way of resolving this compared to using mem::replace before the value is moved.

Other cases

No response

Rust Version
rustc 1.80.1 (3f5fd8dd4 2024-08-06)
binary: rustc
commit-hash: 3f5fd8dd41153bc5fdca9427e9e05be2c767ba23
commit-date: 2024-08-06
host: x86_64-pc-windows-msvc
release: 1.80.1
LLVM version: 18.1.7
Anything else?

I'm not at all sure that "help:" is the correct way of phrasing this, as usually "help:" is used for cases where only one fix exists. Theres many ways to work around this, but i can't recall whether there is any existing phrasing for "have you considered this option?"

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 E0382 diagnostic described in the issue and reproduce the example from src/main.rs. Locate the compiler diagnostic implementation and its existing tests, then compare the current and desired output; done means the partially moved struct case receives the proposed reassignment guidance without regressing other E0382 diagnostics.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.