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

Mention Clone and refs in --explain E0382 #45082

Merged
merged 1 commit into from
Oct 18, 2017

Conversation

jacwah
Copy link
Contributor

@jacwah jacwah commented Oct 7, 2017

I followed the discussion in #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 ✨

@rust-highfive
Copy link
Collaborator

Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @eddyb (or someone else) soon.

If any changes to this PR are deemed necessary, please add them as extra commits. This ensures that the reviewer can see what has changed since they last reviewed the code. Due to the way GitHub handles out-of-date commits, this should also make it reasonably obvious what issues have or haven't been addressed. Large or tricky changes may require several passes of review and changes.

Please see the contribution instructions for more information.

@eddyb
Copy link
Member

eddyb commented Oct 7, 2017

How do I keep getting assigned to suggestion PR?! r? @nikomatsakis

@rust-highfive rust-highfive assigned nikomatsakis and unassigned eddyb Oct 7, 2017

```
#[derive(Clone)]
struct MyStruct { s: u32 }
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am trying to figure out how to tie in the copy vs clone dichotomy a bit better here. I think it's useful to explain when you would want what. I sometimes mention a three-level grouping here:

  • Default: represents a unique resource
    • Example: Money
  • Implements Clone: represents a resource, but one that can be copied
    • Example: Vector, String
  • Implements Copy and Clone: represents a value with no ownership semantics
    • Example: integer

I am torn between keeping the presentation as you did it -- with one type (MyStruct) that grows impls -- versus changing to give a bit more semantics context. For example, one could have struct MyStruct { values: Vec<u32> } for clone and change to struct Point { x: u32, y: u32 } or something as an example where Copy would be appropriate.

Really, I'd prefer to tag in somebody like @steveklabnik or @carols10cents here -- it seems like we should work hard to ensure that this extended error message ties in well with the way that the book explains things.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I think giving better examples is a good idea. Clone vs Copy is explained as heap vs stack data in the book. I'm not sure this is the best way to go about it though. I like your explanation better, focusing on semantics instead of low-level memory layout which may be less familiar to a newcomer.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I also like Niko's framing as well; the book tries to do a fairly low-level explanation, but once you grasp that, the higher-level one is better, IMO.

@nikomatsakis
Copy link
Contributor

r? @steveklabnik

@carols10cents carols10cents added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Oct 9, 2017
@carols10cents
Copy link
Member

Around here is where we first discuss Clone/Copy in the book.

@steveklabnik
Copy link
Member

I'd be okay with merging this, but if you want to try something like @nikomatsakis suggested, I'd be open to it as well.

@jacwah
Copy link
Contributor Author

jacwah commented Oct 13, 2017

@nikomatsakis @steveklabnik I updated some of the examples. They hopefully show the use cases for different strategies a bit better.

x.s = 6;
println!("{}", x.s);
let mut p1 = Point{ x: -1, y: 2 };
let y = p2;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You mean let p2 = p1; here?

[01:14:25] ---- /checkout/obj/build/x86_64-unknown-linux-gnu/test/error-index.md - Rust_Compiler_Error_Index (line 6184) stdout ----
[01:14:25] 	error[E0425]: cannot find value `p2` in this scope
[01:14:25]  --> /checkout/obj/build/x86_64-unknown-linux-gnu/test/error-index.md:6:13
[01:14:25]   |
[01:14:25] 6 |     let y = p2;
[01:14:25]   |             ^^ did you mean `p1`?
[01:14:25] 
[01:14:25] error[E0425]: cannot find value `p2` in this scope
[01:14:25]  --> /checkout/obj/build/x86_64-unknown-linux-gnu/test/error-index.md:9:28
[01:14:25]   |
[01:14:25] 9 |     println!("p2: {}, {}", p2.x, p2.y);
[01:14:25]   |                            ^^ did you mean `p1`?
[01:14:25] 
[01:14:25] error[E0425]: cannot find value `p2` in this scope
[01:14:25]  --> /checkout/obj/build/x86_64-unknown-linux-gnu/test/error-index.md:9:34
[01:14:25]   |
[01:14:25] 9 |     println!("p2: {}, {}", p2.x, p2.y);
[01:14:25]   |                                  ^^ did you mean `p1`?
[01:14:25] 
[01:14:25] thread 'rustc' panicked at 'couldn't compile the test', /checkout/src/librustdoc/test.rs:283:12

@kennytm kennytm added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Oct 14, 2017
@bors
Copy link
Contributor

bors commented Oct 14, 2017

☔ The latest upstream changes (presumably #45167) made this pull request unmergeable. Please resolve the merge conflicts.

Mention Clone and refererences, and use more realistic examples (within
the constraints of a few lines :).
@jacwah
Copy link
Contributor Author

jacwah commented Oct 14, 2017

I squashed and rebased the work on master. Should be good to go :)

@kennytm kennytm added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Oct 14, 2017
@steveklabnik
Copy link
Member

Thanks a ton!

@bors: r+ rollup

@bors
Copy link
Contributor

bors commented Oct 17, 2017

📌 Commit 47ea51e has been approved by steveklabnik

@kennytm kennytm added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Oct 17, 2017
kennytm added a commit to kennytm/rust that referenced this pull request 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 ✨
bors added a commit that referenced this pull request Oct 18, 2017
Rollup of 10 pull requests

- Successful merges: #44138, #45082, #45098, #45181, #45217, #45281, #45325, #45326, #45340, #45354
- Failed merges:
@alexcrichton alexcrichton merged commit 47ea51e into rust-lang:master Oct 18, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

9 participants