Skip to content

Commit

Permalink
inform user if inheritance cannot be resolved (#1956)
Browse files Browse the repository at this point in the history
* inform user if inheritance cannot be resolved

* lint

* update explanation
  • Loading branch information
0xalpharush authored Jun 15, 2023
1 parent 414d976 commit 7cb7cb9
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 1 deletion.
11 changes: 10 additions & 1 deletion slither/solc_parsing/slither_compilation_unit_solc.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@
logger.setLevel(logging.INFO)


class InheritanceResolutionError(SlitherException):
pass


def _handle_import_aliases(
symbol_aliases: Dict, import_directive: Import, scope: FileScope
) -> None:
Expand Down Expand Up @@ -432,7 +436,12 @@ def parse_contracts(self) -> None: # pylint: disable=too-many-statements,too-ma
target = contract_parser.underlying_contract.file_scope.get_contract_from_name(
contract_name
)
assert target
if target == contract_parser.underlying_contract:
raise InheritanceResolutionError(
"Could not resolve contract inheritance. This is likely caused by an import renaming that collides with existing names (see https://github.com/crytic/slither/issues/1758)."
f"\n Try changing `contract {target}` ({target.source_mapping}) to a unique name."
)
assert target, f"Contract {contract_name} not found"
ancestors.append(target)
elif i in self._contracts_by_id:
ancestors.append(self._contracts_by_id[i])
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
import {ERC20 as ERC20_1} from "./import.sol";
contract ERC20 is ERC20_1 {}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
contract ERC20 {}
18 changes: 18 additions & 0 deletions tests/unit/core/test_error_messages.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
from pathlib import Path
import pytest


from slither import Slither
from slither.solc_parsing.slither_compilation_unit_solc import InheritanceResolutionError

TEST_DATA_DIR = Path(__file__).resolve().parent / "test_data"
INHERITANCE_ERROR_ROOT = Path(TEST_DATA_DIR, "inheritance_resolution_error")


def test_inheritance_resolution_error(solc_binary_path) -> None:
with pytest.raises(InheritanceResolutionError):
solc_path = solc_binary_path("0.8.0")
Slither(
Path(INHERITANCE_ERROR_ROOT, "contract_with_duplicate_names.sol").as_posix(),
solc=solc_path,
)

0 comments on commit 7cb7cb9

Please sign in to comment.