Stack to deep when compiling inline assembly

Hello,

I seem to be having an issue running “solidity-coverage” i get the following error

CompilerError: Stack too deep when compiling inline assembly: Variable headStart is 1 slot(s) too deep inside the stack.

Error in plugin solidity-coverage: HardhatError: HH600: Compilation failed

I’m not sure i understand why, since when i run yarn compile i get no issues. Is there anyway i can debug? It doesn’t necessarily tell me what contract is having an issue compiling.

1 Like

There is a 16 local variable constraint in a function. Make sure you don’t have too many local variables.

2 Likes

Maybe you can have a look at this article:

1 Like

Ok so i figured it out, but i don’t understand it

The following code invokes this error…

function addMultipleVesters(
        string[] _name;
        address[] _vester,
        uint104[] _amount,
        uint8[] _percentInitialAmount,
        uint8[] _percentAmountPerWithdraw,
        uint8[] _percentBonus
) external onlyManager {
 ...
}

If i could get an explaination that would be AMAZING

I think for the piece of the code, it is ok.

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.

2 Likes

This error arises when using the pragma abi v2. Removing local variables in the contract implementation will not help here (assuming there are less than 16 which seems to be the case). Running compilation with optimization turned on removes the error, unfortunately this is not possible when running solidity coverage.

1 Like

if i may ask, what does solidity-coverage do?

It checks what percentage of your code is covered by your test suites.

1 Like

I get the error, but with optimization on. If I turn it off, the error goes away. (in 0.8.5)

Setting optimizer enabled to true will fix it.

solidity: {
    version: '0.8.0',
    settings: {
      optimizer: {
        runs: 200,
        enabled: true
      }
    }
  },
2 Likes

It trully works when optmizer is turned on