feat(forge): new flatten implementation#6936
Conversation
forge) New flatten implementationforge) New flatten implementation
|
(You have to write "closes" before each PR, preferably each on a new line) |
|
@klkvr Great work, congrats! I run your version of flatten on a project that uses prb math (both UD60x18 and SD59x18) and I can see that the resulted file has some issues renamed variables in assembly blocks. For example, in the code below uUNIT was not replaced with the renamed version (uUNIT_1, _2, ...) Is this an easy fix for you? |
forge) New flatten implementationforge): new flatten implementation
|
@avniculae ah, assembly blocks are not in scope of renaming atm It shouldn't be hard to fix, could you please drop a link to the contract you are flattening or a minimal repro so it's easier to debug? |
Thanks for letting me know! I run it on a private repo, but I think this file should represent a good test. |
|
@klkvr another point worth mentioning: docs comments are also not updated during flattening, preventing the final file from compiling successfully. For example: |
|
@klkvr another point: if the contract renames an import (e.g., |
|
@avniculae thanks for all the points, will try to fix all of them regarding imports renaming could you please give a bit more context as this case was addressed directly during development and it shouldn't fail which entity is the mulUD? I mean variable/enum/struct etc |
|
@klkvr I am experiencing issues with function renaming (mul is a function here) Later Edit: I am experiencing the same issue with some imported variables as well, which are not renamed in the original import at all. Does the code work for constant variables as well (that are freely declared in a file -- i.e. not within a contract?) Later Edit 2: I think the bigger problem here might be that identifiers are not replaced when prepended or appended with characters such as |
|
I can't reproduce this behavior locally, maybe I got something wrong? mul.sol: function mul(uint256 a, uint256 b) pure returns(uint256) {
return a * b;
}test.sol pragma solidity 0.8.6;
import {mul as mulUD} from "./mul.sol";
contract Test {
function foo() public {
mulMD(1, 2);
}
}And test.sol is getting flattened to pragma solidity 0.8.6;
function mul(uint256 a, uint256 b) pure returns(uint256) {
return a * b;
}
uint256 constant someVar = 1;
contract Test {
function foo(uint256 a) public {
mul(1, 2);
}
}which seems correct |
Co-authored-by: DaniPopes <[email protected]>
|
@klkvr Here's a small counterexample: SFixed.sol UFixed.sol Contract.sol In the flattened version, |
|
hey @avniculae thank you once again for all the edge cases I belive that all of them should be fixed now, could you please pull this PRs version and try once again if they are working for you? |
|
@klkvr Thanks for pushing these fixes! I re-run the latest version and I now get the following error: However, it's hard to tell what's causing this error since the flattened code has over 11k loc, and I wasn't able to reproduce it with small examples |
|
@avniculae is this repo public? |
|
@avniculae this happens while updating |
|
@avniculae any chance you can share names of other dependencies besides prb-math? |
|
@klkvr I think the main ones are prb-math and @openzeppelin/contracts/ |
Ref foundry-rs/foundry#6936 (comment) Not sure why exactly this unwrap is failing, adding `None` variant handling and some logs to debug it. --------- Co-authored-by: Matthias Seitz <[email protected]>
|
This includes a ton of improvements, but might still have some limitations which we can fix as soon as they get reported. |
Support for more flattening edge cases. Resolves foundry-rs/foundry#6936 (comment) and foundry-rs/foundry#6936 (comment) We still don't rename stuff in yul blocks. prb-math turned out to be a great codebase to test flattening against as it has a lot of unusual patterns (qulified imports, user-defined functions on user-defined types, etc). Currently, their contracts flattened by this flattener are only having troubles with yul code blocks
Ref foundry-rs/foundry#6936 (comment) Not sure why exactly this unwrap is failing, adding `None` variant handling and some logs to debug it. --------- Co-authored-by: Matthias Seitz <[email protected]>
Support for more flattening edge cases. Resolves foundry-rs/foundry#6936 (comment) and foundry-rs/foundry#6936 (comment) We still don't rename stuff in yul blocks. prb-math turned out to be a great codebase to test flattening against as it has a lot of unusual patterns (qulified imports, user-defined functions on user-defined types, etc). Currently, their contracts flattened by this flattener are only having troubles with yul code blocks
Ref foundry-rs/foundry#6936 (comment) Not sure why exactly this unwrap is failing, adding `None` variant handling and some logs to debug it. --------- Co-authored-by: Matthias Seitz <[email protected]>
Motivation
Ref foundry-rs/compilers#52
Full rewrite of flattening utilizing native solc AST.
forge flattencommand, then I run forge build, it shows: Multiple SPDX license identifiers found in source file. #3678forge flattenwith renamed interface import does not compile #3265forge flattendoes not support qualified imports #2545Solution
It seems that we don't have any tests for
forge flattenbut creating PR with patched dep to run CI just in case.