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

Suggest calling clone on moved values #42446

Closed
Mark-Simulacrum opened this issue Jun 5, 2017 · 9 comments
Closed

Suggest calling clone on moved values #42446

Mark-Simulacrum opened this issue Jun 5, 2017 · 9 comments
Labels
A-diagnostics Area: Messages for errors, warnings, and lints C-enhancement Category: An issue proposing an enhancement or a PR with one.

Comments

@Mark-Simulacrum
Copy link
Member

We should add a help message indicating that calling clone where the value was moved is a good idea, but we should only do this when the value is cloneable.

fn main() {
    let a = String::new();
    a;
    a;
}
error[E0382]: use of moved value: `a`
 --> test.rs:4:5
  |
3 |     a;
  |     - value moved here
4 |     a;
  |     ^ value used here after move
  |
  = note: move occurs because `a` has type `std::string::String`, which does not implement the `Copy` trait

error: aborting due to previous error(s)
@zackmdavis
Copy link
Member

(Caution may be warranted, as a comment in borrowck suggests that an earlier implementation of this was more trouble than it was worth.)

@Mark-Simulacrum
Copy link
Member Author

Hmm, that's true. I see why we don't want this necessarily (Rust has clones everywhere!) but I am also concerned about the other side where users don't know about Clone and nothing tells them that it's a possibility.

@nikomatsakis
Copy link
Contributor

I am against this suggestion, because it is far from universal in my experience that clone() is the correct solution. I would however think this would be a good topic to cover in the extended error message for E0382 -- i.e., what are the strategies for avoiding moves: borrow, clone, etc.

@Mark-Simulacrum
Copy link
Member Author

Yeah, I'm going to close this. It seems that suggesting calling clone isn't the best idea here...

@Mark-Simulacrum
Copy link
Member Author

Ah, actually, yeah, covering this in extended documentation is a good idea...

@Mark-Simulacrum Mark-Simulacrum added the C-enhancement Category: An issue proposing an enhancement or a PR with one. label Jul 26, 2017
@estebank
Copy link
Contributor

Mentoring instructions

Expand the documentation for E0382 when it talks about Copy and Clone.


@nikomatsakis, @Mark-Simulacrum, would it make sense to check for Clone and add a note, not a suggestion?

  = note: move occurs because `a` has type `std::string::String`, which does not implement the `Copy` trait
  = help: `std::string::String` implements the `Clone` trait, so you could call `.clone()` on `a` to copy its contents to a new location in memory

@estebank estebank added E-easy Call for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue. E-mentor Call for participation: This issue has a mentor. Use #t-compiler/help on Zulip for discussion. WG-diagnostics Working group: Diagnostics labels Sep 25, 2017
@Mark-Simulacrum
Copy link
Member Author

I don't think adding the help would be a good idea. I share @nikomatsakis' concerns over the potential negative impacts, along the lines of "why no auto clone, then?" which would probably be non-ideal for us. I think the extended error description is the perfect place to put this, since newcomers to Rust hopefully are using that as much as possible (do we have any data on this?).

@estebank
Copy link
Contributor

I don't think adding the help would be a good idea.

Fair enough. We can close this ticket then when we expand the error docs.

do we have any data on this?

I don't know if we do, as I haven't seen any code in the compiler to send metrics (but I haven't looked for that either).

Now that I think about it, it might be a neat feature to highlight very prominently the --explain flag when first running rustc and always highlight it when doing rustc -h, or at the very least make it the first line of the help (instead of now somewhere in the lower half).

kennytm added a commit to kennytm/rust that referenced this issue Oct 18, 2017
Mention Clone and refs in --explain E0382

I followed the discussion in rust-lang#42446 and came up with these additions.

- Mention references before going into traits. They're probably more likely solutions.
- Mention `Clone` before `Copy`. Cloning has wider applicability and `#derive[Copy, Clone]` makes more sense after learning about `Clone`.

The language is not great, any suggestions there would be appreciated ✨
@CAD97
Copy link
Contributor

CAD97 commented Dec 23, 2017

Is this fixed by #45082 or do we want more?

@estebank estebank removed E-easy Call for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue. E-mentor Call for participation: This issue has a mentor. Use #t-compiler/help on Zulip for discussion. WG-diagnostics Working group: Diagnostics labels Feb 13, 2018
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.
Projects
None yet
Development

No branches or pull requests

6 participants