# Assuming \`Clone::clone()\` is equivalent to \`Copy\` semantics for all instances of \`Copy\`

**URL:** <https://internals.rust-lang.org/t/assuming-clone-clone-is-equivalent-to-copy-semantics-for-all-instances-of-copy/3148>\
**Category:** language design\
**Created:** [February 4, 2016, 9:10pm UTC](https://internals.rust-lang.org/t/assuming-clone-clone-is-equivalent-to-copy-semantics-for-all-instances-of-copy/3148 "2016-02-04T21:10:31Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![briansmith](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/briansmith/32/1306_2.png) [@briansmith](https://internals.rust-lang.org/u/briansmith)\
**Post date:** [February 4, 2016, 9:10pm UTC](https://internals.rust-lang.org/t/assuming-clone-clone-is-equivalent-to-copy-semantics-for-all-instances-of-copy/3148/1 "2016-02-04T21:10:31Z")

</div>

Has the following been discussed?

- If a type implements `Copy`, it must implement `Clone`. Or, rather, `Clone` is automatically derived for all `Copy` types.
- If a type implements `Copy`, it must derive `Clone`, not provide a custom implementation.

In other words, Every `Copy` type is also `Clone` and one can assume that `Clone` is equivalent to `Copy` for any `Copy` type.

This would make the proposed `clone_from_slice` semantically equivalent to the proposed `copy_from` (assuming the behaviors are aligned w.r.t. mismatched lengths) and would allow `clone_from_slice` to be specialized (hopefully using the official proposed trait specialization feature) for `copy` types such that the new `copy_from` function wouldn’t be necessary, AFAICT.

Currently, it is strange (bad) that (a) a type can implement `Clone` in a way that is different semantically from `Copy`, and (b) `Clone::clone()` can panic even for a copy type. I think the above suggestion would eliminate this strangeness.

---

<div class="post-metadata">

**Author:** ![DanielKeep](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/danielkeep/32/1332_2.png) [@DanielKeep](https://internals.rust-lang.org/u/DanielKeep)\
**Post date:** [February 5, 2016, 2:20am UTC](https://internals.rust-lang.org/t/assuming-clone-clone-is-equivalent-to-copy-semantics-for-all-instances-of-copy/3148/2 "2016-02-05T02:20:24Z")

</div>

> [@briansmith](#):
>
> If a type implements Copy, it must derive Clone, not provide a custom implementation.

This isn't true in a mechanical sense: I've implemented `Clone` by hand on `Copy` types in the past just fine (you have to sometimes if you have phantom'ed type parameters which derive will merrily whack constraints on even when it doesn't make sense to do so).

---

<div class="post-metadata">

**Author:** ![retep998](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/retep998/32/729_2.png) [@retep998](https://internals.rust-lang.org/u/retep998)\
**Post date:** [February 5, 2016, 3:25am UTC](https://internals.rust-lang.org/t/assuming-clone-clone-is-equivalent-to-copy-semantics-for-all-instances-of-copy/3148/3 "2016-02-05T03:25:33Z")

</div>

I implement `Clone` by hand in `winapi` even though everything is also `Copy` since it lets me make the `Clone` impl invoke the `Copy` impl. The derived `Clone` is a pile of bloated codegen that takes a significant amount of time to compile.

---

<div class="post-metadata">

**Author:** ![briansmith](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/briansmith/32/1306_2.png) [@briansmith](https://internals.rust-lang.org/u/briansmith)\
**Post date:** [February 5, 2016, 3:40am UTC](https://internals.rust-lang.org/t/assuming-clone-clone-is-equivalent-to-copy-semantics-for-all-instances-of-copy/3148/4 "2016-02-05T03:40:18Z")

</div>

> [@retep998](#):
>
> I implement Clone by hand in winapi even though everything is also Copy since it lets me make the Clone impl invoke the Copy impl. The derived Clone is a pile of bloated codegen that takes a significant amount of time to compile.

I think that will be fixed in [#[derive(Clone)] should rely on Copy when possible · Issue #31085 · rust-lang/rust · GitHub](https://github.com/rust-lang/rust/issues/31085).

---

<div class="post-metadata">

**Author:** ![briansmith](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/briansmith/32/1306_2.png) [@briansmith](https://internals.rust-lang.org/u/briansmith)\
**Post date:** [February 5, 2016, 3:40am UTC](https://internals.rust-lang.org/t/assuming-clone-clone-is-equivalent-to-copy-semantics-for-all-instances-of-copy/3148/5 "2016-02-05T03:40:43Z")

</div>

> [@DanielKeep](#):
>
> This isn't true in a mechanical sense: I've implemented Clone by hand on Copy types in the past just fine (you have to sometimes if you have phantom'ed type parameters which derive will merrily whack constraints on even when it doesn't make sense to do so).

Sorry, I don't understand this. Could you post (a link to) an example?

---

<div class="post-metadata">

**Author:** ![DanielKeep](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/danielkeep/32/1332_2.png) [@DanielKeep](https://internals.rust-lang.org/u/DanielKeep)\
**Post date:** [February 5, 2016, 3:42am UTC](https://internals.rust-lang.org/t/assuming-clone-clone-is-equivalent-to-copy-semantics-for-all-instances-of-copy/3148/6 "2016-02-05T03:42:48Z")

</div>

Well, there’re these [manual impls of `Copy` and `Clone`](https://github.com/DanielKeep/rust-scan-rules/blob/master/src/input.rs#L166-L187).

---

<div class="post-metadata">

**Author:** ![huon](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/huon/32/3698_2.png) [@huon](https://internals.rust-lang.org/u/huon)\
**Post date:** [February 5, 2016, 10:51am UTC](https://internals.rust-lang.org/t/assuming-clone-clone-is-equivalent-to-copy-semantics-for-all-instances-of-copy/3148/7 "2016-02-05T10:51:53Z")

</div>

It has been discussed. One route planned is/was to have a `impl<T: Copy> Clone for T {}`, but this needs specialisation, and, apparently, even the current specialisation plan doesn't quite solve it.

> [@briansmith](#):
>
> If a type implements Copy, it must derive Clone, not provide a custom implementation.

This is non-trivial: a type may only be `Copy` in a (strict) subset of the times it is `Clone`, e.g. `Option<T>: Copy` when `T: Copy`, but `Option<T>: Clone` when `T: Clone`.

---

<div class="post-metadata">

**Author:** ![nikomatsakis](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/nikomatsakis/32/5410_2.png) [@nikomatsakis](https://internals.rust-lang.org/u/nikomatsakis)\
**Post date:** [February 12, 2016, 7:09pm UTC](https://internals.rust-lang.org/t/assuming-clone-clone-is-equivalent-to-copy-semantics-for-all-instances-of-copy/3148/8 "2016-02-12T19:09:33Z")

</div>

My feeling is that if a type is `Copy`, then it is valid to not invoke `Clone` and instead copy the bytes manually.

---

<div class="post-metadata">

**Author:** ![nikomatsakis](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/nikomatsakis/32/5410_2.png) [@nikomatsakis](https://internals.rust-lang.org/u/nikomatsakis)\
**Post date:** [February 12, 2016, 7:12pm UTC](https://internals.rust-lang.org/t/assuming-clone-clone-is-equivalent-to-copy-semantics-for-all-instances-of-copy/3148/9 "2016-02-12T19:12:15Z")

</div>

In other words – `Copy` refines the contract on `Clone` to be more precise, and if your Clone impl diverges from the Copy semantics then you are asking for trouble.

---

<div class="post-metadata">

**Author:** ![glaebhoerl](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/glaebhoerl/32/1978_2.png) [@glaebhoerl](https://internals.rust-lang.org/u/glaebhoerl)\
**Post date:** [February 13, 2016, 12:29pm UTC](https://internals.rust-lang.org/t/assuming-clone-clone-is-equivalent-to-copy-semantics-for-all-instances-of-copy/3148/10 "2016-02-13T12:29:46Z")

</div>

@nikomatsakis But isn’t giving the optimizer the right to _assume_ that the contract is obeyed the kind of thing that’s only OK with `unsafe trait`s?

---

<div class="post-metadata">

**Author:** ![hanna-kruppe](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/hanna-kruppe/32/6540_2.png) [@hanna-kruppe](https://internals.rust-lang.org/u/hanna-kruppe)\
**Post date:** [February 13, 2016, 12:53pm UTC](https://internals.rust-lang.org/t/assuming-clone-clone-is-equivalent-to-copy-semantics-for-all-instances-of-copy/3148/11 "2016-02-13T12:53:17Z")

</div>

I don’t know about the optimizer (different behavior depending on whether a compiler optimization runs is really really bad), but certainly library code (including essential code in libcore, like `clone_from_slice`) can assume a contract holds even without `unsafe trait`. The latter is only needed when violating the contract can lead to memory unsafety. Functionally wrong behavior is not in the purview of `unsafe`.

---

<div class="post-metadata">

**Author:** ![glaebhoerl](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/glaebhoerl/32/1978_2.png) [@glaebhoerl](https://internals.rust-lang.org/u/glaebhoerl)\
**Post date:** [February 18, 2016, 5:48pm UTC](https://internals.rust-lang.org/t/assuming-clone-clone-is-equivalent-to-copy-semantics-for-all-instances-of-copy/3148/12 "2016-02-18T17:48:40Z")

</div>

> [@hanna-kruppe](#):
>
> certainly library code (including essential code in libcore, like clone\_from\_slice) can assume a contract holds even without unsafe trait. The latter is only needed when violating the contract can lead to memory unsafety

Yes I agree.

> I don't know about the optimizer

I think it's different.

Suppose you wrote `unsafe` code which depends on your custom implementation of `clone()` being called. The `unsafe` code in this instance is not relying on any implicit contracts: it is relying on the fact that when it calls `clone()`, `clone()` will be called. If the compiler rewrites it to skip the call and `Copy` instead, the `unsafe` code violates memory safety. Who is at fault? It seems pretty clear-cut to me that it's the compiler. What gives it the right to rewrite expressions according to the informal invariant around `Clone` and `Copy`, but not other any other `trait`s, when there isn't any in-language indication of `Clone` or `Copy` being special in this respect? For this to be OK I believe that we would need `unsafe trait Copy`, analogously to `ExactSizeIterator`.

Note that I'm not asking how _probable_, _wise_, or _inlandish_ this scenario is. It's probably not any of those things. But when thinking about language guarantees and `unsafe` we deal in absolutes. Overflowing the reference count of an `Rc` or `Arc` is not particularly likely either, yet we considered it a bug in need of fixing. This optimization would likewise be wrong on principle.

---

<div class="post-metadata">

**Author:** ![bluss](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/bluss/32/2264_2.png) [@bluss](https://internals.rust-lang.org/u/bluss)\
**Post date:** [February 18, 2016, 7:44pm UTC](https://internals.rust-lang.org/t/assuming-clone-clone-is-equivalent-to-copy-semantics-for-all-instances-of-copy/3148/13 "2016-02-18T19:44:35Z")

</div>

This seems to be precisely the parametricity debate that is going on in the impl specialization RFC thread too. We discussed different kinds of parametricity violations, and that traits that are sufficiently similar, such as `BufRead` being an extension of `Read`, could be expected to be used.

Following that, `Copy` is a very close extension to `Clone`, and if it seems to be the epitome of a closely related trait and is strictly better to use when it is possible.

---

<div class="post-metadata">

**Author:** ![bluss](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/bluss/32/2264_2.png) [@bluss](https://internals.rust-lang.org/u/bluss)\
**Post date:** [February 18, 2016, 7:50pm UTC](https://internals.rust-lang.org/t/assuming-clone-clone-is-equivalent-to-copy-semantics-for-all-instances-of-copy/3148/14 "2016-02-18T19:50:50Z")

</div>

I agree that the optimizer shouldn’t outright replace a call to `.clone()` with something else.

---

<div class="post-metadata">

**Author:** ![nikomatsakis](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/nikomatsakis/32/5410_2.png) [@nikomatsakis](https://internals.rust-lang.org/u/nikomatsakis)\
**Post date:** [February 18, 2016, 7:53pm UTC](https://internals.rust-lang.org/t/assuming-clone-clone-is-equivalent-to-copy-semantics-for-all-instances-of-copy/3148/15 "2016-02-18T19:53:11Z")

</div>

Aren’t we talking about library code here? I was not saying that the optimizer would go and remove calls to `clone`.

---

<div class="post-metadata">

**Author:** ![arielb1](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/arielb1/32/3044_2.png) [@arielb1](https://internals.rust-lang.org/u/arielb1)\
**Post date:** [February 18, 2016, 8:11pm UTC](https://internals.rust-lang.org/t/assuming-clone-clone-is-equivalent-to-copy-semantics-for-all-instances-of-copy/3148/16 "2016-02-18T20:11:19Z")

</div>

At least, if we do it, make `Clone` a lang-item. If we have the rule well-specified enough this should not pose a _conceptual_ problem (in the same way that integers being able to overflow is not a _conceptual_ problem, just a semantics problem).

---

<div class="post-metadata">

**Author:** ![briansmith](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/briansmith/32/1306_2.png) [@briansmith](https://internals.rust-lang.org/u/briansmith)\
**Post date:** [February 18, 2016, 8:23pm UTC](https://internals.rust-lang.org/t/assuming-clone-clone-is-equivalent-to-copy-semantics-for-all-instances-of-copy/3148/17 "2016-02-18T20:23:51Z")

</div>

> [@nikomatsakis](#):
>
> Aren't we talking about library code here? I was not saying that the optimizer would go and remove calls to clone.

The optimizer sure can remove calls to `clone` and I hope it already does! In particular, the compiler should be able to determine when a `clone()` implementation is equivalent to a `memcpy` and then optimize it away to `memcpy`. That's optimizer 101 in 2016 and any case the compiler doesn't already do that should be filed as a bug against the compiler. The only issue is whether the optimizer can cheat and consider a `Copy` annotation to be sufficient evidence to avoid analyzing the implementation of the `clone()` method. Actually, having thought about it now, I don't think it is necessary for it to do that, because it should easily be able to infer that from the implementation of `clone()`.

---

<div class="post-metadata">

**Author:** ![nikomatsakis](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/nikomatsakis/32/5410_2.png) [@nikomatsakis](https://internals.rust-lang.org/u/nikomatsakis)\
**Post date:** [February 18, 2016, 8:26pm UTC](https://internals.rust-lang.org/t/assuming-clone-clone-is-equivalent-to-copy-semantics-for-all-instances-of-copy/3148/18 "2016-02-18T20:26:53Z")

</div>

@briansmith that is a bit different – if it can reason about the actual code being called, it can do the usual optimizations, but the author can’t tell the difference. The question here is could the compiler opt to change

```
x.clone()

```

so that it in fact just does a mem copy even if `clone` contains (or may contain, because we don’t know what it does) a `println!` (and hence the println would never fire).

Probably this is a bridge too far, though it’s certainly not _that_ different from libstd APIs selecting a specialized impl. You can use this to argue many possible conclusions, I think. 🙂

---

<div class="post-metadata">

**Author:** ![briansmith](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/briansmith/32/1306_2.png) [@briansmith](https://internals.rust-lang.org/u/briansmith)\
**Post date:** [February 18, 2016, 8:43pm UTC](https://internals.rust-lang.org/t/assuming-clone-clone-is-equivalent-to-copy-semantics-for-all-instances-of-copy/3148/19 "2016-02-18T20:43:11Z")

</div>

My point is that, given a reasonable optimizer, then there will be no significant advantage to recognizing that the type implements `Copy` so the issue is moot, at least as far as `Clone`/`Copy` goes.

There might be a more general issue for specialization, but it shouldn’t be an issue for `Clone`/`Copy`.

At least, it would be helpful to see a realistic example of an implementation of `clone()` that the compiler can’t reasonably be expected to optimize to `memcpy()` even without knowing anything special about `Copy`.

---

<div class="post-metadata">

**Author:** ![bluss](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/bluss/32/2264_2.png) [@bluss](https://internals.rust-lang.org/u/bluss)\
**Post date:** [February 19, 2016, 1:42am UTC](https://internals.rust-lang.org/t/assuming-clone-clone-is-equivalent-to-copy-semantics-for-all-instances-of-copy/3148/20 "2016-02-19T01:42:37Z")

</div>

[Sure here’s such an example (playground link).](https://play.rust-lang.org/?gist=3d3f9c1476253509cde5&version=nightly).

The function `copy_composite` is compiled to memcpy, the `clone_composite` function is not. Use Release mode and look at ASM to verify.

The reason seems to be that in `clone_composite`, it’s using the derived Clone impl that assigns fields separately, and the other struct uses a Copy-based Clone impl. It’s a bit disappointing that such a simple example (no struct padding) fails. The simpler example of a single-field struct is of course successful.

[Next page](https://internals.rust-lang.org/t/assuming-clone-clone-is-equivalent-to-copy-semantics-for-all-instances-of-copy/3148.md?page=2)
