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

Confusing error message with unimplemented Clone #55121

Open
andreytkachenko opened this issue Oct 16, 2018 · 5 comments
Open

Confusing error message with unimplemented Clone #55121

andreytkachenko opened this issue Oct 16, 2018 · 5 comments
Labels
A-diagnostics Area: Messages for errors, warnings, and lints C-enhancement Category: An issue proposing an enhancement or a PR with one. D-papercut Diagnostics: An error or lint that needs small tweaks. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@andreytkachenko
Copy link

Consider this example:

struct Test(u64, u64);
impl Test {
    fn into_tuple(self) -> (u64, u64) {
        (self.0, self.1)
    }
}
fn main() {
    let x = vec![Test(1,1), Test(1,2)];
    
    let _tuples = x.iter().map(|test| test.clone().into_tuple()).collect::<Vec<_>>();
}

struct Test is not implementing Clone trait, but here is the error message:

   Compiling playground v0.0.1 (file:///playground)
error[E0507]: cannot move out of borrowed content
  --> src/main.rs:12:39
   |
12 |     let _tuples = x.iter().map(|test| test.clone().into_tuple()).collect::<Vec<_>>();
   |                                       ^^^^^^^^^^^^ cannot move out of borrowed content

error: aborting due to previous error
@estebank estebank added the A-diagnostics Area: Messages for errors, warnings, and lints label Oct 16, 2018
@estebank
Copy link
Contributor

Proposed output:

error[E0507]: cannot move out of borrowed content
  --> src/main.rs:12:39
   |
1  | struct Test(u64, u64);
   | ---------------------- `Test` doesn't implement `Clone`
...
12 |     let _tuples = x.iter().map(|test| test.clone().into_tuple()).collect::<Vec<_>>();
   |                                       ^^^^^^^^^^^^ cannot move out of borrowed content
   |
   = help: `test` is of type `&Test` and `Test` doesn't implement `Clone`, so you're cloning the borrow

error: aborting due to previous error

@estebank
Copy link
Contributor

Triage, current output:

error[E0507]: cannot move out of a shared reference
  --> src/main.rs:10:39
   |
10 |     let _tuples = x.iter().map(|test| test.clone().into_tuple()).collect::<Vec<_>>();
   |                                       ^^^^^^^^^^^^ move occurs because value has type `Test`, which does not implement the `Copy` trait

@estebank estebank added D-papercut Diagnostics: An error or lint that needs small tweaks. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Jan 22, 2020
@JohnTitor JohnTitor added the C-enhancement Category: An issue proposing an enhancement or a PR with one. label Apr 12, 2020
@estebank
Copy link
Contributor

estebank commented Aug 3, 2023

Current output:

error[E0507]: cannot move out of a shared reference
  --> src/main.rs:10:39
   |
10 |     let _tuples = x.iter().map(|test| test.clone().into_tuple()).collect::<Vec<_>>();
   |                                       ^^^^^^^^^^^^^------------
   |                                       |            |
   |                                       |            value moved due to this method call
   |                                       move occurs because value has type `Test`, which does not implement the `Copy` trait
   |
note: `Test::into_tuple` takes ownership of the receiver `self`, which moves value
  --> src/main.rs:3:19
   |
3  |     fn into_tuple(self) -> (u64, u64) {
   |                   ^^^^

We should probably suggest #[derive(Copy)] on Test.

@estebank
Copy link
Contributor

Output with #122603:

error[E0507]: cannot move out of a shared reference
  --> f109.rs:10:39
   |
10 |     let _tuples = x.iter().map(|test| test.clone().into_tuple()).collect::<Vec<_>>();
   |                                       ^^^^^^^^^^^^ ------------ value moved due to this method call
   |                                       |
   |                                       move occurs because value has type `Test`, which does not implement the `Copy` trait
   |
note: `Test::into_tuple` takes ownership of the receiver `self`, which moves value
  --> f109.rs:3:19
   |
3  |     fn into_tuple(self) -> (u64, u64) {
   |                   ^^^^
note: if `Test` implemented `Clone`, you could clone the value
  --> f109.rs:1:1
   |
1  | struct Test(u64, u64);
   | ^^^^^^^^^^^

We could further explain that the .clone() is on the reference.

@estebank
Copy link
Contributor

estebank commented May 2, 2024

Current output:

error[E0507]: cannot move out of a shared reference
  --> src/main.rs:10:39
   |
10 |     let _tuples = x.iter().map(|test| test.clone().into_tuple()).collect::<Vec<_>>();
   |                                       ^^^^^^^^^^^^ ------------ value moved due to this method call
   |                                       |
   |                                       move occurs because value has type `Test`, which does not implement the `Copy` trait
   |
note: `Test::into_tuple` takes ownership of the receiver `self`, which moves value
  --> src/main.rs:3:19
   |
3  |     fn into_tuple(self) -> (u64, u64) {
   |                   ^^^^
note: if `Test` implemented `Clone`, you could clone the value
  --> src/main.rs:1:1
   |
1  | struct Test(u64, u64);
   | ^^^^^^^^^^^ consider implementing `Clone` for this type
...
10 |     let _tuples = x.iter().map(|test| test.clone().into_tuple()).collect::<Vec<_>>();
   |                                       ------------ you could clone this value

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints C-enhancement Category: An issue proposing an enhancement or a PR with one. D-papercut Diagnostics: An error or lint that needs small tweaks. 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