Skip to content

Propagate type aliases from base to derived contracts #2693

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 7, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 12 additions & 5 deletions slither/solc_parsing/declarations/contract.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ def __init__(
self._enumsNotParsed: List[Dict] = []
self._structuresNotParsed: List[Dict] = []
self._usingForNotParsed: List[Dict] = []
self._customErrorParsed: List[Dict] = []
self._customErrorsNotParsed: List[Dict] = []

self._functions_parser: List[FunctionSolc] = []
self._modifiers_parser: List[ModifierSolc] = []
Expand Down Expand Up @@ -277,13 +277,20 @@ def _parse_contract_items(self) -> None:
elif item[self.get_key()] == "UsingForDirective":
self._usingForNotParsed.append(item)
elif item[self.get_key()] == "ErrorDefinition":
self._customErrorParsed.append(item)
self._customErrorsNotParsed.append(item)
elif item[self.get_key()] == "UserDefinedValueTypeDefinition":
self._parse_type_alias(item)
else:
raise ParsingError("Unknown contract item: " + item[self.get_key()])
return

def parse_type_alias(self) -> None:
# We keep parse_ in the name just to keep the naming convention even if we already parsed them initially.
# Here we only update the current contract type_aliases_as_dict with the fathers' values
# It's useful to keep using the same pattern anyway as we know all the fathers have been analyzed
for father in self._contract.inheritance_reverse:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is reversed right? I think the most recent ancestor should take priority

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

type alias declarations can not be shadowed or you get an identifier already declared error so i believe you get the same result

self._contract.type_aliases_as_dict.update(father.type_aliases_as_dict)

def _parse_type_alias(self, item: Dict) -> None:
assert "name" in item
assert "underlyingType" in item
Expand Down Expand Up @@ -337,9 +344,9 @@ def parse_custom_errors(self) -> None:
for father in self._contract.inheritance_reverse:
self._contract.custom_errors_as_dict.update(father.custom_errors_as_dict)

for custom_error in self._customErrorParsed:
for custom_error in self._customErrorsNotParsed:
self._parse_custom_error(custom_error)
self._customErrorParsed = []
self._customErrorsNotParsed = []

def parse_state_variables(self) -> None:
for father in self._contract.inheritance_reverse:
Expand Down Expand Up @@ -793,7 +800,7 @@ def delete_content(self) -> None:
self._enumsNotParsed = []
self._structuresNotParsed = []
self._usingForNotParsed = []
self._customErrorParsed = []
self._customErrorsNotParsed = []

def _handle_comment(self, attributes: Dict) -> None:
"""
Expand Down
1 change: 1 addition & 0 deletions slither/solc_parsing/slither_compilation_unit_solc.py
Original file line number Diff line number Diff line change
Expand Up @@ -730,6 +730,7 @@ def _parse_struct_var_modifiers_functions(self, contract: ContractSolc) -> None:
contract.parse_modifiers()
contract.parse_functions()
contract.parse_custom_errors()
contract.parse_type_alias()
contract.set_is_analyzed(True)

def _analyze_struct_events(self, contract: ContractSolc) -> None:
Expand Down
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
{
"OtherTest": {
"myfunc()": "digraph{\n0[label=\"Node Type: ENTRY_POINT 0\n\"];\n0->1;\n1[label=\"Node Type: NEW VARIABLE 1\n\"];\n}\n"
"myfunc()": "digraph{\n0[label=\"Node Type: ENTRY_POINT 0\n\"];\n0->1;\n1[label=\"Node Type: NEW VARIABLE 1\n\"];\n1->2;\n2[label=\"Node Type: NEW VARIABLE 2\n\"];\n}\n"
},
"DeleteTest": {}
"DeleteTest": {},
"A": {
"q()": "digraph{\n0[label=\"Node Type: ENTRY_POINT 0\n\"];\n0->1;\n1[label=\"Node Type: EXPRESSION 1\n\"];\n}\n"
},
"B": {
"q()": "digraph{\n0[label=\"Node Type: ENTRY_POINT 0\n\"];\n0->1;\n1[label=\"Node Type: EXPRESSION 1\n\"];\n}\n",
"y()": "digraph{\n0[label=\"Node Type: ENTRY_POINT 0\n\"];\n0->1;\n1[label=\"Node Type: RETURN 1\n\"];\n}\n"
}
}
13 changes: 13 additions & 0 deletions tests/e2e/solc_parsing/test_data/type-aliases.sol
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,16 @@ contract OtherTest {
contract DeleteTest {
type Z is int;
}

contract A {
type MyU is uint256;
function q() public returns(MyU) {
MyU.wrap(1);
}
}

contract B is A {
function y() public returns(MyU) {
return MyU.wrap(343);
}
}