"Stack too deep" when compiling inline assembly

The “stack too deep” error is a limitation of the current code generator. The EVM stack only has 16 slots and that’s sometimes not enough to fit all the local variables, parameters and/or return variables. The solution is to move some of them to memory, which is more expensive but at least makes your code compile.

Without being able to do this the compiler can’t do much else than just stop and give up when it gets into such a situation. It also can’t really tell you exactly where the problem is because it’s not caused by any particular line of code. It could potentially show you the general area that was being compiled when it happened but it would require adapting error reporting just for this particular type of error and it’s really just a half-measure. It’s better to focus on finishing the new code generator and proper support for moving stuff to memory. Until then unfortunately you have work around this problem manually by rearranging your code in a way that makes it take less stack space.

Having tons of parameters is generally not a considered a good style anyway and grouping them into structs is recommended.

Also note that some types require more than 1 stack slot so the limit is not exactly 16 locals/parameters. It also depends on what types you use.

4 Likes