foundry-rs / compilers

Utilities for working with native solc and compiling projects.
Apache License 2.0
73 stars 41 forks source link

Invalid contract flattening with matching interface names #34

Closed SidestreamColdMelon closed 4 months ago

SidestreamColdMelon commented 9 months ago

This issue is the successor of https://github.com/gakonst/ethers-rs/issues/2588, created after the deprecation announcement of ethers-rs. Other issues relevant to flattening are collected here: https://github.com/foundry-rs/foundry/issues/6022.

Problem

As previously described in https://github.com/foundry-rs/foundry/issues/4034, the latest version of forge (that now depends on compilers instead of ethers-rs) still doesn't properly handle situations when multiple files contain interfaces with the same name.

Example

Given two files FileA.sol and FileB.sol

// FileA.sol
pragma solidity ^0.8.10;
import "./FileB.sol";

interface FooBar {
    function foo() external;
}
contract A {
    function execute() external {
        FooBar(address(0)).foo();
    }
}
// FileB.sol
pragma solidity ^0.8.10;

interface FooBar {
    function bar() external;
}
contract B {
    function execute() external {
        FooBar(address(0)).bar();
    }
}

Flattening will currently produce:

pragma solidity ^0.8.10;

interface FooBar {
    function bar() external;
}
contract B {
    function execute() external {
        FooBar(address(0)).bar();
    }
}

interface FooBar { // here is the conflict with the interface declared above
    function foo() external;
}
contract A {
    function execute() external {
        FooBar(address(0)).foo();
    }
}

Instead, the desired outcome is:

pragma solidity ^0.8.10;

interface FooBar_2 {
    function bar() external;
}
contract B {
    function execute() external {
        FooBar_2(address(0)).bar();
    }
}

interface FooBar_1 { // conflict avoided since the interface above was renamed before flattening
    function foo() external;
}
contract A {
    function execute() external {
        FooBar_1(address(0)).foo();
    }
}

Notes

SidestreamColdMelon commented 4 months ago

Closed by https://github.com/foundry-rs/compilers/pull/52