-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
RFC - compiler-generated Clone impls for Arrays and Tuples #2133
Conversation
Perhaps add to Alternatives the option of waiting until the language features needed to make a proper generic implementation are available. For arrays, that would presumably be RFC 2000; who knows about tuples. |
text/0000-all-the-clones.md
Outdated
|
||
## Implement `Clone` only for `Copy` types | ||
|
||
As of Rust 1.19, the compiler *does not* have the `Clone` implementations, which causes ICEs such as rust-lang/rust#25733 because `Clone` is a supertrait of `Copy`. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For convenience, please make these real links.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Make what real links?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@arielb1 Change
... ICEs such as rust-lang/rust#25733 because ...
to
... ICEs such as [rust-lang/rust#25733] because ...
[rust-lang/rust#25733]: https://github.com/rust-lang/rust/issues/25733
Similar for references to "RFC #XXXX".
#2132 (comment) refers me back to here 😂: You may want to add rules for function types and function pointers (with arity > 12) as well. These types already impl Copy, and thus should also impl Clone.
|
text/0000-all-the-clones.md
Outdated
|
||
~~The reason (I think) is that there is no good way to write a variable-length array expression in macros. This wouldn't be fixed by the first iteration of const generics.~~ Actually, this can be done using a for-loop (`ArrayVec` is used here instead of a manual panic guard for simplicity, but it can be easily implemented given const generics). | ||
```Rust | ||
impl<const n: usize; T> Clone for [T; n] { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Missing a T: Copy
bound?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A T: Clone
rule
These were non-controversial, so I didn't include them in the RFC. Maybe I should. |
Github markdown does not include auto-links to other issues like Github in issues does.
@kennytm Done. |
I don't think we are going to stay with the ICE forever - we need to do something about higher-ranked fn pointers, and in that case there's no good language reason not to also do |
Any reason on why Also, what to do once we have RFCs 2000 and 1935 merged, should the special case in the compiler be removed in favour of an implementation in libcore, and if yes, why not? I'd like if the proposal got extended to all traits which are currently lang items. The situation with this RFC won't be perfect (e.g. It might look ugly to add so much manual stuff to the compiler but if the RFC is intended as temporary solution until we have the proper tuple/array generics RFCs merged which then would allow us to move the logic into libcore, its worth it IMO. |
@kennytm I see. Either way, I still think it would be a good idea to implement this for other lang items as well, just so that we don't have to wait on const generics. |
Team member @aturon has proposed to merge this. The next step is review by the rest of the tagged teams: Concerns:
Once these reviewers reach consensus, this will enter its final comment period. If you spot a major issue that hasn't been raised at any point in this process, please speak up! See this document for info about what commands tagged team members can give me. |
@kennytm |
Omg yes please, I ran headfirst into this inconsistency just this week (manually newtyped around it). This is definitely something that needs to be done! |
@rfcbot concern why-only-clone See #2133 (comment) and rust-lang/rust#44045 (comment) and the ensuing discussion. |
So, as for the "Why only How we already have a disparity: We already say that (In retrospect, maybe we should have moved all copy impls out of the compiler. Then there would be no disparity, all things would be equally bad. Ah well, water under the bridge.) How this RFC changes the line: However, under this RFC, we would have a somewhat bigger disparity, in that
How we could resolve this disparity: We have a number of ideas floating that can help. Const generics should enable one to write a What are our options? So it seems to me that we are stuck with a disparity between (I feel like teaching etc would not be severely impacted by this: either way, in some cases you have to explain that |
It seems like what's really wanted is a blanket |
@Diggsey it would be a solution to fix the ICEs indeed (and one that @nikomatsakis wanted to explore at some point) although another one was preferred (for the moment), but it would not address the main topics of the RFC, that is types like |
@Diggsey see my long series of blog posts on the topic ;) regardless, I do still believe we can enable that eventually through specialization, yes, though things are .. complex. That said, I don't actually think it has all that much to do with this RFC. Or, to put it another way, that's effectively an implement detail -- @scalexm has implemented something that behaves analogously already, but it also went further, and implemented |
So I talked to @eddyb on IRC, and it seemed like we reached an agreement that he was OK with this RFC, in combination with #2132, so long as:
|
Also I should say that with variadic generics (or even today!), I like the possibility of reusing tuple impls for closures - if |
@rfcbot resolved why-only-clone |
@eddyb woah -- i.e., imagining that the compiler generates a bridge impl of that kind (but for every trait)? That's a really interesting idea. I'm keen on it. EDIT: Getting less keen as I think more about it. =) Don't want to expose ordering of the data in a closure's environment, for one thing, and also wary of having surprising traits implemented for closures. But it'd definitely be cool to allow users to implement traits for closures more easily. |
+1 for the bridge impl (I'm imagining in However, I don't want to limit the number of captures in a clone-able closure, so then the closure stuff is blocked on this change (making tuples of all sizes |
🔔 This is now entering its final comment period, as per the review above. 🔔 |
This RFC has been merged! Tracking issue. Thanks @arielb1! |
@arielb1 the rendered link is now broken |
rendered.
cc @eddyb @nikomatsakis @aturon
Edit: updated rendered link