foundry-rs / foundry

Foundry is a blazing fast, portable and modular toolkit for Ethereum application development written in Rust.
https://getfoundry.sh
Apache License 2.0
8.31k stars 1.76k forks source link

Variable _1 is one level too deep in the stack #6703

Closed Renzo1 closed 10 months ago

Renzo1 commented 10 months ago

My foundry tests suddenly stopped working. Could anyone please help me understand why a previously functional foundry test might fail due to the following error?

Error:
Compiler run failed:
Error: Yul exception: Variable _1 is one level too deep in the stack [ RET memPtr _2 _5 zero_struct_VaultForTest_mpos _1 _6 _4 var_j expr_component_1 _31 _26 _10 _8 _32 _9 _35 _34 _15 _36 _33 _22 ]
Memory guard was present.
Memory guard was present.

I tried writing an empty test function (testMint) in efforts to debug the issue, and got thesame error.

function testMint() public {
        assert(true);
    }
DaniPopes commented 10 months ago

Can you please share more details? Forge version, Solc version, any minimal example...

Renzo1 commented 10 months ago
$ forge --version
forge 0.2.0 (bf56869 2023-07-05T00:07:51.006216400Z)

solc version: 0.8.17

Minimal example:
As mentioned in my initial post, I attempted to create an empty function and encountered the same error.

function testMint() public { assert(true); }


This is what my Foundry.toml looks like:
```toml
[profile.default]
src = 'contracts'
out = 'out'
libs = ['node_modules', 'lib']
test = 'test/foundry'
cache_path  = 'cache_forge'
optimizer = true
viaIR = true

[fuzz]
runs = 10

[invariant]
runs = 10
depth = 10

Apart from the above, I honestly can't pinpoint where things went wrong. One moment, all functions were working, then it stopped. It briefly worked again (when I commented out some lines in the testFunction I was testing), but then stopped again. I thought it might be something in the testFunction(), so I decided to create an empty function, but the problem persisted. I even test other functions that worked before, but they also stopped working. I have tried restarting my system, but the issue remains. I created a new Foundry project to check if it's a global issue, and everything worked fine. I then attempted to copy my project into this new project, but the error persisted.

Renzo1 commented 10 months ago

Thanks for the support. I have managed to resolve the issue.

The problem originated from a struct I added to my test.

Anyone facing the same error should inspect their error message for matching variables or datatypes, and then consider removing them from the test and trying again.

In my case it was VaultForTest struct.