Explicit Copies Mode

The way I think something like this should work is that we should guarantee that copies will be elided in some core set of situations and then allow programmers to opt in to being explicit about their desire to copy everywhere else. That core set of situations could grow over time, but never shrink, so it would have to be shaped by what copies we think we can guarantee to eliminate. At minimum, it would start with things like "a conservative copy of a local var passed as a borrowed parameter will always be eliminated if the var does not escape and is not mutated during the call"; that would be semantically compatible with the current language. We could also experiment with a mode that simply doesn't do conservative copies at all, but I'm wary of adding more hard-fork modes like that.

Anyway, these guarantees would have to be real guarantees, and the compiler would be responsible for doing them even in debug builds. It would be very short-sighted to define the set of no-explicit-copy-required situations by what -O is able to eliminate in a particular release, because that cannot realistically be assumed to be stable. As nice as it would be to say that the optimizer should never regress by failing to eliminate a copy that it eliminated in a previous release, in practice that's essentially a "never rewrite an optimizer pass again" level of restriction.

16 Likes