mirror of https://github.com/crytic/slither
Merge pull request #1625 from crytic/using-for-global-lib-collision
Fix using for global function name collisionpull/1741/head
commit
af39ca940c
@ -0,0 +1,59 @@ |
||||
import os |
||||
from contextlib import contextmanager |
||||
from pathlib import Path |
||||
from typing import Optional |
||||
|
||||
import pytest |
||||
from crytic_compile import CryticCompile |
||||
from crytic_compile.platform.solc_standard_json import SolcStandardJson |
||||
from solc_select import solc_select |
||||
|
||||
from slither import Slither |
||||
|
||||
|
||||
@contextmanager |
||||
def _select_solc_version(version: Optional[str]): |
||||
"""Selects solc version to use for running tests. |
||||
If no version is provided, latest is used.""" |
||||
if not version: |
||||
# This sorts the versions numerically |
||||
vers = sorted( |
||||
map( |
||||
lambda x: (int(x[0]), int(x[1]), int(x[2])), |
||||
map(lambda x: x.split(".", 3), solc_select.installed_versions()), |
||||
) |
||||
) |
||||
ver = list(vers)[-1] |
||||
version = ".".join(map(str, ver)) |
||||
env = dict(os.environ) |
||||
env_restore = dict(env) |
||||
env["SOLC_VERSION"] = version |
||||
os.environ.clear() |
||||
os.environ.update(env) |
||||
|
||||
yield version |
||||
|
||||
os.environ.clear() |
||||
os.environ.update(env_restore) |
||||
|
||||
|
||||
@pytest.fixture(name="select_solc_version") |
||||
def fixture_select_solc_version(): |
||||
return _select_solc_version |
||||
|
||||
|
||||
@pytest.fixture |
||||
def slither_from_dir(select_solc_version): |
||||
@contextmanager |
||||
def _slither_from_dir(directory: str, solc_version: Optional[str] = None): |
||||
"""Yields a Slither instance using solidity files in directory and solc_version. |
||||
Temporarily changes the solc-version temporary to solc_version. |
||||
""" |
||||
standard_json = SolcStandardJson() |
||||
for source_file in Path(directory).rglob("*.sol"): |
||||
standard_json.add_source_file(Path(source_file).as_posix()) |
||||
with select_solc_version(solc_version): |
||||
compilation = CryticCompile(standard_json) |
||||
yield Slither(compilation) |
||||
|
||||
return _slither_from_dir |
@ -0,0 +1,2 @@ |
||||
import "./MyTypeA/Type.sol"; |
||||
import "./MyTypeA/Math.sol"; |
@ -0,0 +1,4 @@ |
||||
import "./Type.sol"; |
||||
function unwrap(MyTypeA a) pure returns (int256) { |
||||
return MyTypeA.unwrap(a); |
||||
} |
@ -0,0 +1,5 @@ |
||||
import "./Type.sol"; |
||||
|
||||
function mul(MyTypeA a, MyTypeA b) pure returns (MyTypeA) { |
||||
return MyTypeA.wrap(MyTypeA.unwrap(a) * MyTypeA.unwrap(b)); |
||||
} |
@ -0,0 +1,6 @@ |
||||
import "./Casting.sol" as C; |
||||
import "./Math.sol" as M; |
||||
|
||||
type MyTypeA is int256; |
||||
|
||||
using {M.mul, C.unwrap} for MyTypeA global; |
@ -0,0 +1,2 @@ |
||||
import "./MyTypeB/Type.sol"; |
||||
import "./MyTypeB/Math.sol"; |
@ -0,0 +1,4 @@ |
||||
import "./Type.sol"; |
||||
function unwrap(MyTypeB a) pure returns (uint256) { |
||||
return MyTypeB.unwrap(a); |
||||
} |
@ -0,0 +1,6 @@ |
||||
import "./Type.sol"; |
||||
|
||||
function mul(MyTypeB a, MyTypeB b) pure returns (MyTypeB) { |
||||
return MyTypeB.wrap(MyTypeB.unwrap(a) * MyTypeB.unwrap(b)); |
||||
} |
||||
|
@ -0,0 +1,6 @@ |
||||
import "./Casting.sol" as C; |
||||
import "./Math.sol" as M; |
||||
|
||||
type MyTypeB is uint256; |
||||
|
||||
using {M.mul, C.unwrap} for MyTypeB global; |
@ -0,0 +1,7 @@ |
||||
import "./MyTypeB.sol"; |
||||
|
||||
contract UsingForGlobalTopLevelCollision { |
||||
function mulAndUnwrap(MyTypeB x, MyTypeB y) external pure returns (uint256 z) { |
||||
z = x.mul(y).unwrap(); |
||||
} |
||||
} |
Loading…
Reference in new issue