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

Implement Clone for all Copy types "by magic" #33507

Closed
DemiMarie opened this issue May 9, 2016 · 9 comments
Closed

Implement Clone for all Copy types "by magic" #33507

DemiMarie opened this issue May 9, 2016 · 9 comments

Comments

@DemiMarie
Copy link
Contributor

DemiMarie commented May 9, 2016

This probably needs an RFC, but one solution to the problem of Copy not implying Clone is to make this happen via compiler magic: the compiler "magically" inserts an implementation of Clone for any Copy type.

GHC does something similar with Coercible.

@retep998
Copy link
Member

retep998 commented May 9, 2016

Due to RFC 1521, we're allowed to assume that a Clone impl for Copy types is trivial and can be optimized out. Having the compiler automatically add trivial memcpy Clone implementation for all Copy types should be safe. As well there shouldn't be a backwards compatibility hazard as it would only allow more code to compile.

@bstrie
Copy link
Contributor

bstrie commented May 9, 2016

Seems a bit magical, is the intent of this simply to allow one to type #[derive(Copy)] instead of #[derive(Copy, Clone)] ?

@durka
Copy link
Contributor

durka commented May 9, 2016

@bstrie also to fix the bug where large arrays and tuples are Copy but not Clone.

@DemiMarie
Copy link
Contributor Author

DemiMarie commented May 10, 2016

@bstrie The latter is actually the main purpose. Also to fix the ICEs that can result from a type being Copy but not Clone.

I do wish that this could be done in libcore as something like

impl<T: Copy> Clone for T {
  fn clone(&self) {
    return self;
  }
}

@sfackler
Copy link
Member

This feels maybe doable with specialization, though I think it might require the lattice rule.

At the moment, we don't want to use specialization in a way that adds functionality that can't exist without it since the feature's unstable.

@durka
Copy link
Contributor

durka commented May 10, 2016

I've never seen it tried, but yeah, whenever this comes up in the context
of specialization that seems to be the prevailing wisdom.

AFAIU the proposal here is to do it by magic, to eliminate concerns over
whether specialization is powerful and/or stable enough.

It's a good idea if we want to commit to the fact that large arrays and
tuples WILL be Clone... somehow... eventually. It's a bad idea if we could
contemplate fixing the bug some other way, like by making those types
non-Copy (a breaking change). There are also other ways to fix the bug,
like type-level integers, or even halfway type-level integers that I've
seen proposed (where you can impl for [T; _] but you can't refer to N
directly).

On Tue, May 10, 2016 at 12:21 PM, Steven Fackler notifications@github.com
wrote:

This feels maybe doable with specialization, though I think it might
require the lattice rule.

At the moment, we don't want to use specialization in a way that adds
functionality that can't exist without it since the feature's unstable.


You are receiving this because you are subscribed to this thread.
Reply to this email directly or view it on GitHub
#33507 (comment)

@retep998
Copy link
Member

It's a bad idea if we could contemplate fixing the bug some other way, like by making those types non-Copy (a breaking change)

Please no! This would break so much!

Mostly winapi.

@DemiMarie
Copy link
Contributor Author

Yes, the idea here is to do it purely by compiler magic.  My intent is
for the magic to be temporary, and to be removed when it becomes
possible to implement in libcore.
This is similar to the magic typing rules for std::mem::transmute (I
believe that there is no other way to write code that compiles if and
only if two types are the same size).
On Tue, 2016-05-10 at 09:27 -0700, Alex Burka wrote:

I've never seen it tried, but yeah, whenever this comes up in the
context
of specialization that seems to be the prevailing wisdom.

AFAIU the proposal here is to do it by magic, to eliminate concerns
over
whether specialization is powerful and/or stable enough.

It's a good idea if we want to commit to the fact that large arrays
and
tuples WILL be Clone... somehow... eventually. It's a bad idea if we
could
contemplate fixing the bug some other way, like by making those types
non-Copy (a breaking change). There are also other ways to fix the
bug,
like type-level integers, or even halfway type-level integers that
I've
seen proposed (where you can impl for [T; _] but you can't refer to N
directly).

On Tue, May 10, 2016 at 12:21 PM, Steven Fackler
b.com>
wrote:

This feels maybe doable with specialization, though I think it
might
require the lattice rule.

At the moment, we don't want to use specialization in a way that
adds
functionality that can't exist without it since the feature's
unstable.


You are receiving this because you are subscribed to this thread.
Reply to this email directly or view it on GitHub

237>


You are receiving this because you authored the thread.
Reply to this email directly or view it on GitHub

@Mark-Simulacrum
Copy link
Member

I'm going to close this issue, since this is not the right place for it. If someone wants to pursue this, please follow the RFC process here https://github.com/rust-lang/rfcs#before-creating-an-rfc.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants