Skip to content

Commit

Permalink
refactor: merge annotation.py and local.py (#3456)
Browse files Browse the repository at this point in the history
This commit merges two phases of analysis: typechecking
(`vyper/semantics/analysis/local.py`) and annotation of ast nodes with
types (`vyper/semantics/analysis/annotation.py`). This is both for
consistency with how it is done for `analysis/module.py`, and also
because it increases internal consistency, as some typechecking was
being done in `annotation.py`. It is also easier to maintain this way,
since bugfixes or modifications to typechecking need to only be done in
one place, rather than two passes. Lastly, it also probably improves
performance, because it collapses two passes into one (and calls
`get_*_type_from_node` less often).

This commit also fixes a bug with accessing the iterator when the
iterator is an empty list literal.
  • Loading branch information
tserg authored Oct 19, 2023
1 parent 5482bbc commit 3ba1412
Show file tree
Hide file tree
Showing 8 changed files with 420 additions and 518 deletions.
3 changes: 2 additions & 1 deletion tests/parser/syntax/test_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -305,8 +305,9 @@ def foo():
"""
@external
def foo():
x: DynArray[uint256, 3] = [1, 2, 3]
for i in [[], []]:
pass
x = i
""",
]

Expand Down
5 changes: 5 additions & 0 deletions vyper/ast/nodes.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ class Expr(VyperNode):

class UnaryOp(ExprNode):
op: VyperNode = ...
operand: VyperNode = ...

class USub(VyperNode): ...
class Not(VyperNode): ...
Expand All @@ -165,12 +166,15 @@ class BitXor(VyperNode): ...

class BoolOp(ExprNode):
op: VyperNode = ...
values: list[VyperNode] = ...

class And(VyperNode): ...
class Or(VyperNode): ...

class Compare(ExprNode):
op: VyperNode = ...
left: VyperNode = ...
right: VyperNode = ...

class Eq(VyperNode): ...
class NotEq(VyperNode): ...
Expand All @@ -179,6 +183,7 @@ class LtE(VyperNode): ...
class Gt(VyperNode): ...
class GtE(VyperNode): ...
class In(VyperNode): ...
class NotIn(VyperNode): ...

class Call(ExprNode):
args: list = ...
Expand Down
2 changes: 1 addition & 1 deletion vyper/builtins/_signatures.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ def decorator_fn(self, node, context):
return decorator_fn


class BuiltinFunction:
class BuiltinFunction(VyperType):
_has_varargs = False
_kwargs: Dict[str, KwargSettings] = {}

Expand Down
6 changes: 6 additions & 0 deletions vyper/builtins/functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -475,6 +475,12 @@ def evaluate(self, node):

return vy_ast.Int.from_node(node, value=length)

def infer_arg_types(self, node):
self._validate_arg_types(node)
# return a concrete type
typ = get_possible_types_from_node(node.args[0]).pop()
return [typ]

def build_IR(self, node, context):
arg = Expr(node.args[0], context).ir_node
if arg.value == "~calldata":
Expand Down
283 changes: 0 additions & 283 deletions vyper/semantics/analysis/annotation.py

This file was deleted.

19 changes: 13 additions & 6 deletions vyper/semantics/analysis/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,17 @@ class VyperNodeVisitorBase:
def visit(self, node, *args):
if isinstance(node, self.ignored_types):
return

# iterate over the MRO until we find a matching visitor function
# this lets us use a single function to broadly target several
# node types with a shared parent
for class_ in node.__class__.mro():
ast_type = class_.__name__
visitor_fn = getattr(self, f"visit_{ast_type}", None)
if visitor_fn:
return visitor_fn(node, *args)

node_type = type(node).__name__
visitor_fn = getattr(self, f"visit_{node_type}", None)
if visitor_fn is None:
raise StructureException(
f"Unsupported syntax for {self.scope_name} namespace: {node_type}", node
)
visitor_fn(node, *args)
raise StructureException(
f"Unsupported syntax for {self.scope_name} namespace: {node_type}", node
)
Loading

0 comments on commit 3ba1412

Please sign in to comment.