Rust should have stable tail calls

46 points by LesleyLai


polywolf

Cool to see this gain momentum! Some thoughts on the missing arch support

Whenever a function in a different object (e.g. a dynamic library) is called, the call sequence will swap out the caller's TOC for the callee's TOC, call the function, and then switch back to the caller's TOC. But, with a tail call, the callee never returns to the caller and restoring the TOC value never happens.

Might it be possible to, when calling one of these functions, generate something that looks like:

That is, we're preserving the most important aspects of a tail call (the fact it re-uses a stack frame) while maintaining correctness of the external call. This slight extra instruction cost would also only be paid when crossing the dynamic linking boundary, which should hopefully be rare.

Now, I know nothing about powerpc or the compiler backends for it, so this may well be unworkable in practice for a multitude of reasons :P

olliej

This talks about the performance implications, but tail calls can be required for correctness. e.g. this trivial function (because I can't be bothered remembering the better examples)

len_tail [] acc = acc
len_tail (h:t) acc = len_tail t (acc + 1)
len l = len_tail l 0

is only correct if tail recursion is guaranteed