Skip to content
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

Fix IR for top level functions with using-for #2367

Merged
merged 6 commits into from
Mar 29, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
29 changes: 28 additions & 1 deletion slither/core/declarations/function_top_level.py
Original file line number Diff line number Diff line change
@@ -1,26 +1,53 @@
"""
Function module
"""
from typing import Dict, List, Tuple, TYPE_CHECKING
from typing import Dict, List, Tuple, TYPE_CHECKING, Union, Optional

from slither.core.declarations import Function
from slither.core.declarations.top_level import TopLevel
from slither.core.solidity_types.type import Type

if TYPE_CHECKING:
from slither.core.compilation_unit import SlitherCompilationUnit
from slither.core.scope.scope import FileScope
from slither.slithir.variables.state_variable import StateIRVariable

USING_FOR_KEY = Union[str, Type]
USING_FOR_ITEM = List[Union[Type, Function]]


class FunctionTopLevel(Function, TopLevel):
def __init__(self, compilation_unit: "SlitherCompilationUnit", scope: "FileScope") -> None:
super().__init__(compilation_unit)
self._scope: "FileScope" = scope
self._using_for_complete: Optional[Dict[USING_FOR_KEY, USING_FOR_ITEM]] = None
0xalpharush marked this conversation as resolved.
Show resolved Hide resolved

@property
def file_scope(self) -> "FileScope":
return self._scope

@property
def using_for_complete(self) -> Dict[USING_FOR_KEY, USING_FOR_ITEM]:
"""
Dict[Union[str, Type], List[Type]]: Dict of top level directive
Copy link
Contributor

Choose a reason for hiding this comment

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

This type doesn't match in the comment and the return signature is different than the definition. If we return an empty dict and not None, it isn't Optional

"""

def _merge_using_for(
uf1: Dict[USING_FOR_KEY, USING_FOR_ITEM], uf2: Dict[USING_FOR_KEY, USING_FOR_ITEM]
) -> Dict[USING_FOR_KEY, USING_FOR_ITEM]:
result = {**uf1, **uf2}
for key, value in result.items():
if key in uf1 and key in uf2:
result[key] = value + uf1[key]
return result

if self._using_for_complete is None:
result = {}
for uftl in self.file_scope.using_for_directives:
result = _merge_using_for(result, uftl.using_for)
self._using_for_complete = result
return self._using_for_complete

@property
def canonical_name(self) -> str:
"""
Expand Down
21 changes: 13 additions & 8 deletions slither/slithir/convert.py
Original file line number Diff line number Diff line change
Expand Up @@ -594,11 +594,13 @@
def propagate_types(ir: Operation, node: "Node"): # pylint: disable=too-many-locals
# propagate the type
node_function = node.function
using_for: Dict[USING_FOR_KEY, USING_FOR_ITEM] = (
node_function.contract.using_for_complete
if isinstance(node_function, FunctionContract)
else {}
)

using_for: Dict[USING_FOR_KEY, USING_FOR_ITEM] = {}
if isinstance(node_function, FunctionContract):
using_for = node_function.contract.using_for_complete
elif isinstance(node_function, FunctionTopLevel):
using_for = node_function.using_for_complete

if isinstance(ir, OperationWithLValue) and ir.lvalue:
# Force assignment in case of missing previous correct type
if not ir.lvalue.type:
Expand Down Expand Up @@ -1209,7 +1211,7 @@
internalcall.set_expression(ins.expression)
return internalcall

raise Exception(f"Not extracted {type(ins.called)} {ins}")
raise Exception(f"Not extracted {type(ins.called)} {ins}") # pylint: disable=bad-option-value

Check warning on line 1214 in slither/slithir/convert.py

View workflow job for this annotation

GitHub Actions / Lint Code Base

W0719: Raising too general exception: Exception (broad-exception-raised)
0xalpharush marked this conversation as resolved.
Show resolved Hide resolved


# endregion
Expand Down Expand Up @@ -1533,9 +1535,10 @@
internalcall.lvalue = None
return internalcall

lib_contract = None
if isinstance(destination, FunctionContract) and destination.contract.is_library:
lib_contract = destination.contract
else:
elif not isinstance(destination, FunctionTopLevel):
lib_contract = contract.file_scope.get_contract_from_name(str(destination))
if lib_contract:
lib_call = LibraryCall(
Expand Down Expand Up @@ -1563,7 +1566,9 @@
# We use contract_declarer, because Solidity resolve the library
# before resolving the inheritance.
# Though we could use .contract as libraries cannot be shadowed
contract = node.function.contract_declarer
contract = (
node.function.contract_declarer if isinstance(node.function, FunctionContract) else None
)
t = ir.destination.type
if t in using_for:
new_ir = look_for_library_or_top_level(contract, ir, using_for, t)
Expand Down
Loading