Here is a C program:
#include <stdio.h>
int main(void) { while (1) malloc(64); }
With clang, when compiled with -O0, this consumes all the memory on my system. When compiled with -O2, it runs forever in constant memory, because clang has optimized out the call to malloc. If I replace the call with, say, 'malloc((size_t)(-1) >> 1)', it even changes the output of the program.Here is another example:
int factorial(long long x) { return x * factorial(x-1); }
With gcc, when compiled with -O2, the factorial function can correctly compute the factorial of a very large number. When compiled with -O0, it cannot.I would call both of these optimizations, even though someone may rely on either for correctness. So I don't think tail calls are unique in this respect.
make the syntax for guaranteed-eliminated-tail-call different than a regular call
Agreed.
In a language which prefers diagnosing programmer error over punting both should be errors.
That someone is playing with fire. It's the kind of optimization that might be removed in a hotfix because it turns out to break something else.
It's even worse than relying on things like byte order -- and if you rely on them, you are definitely outside the realm of "C language", and into the realm of "Specifically LLVM v.37x patchlevel 2, on an SSE4 architecture"
If you worship food, that is. But you're right, people calling proper tail calls "an optimization" should be shot. Those who survive the subsequent generation scavenging should be shot again. :-)
With finite memory (that is, in the real world), a program that relies on tail recursion will blow up if TCE is not implemented even though it can run forever with TCE. Thus, it changes actual program results (rather than just runtime speed or amount of memory required), and cannot be considered just an "optimization", since it is functionally required.
And to future-language designers: Please, for the sake of $DIETY2, make the syntax for guaranteed-eliminated-tail-call different than a regular call: It's really a bad idea that a function such as:
cannot in general be TCEd (adding 0 to a floating point imporper denormal will make it proper), whereas can. I suggest "chain y" instead of "return y" so that the compiler can verify that a TCE can indeed happen.