rust-lang / rust-lang/rust

Better error message when trying to move out of a value after an implitic deref

Open
#139,227 0 comments 1 reaction 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
use std::ops::Deref;

struct Wrapper {
    inner: String,
}

impl Deref for Wrapper {
    type Target = String;
    fn deref(&self) -> &String {
        &self.inner
    }
}

impl Wrapper {
    fn new(inner: String) -> Self {
        Wrapper {
            inner,
        }
    }
}

impl From<Wrapper> for String {
    fn from(x: Wrapper) -> String {
        x.inner
    }
}

fn main() {
    let wrapper = Wrapper::new(format!("hello"));

    // This doesnt compile
    let bytes = wrapper.into_bytes();

    // But this works
    //let bytes = wrapper.inner.into_bytes();
    
    // And this should be the suggestion
    //let bytes = String::from(wrapper).into_bytes();
}
Current output
error[E0507]: cannot move out of dereference of `Wrapper`
    --> src/main.rs:33:17
     |
33   |     let bytes = wrapper.into_bytes();
     |                 ^^^^^^^ ------------ value moved due to this method call
     |                 |
     |                 move occurs because value has type `String`, which does not implement the `Copy` trait
     |
note: `String::into_bytes` takes ownership of the receiver `self`, which moves value
    --> /playground/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/alloc/src/string.rs:1048:29
     |
1048 |     pub const fn into_bytes(self) -> Vec<u8> {
     |                             ^^^^
help: you can `clone` the value and consume it, but this might not be your desired behavior
     |
33   |     let bytes = <String as Clone>::clone(&wrapper).into_bytes();
     |                 ++++++++++++++++++++++++++       +
help: consider cloning the value if the performance cost is acceptable
     |
33   |     let bytes = wrapper.clone().into_bytes();
     |                        ++++++++
Desired output
error[E0507]: cannot move out of dereference of `Wrapper`
    --> src/main.rs:33:17
     |
33   |     let bytes = wrapper.into_bytes();
     |                 ^^^^^^^ ------------ value moved due to this method call
     |                 |
     |                 move occurs because value has type `String`, which does not implement the `Copy` trait
     |
note: `String::into_bytes` takes ownership of the receiver `self`, which moves value
    --> /playground/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/alloc/src/string.rs:1048:29
     |
1048 |     pub const fn into_bytes(self) -> Vec<u8> {
     |                             ^^^^
note: `into_bytes` is behind a `Deref`, so your code is equivalent to
     |
33   |     let bytes = Deref::deref(&wrapper).into_bytes();
     |                              ^
     |                              deref inserts a reference, so you cannot move out of the variable
help: use `From` trait to convert `Wrapper` to `String`
     |
     |     let bytes = String::from(wrapper).into_bytes();
Rationale and extra context

It was not obvious to me why I could not move out of wrapper based on the original error message. I spent some time checking for borrows and trying to drop other variables that might reference it. But the issue was simply that into_bytes was not implemented on the Wrapper type but on the inner type after a deref, so it was not possible to use a method that consumes self directly.

If my desired error message is hard to implement, it is enough to mention that Deref is involved. The original error message hints at it by saying "cannot move out of dereference of Wrapper", but that was not enough for me.

Playground link

Other cases

Rust Version
rustc from playground:

Build using the Nightly version: 1.88.0-nightly

(2025-03-31 0b45675cfcec57f30a37)
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

Begin with the Rust Playground reproduction and inspect the compiler diagnostic handling for E0507 when a consuming method is reached through Deref. Use the requested Deref explanation and From-based help as the acceptance criteria, and add regression coverage for this example.

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
Clearly specified
Newbie friendliness
52/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.