Skip to content

Commit

Permalink
remove AST conversion
Browse files Browse the repository at this point in the history
rather than converting positional arg ASTs to kwarg ASTs, we handle
kwargs alongside the existing positional arg logic
  • Loading branch information
z80dev committed Sep 26, 2024
1 parent 7d58cc2 commit 4969586
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 27 deletions.
8 changes: 7 additions & 1 deletion vyper/codegen/stmt.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,13 @@ def parse_If(self):
def parse_Log(self):
event = self.stmt._metadata["type"]

args = [Expr(arg.value, self.context).ir_node for arg in self.stmt.value.keywords]
args = []
if len(self.stmt.value.keywords) > 0:
# keyword arguments
args = [Expr(arg.value, self.context).ir_node for arg in self.stmt.value.keywords]
else:
# positional arguments
args = [Expr(arg, self.context).ir_node for arg in self.stmt.value.args]

topic_ir = []
data_ir = []
Expand Down
9 changes: 7 additions & 2 deletions vyper/semantics/analysis/local.py
Original file line number Diff line number Diff line change
Expand Up @@ -812,8 +812,13 @@ def visit_Call(self, node: vy_ast.Call, typ: VyperType) -> None:
elif is_type_t(func_type, EventT):
# event ctors
expected_types = func_type.typedef.arguments.values() # type: ignore
for kwarg, arg_type in zip(node.keywords, expected_types):
self.visit(kwarg.value, arg_type)
# Handle keyword args if present, otherwise use positional args
if len(node.keywords) > 0:
for kwarg, arg_type in zip(node.keywords, expected_types):
self.visit(kwarg.value, arg_type)
else:
for arg, typ in zip(node.args, expected_types):
self.visit(arg, typ)
elif is_type_t(func_type, StructT):
# struct ctors
expected_types = func_type.typedef.members.values() # type: ignore
Expand Down
48 changes: 24 additions & 24 deletions vyper/semantics/types/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -282,32 +282,32 @@ def from_EventDef(cls, base_node: vy_ast.EventDef) -> "EventT":
return cls(base_node.name, members, indexed, base_node)

def _ctor_call_return(self, node: vy_ast.Call) -> None:
# Handle positional args by converting them to kwargs
# validate keyword arguments if provided
if len(node.keywords) > 0:
return self._ctor_call_return_with_kwargs(node)

# warn about positional argument depreciation
msg = "Instantiating events with positional arguments is "
msg += "deprecated as of v0.4.1 and will be disallowed "
msg += "in a future release. Use kwargs instead eg. "
msg += "Foo(a=1, b=2)"

vyper_warn(msg, node)

validate_call_args(node, len(self.arguments))
for arg, expected in zip(node.args, self.arguments.values()):
validate_expected_type(arg, expected)


def _ctor_call_return_with_kwargs(self, node: vy_ast.Call) -> None:
# TODO: Remove block when positional args are removed
if len(node.args) > 0:
if len(node.keywords) > 0:
# can't mix args and kwargs
raise InstantiationException(
"Event instantiation requires either all positional arguments "
"or all keyword arguments",
node,
)

msg = "Instantiating events with positional arguments is "
msg += "deprecated as of v0.4.1 and will be disallowed "
msg += "in a future release. Use kwargs instead eg. "
msg += "Foo(a=1, b=2)"

vyper_warn(msg, node)

# convert positional args to keywords
kw_list = []
for kw, val in zip(self.arguments.keys(), node.args):
kw_node = vy_ast.keyword(arg=kw, value=val)
kw_node.set_parent(node)
kw_list.append(kw_node)
node.keywords = kw_list
node.args = []
# can't mix args and kwargs
raise InstantiationException(
"Event instantiation requires either all positional arguments "
"or all keyword arguments",
node,
)

# manually validate kwargs for better error messages instead of
# relying on `validate_call_args` (same as structs)
Expand Down

0 comments on commit 4969586

Please sign in to comment.