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: static list of structs as arg #2515

Merged
merged 3 commits into from
Oct 26, 2021
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
33 changes: 33 additions & 0 deletions tests/parser/types/test_lists.py
Original file line number Diff line number Diff line change
Expand Up @@ -362,3 +362,36 @@ def foo() -> (uint256, uint256[3], uint256[2]):
"""
c = get_contract(code)
assert c.foo() == [666, [1, 2, 3], [88, 12]]


def test_list_of_structs_arg(get_contract):
code = """
struct Foo:
x: uint256
y: uint256

@external
def bar(_baz: Foo[3]) -> uint256:
sum: uint256 = 0
for i in range(3):
sum += _baz[i].x * _baz[i].y
return sum
"""
c = get_contract(code)
c_input = [[x, y] for x, y in zip(range(3), range(3))]
assert c.bar(c_input) == 5 # 0 * 0 + 1 * 1 + 2 * 2


def test_list_of_structs_arg_with_dynamic_type(get_contract):
code = """
struct Foo:
x: uint256
_msg: String[32]

@external
def bar(_baz: Foo[3]) -> String[96]:
return concat(_baz[0]._msg, _baz[1]._msg, _baz[2]._msg)
"""
c = get_contract(code)
c_input = [[i, msg] for i, msg in enumerate(("Hello ", "world", "!!!!"))]
assert c.bar(c_input) == "Hello world!!!!"
2 changes: 1 addition & 1 deletion vyper/old_codegen/types/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ def canonicalize_type(t, is_indexed=False):
return byte_type

if isinstance(t, ListType):
if not isinstance(t.subtype, (ListType, BaseType)):
if not isinstance(t.subtype, (ListType, BaseType, StructType)):
raise InvalidType(f"List of {t.subtype} not allowed")
return canonicalize_type(t.subtype) + f"[{t.count}]"

Expand Down