why can't the compiler detect this beforehand and produce a compilation error or warning?
The question assumes an incorrect premise. There is very little technically stopping a compiler writer from doing so. You'd likely want it to be a warning and not an error, but a compiler writer could easily put a threshold on what is an unreasonably enormous size for the total amount of local storage used by a function and give a warning if that size is exceeded.
Since your question is based on a false premise it has no answer. Ask a better question.
OK, if it is possible then why do compiler writers not implement this warning?
Whether your compiler is open source or closed source, whether the people developing it are paid or volunteers, all changes to software require time and effort. Both are in short supply.
Compiler warnings are features and therefore must be designed, specified, implemented, tested, debugged, shipped and maintained. All of these things are expensive in both time and effort, and all of them remove time and effort from other features.
If this feature doesn't exist in a compiler that you like, it's because the developers of that compiler had approximately one million better things to do. Of those one million better things to do, this year they'll probably manage to implement a couple dozen. If you want to advocate for your particular feature suggestion to move up the list from its current low position, start calling up compiler developers and make your case to them that this is the best possible way they could be spending their effort -- on creating a warning for a condition that is obviously bogus and that will crash immediately. You will likely not get far.
When considering what compiler warnings to implement, compiler writers such as myself think hard about what warnings to add to compilers. Good warnings identify situations that are:
- Legal; illegal code is of course an error.
- Detectable with low false positive rate; a warning that instructs developers to change correct, working code into broken code is a bad warning
- Misleading; said programmer should have a belief that the code does one thing when in fact it does another
- Plausible; a programmer should be reasonably likely to type in the warned-about code in a line-of-business program
- Undetectable by trivial testing; the program should run normally and do some subtly wrong thing. If the program crashes immediately then the warning is just finding the bug a few seconds before testing finds it, and that's not good value.
Your proposal meets the first two criteria. It arguably meets the third. It unambiguously fails to meet the fourth and fifth criteria. I would therefore reject your suggestion were I the compiler developer. I would spend my valuable time and effort adding features that were bigger benefit for the cost.