inform user if inheritance cannot be resolved (#1956)

* inform user if inheritance cannot be resolved

* lint

* update explanation
pull/1972/head
alpharush 1 year ago committed by GitHub
parent 414d976f30
commit 7cb7cb94ad
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 11
      slither/solc_parsing/slither_compilation_unit_solc.py
  2. 2
      tests/unit/core/test_data/inheritance_resolution_error/contract_with_duplicate_names.sol
  3. 1
      tests/unit/core/test_data/inheritance_resolution_error/import.sol
  4. 18
      tests/unit/core/test_error_messages.py

@ -33,6 +33,10 @@ logger = logging.getLogger("SlitherSolcParsing")
logger.setLevel(logging.INFO)
class InheritanceResolutionError(SlitherException):
pass
def _handle_import_aliases(
symbol_aliases: Dict, import_directive: Import, scope: FileScope
) -> None:
@ -432,7 +436,12 @@ Please rename it, this name is reserved for Slither's internals"""
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])

@ -0,0 +1,2 @@
import {ERC20 as ERC20_1} from "./import.sol";
contract ERC20 is ERC20_1 {}

@ -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,
)
Loading…
Cancel
Save