E0382: Add help for fixing a partially moved struct by manual assignment
Nobody has claimed this yet.
- 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
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- 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