Associativity, commutivity, etc. for standard operators

Indeed, I'd expect x.clone() to semantically always behave like a function call. OTOH I don't want mere existence of Clone to make containers less efficient, so I think allowing libraries to treat copying and Copy::clone() as equivalent is a reasonable compromise.

Perhaps rustc should have a lint against impl Clone for CopyableType so that users don't waste effort on implementing an "unreliable" method?

Clippy has expl_impl_clone_on_copy, but it's "pedantic" and allowed by default.

Yes, that's what I was arguing, sorry if that wasn't clear. Library functions can produce incorrect results when using incorrectly implemented traits. For example, .sort() doesn't work correctly if the PartialOrd impl doesn't work correctly. A type's Clone impl must behave the same as its Copy impl if available, if not then the impl is incorrect. This means that e.g. Vec::<T>::clone only has to work correctly if T::clone is correct. Still it can't cause UB, just like .sort() with an incorrect PartialOrd impl can't cause UB.

With these rules it is possible to use Copy instead of Clone in the stdlib where available.

I've checked clippy's bug tracker, and the warning is not on by default, because #[derive(Copy, Clone)] may add incorrect bounds, so "manual" implementations are legitimately needed.

Thanks. :slight_smile: However, Stacked Borrows is but a tiny part of the Rust Abstract Machine -- and realistically it is not even that, more of a predecessor to that part. A lot of work is left to be done to properly specify the Rust Abstract Machine.

5 Likes

Give yourself and your team more credit! Formal proofs are difficult, and machine-checked formal proofs are even more difficult to get right! While you might only consider it to be a predecessor to a tiny part of the formal Rust Abstract Machine, it is an important part. The machine will eventually be the definition of what Rust actually is, which will permit formal proofs on the correctness of programs written in Rust. That's important!

4 Likes

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.