Skip to content

Commit

Permalink
add tests for iterators imported from modules
Browse files Browse the repository at this point in the history
  • Loading branch information
charles-cooper committed Feb 9, 2024
1 parent 135fc4e commit 3f514ae
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 2 deletions.
56 changes: 55 additions & 1 deletion tests/functional/codegen/features/iteration/test_for_in_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

import pytest

from vyper.compiler import compile_code
from vyper.exceptions import (
ArgumentException,
ImmutableViolation,
Expand Down Expand Up @@ -841,6 +842,59 @@ def foo():
]


# TODO: move these to tests/functional/syntax
@pytest.mark.parametrize("code,err", BAD_CODE, ids=bad_code_names)
def test_bad_code(assert_compile_failed, get_contract, code, err):
assert_compile_failed(lambda: get_contract(code), err)
with pytest.raises(err):
compile_code(code)


def test_iterator_modification_module_attribute(make_input_bundle):
# test modifying iterator via attribute
lib1 = """
queue: DynArray[uint256, 5]
"""
main = """
import lib1
initializes: lib1
@external
def foo():
for i: uint256 in lib1.queue:
lib1.queue.pop()
"""

input_bundle = make_input_bundle({"lib1.vy": lib1})

with pytest.raises(ImmutableViolation) as e:
compile_code(main, input_bundle=input_bundle)

assert e.value._message == "Cannot modify loop variable `queue`"


def test_iterator_modification_module_function_call(make_input_bundle):
lib1 = """
queue: DynArray[uint256, 5]
@internal
def popqueue():
self.queue.pop()
"""
main = """
import lib1
initializes: lib1
@external
def foo():
for i: uint256 in lib1.queue:
lib1.popqueue()
"""

input_bundle = make_input_bundle({"lib1.vy": lib1})

with pytest.raises(ImmutableViolation) as e:
compile_code(main, input_bundle=input_bundle)

assert e.value._message == "Cannot modify loop variable `queue`"
5 changes: 4 additions & 1 deletion vyper/semantics/analysis/local.py
Original file line number Diff line number Diff line change
Expand Up @@ -431,7 +431,10 @@ def _analyse_list_iter(self, iter_node, target_type):
raise StructureException("For loop must have at least 1 iteration", iter_node)
iter_type = SArrayT(target_type, len_)
else:
iter_type = get_exact_type_from_node(iter_node)
try:
iter_type = get_exact_type_from_node(iter_node)
except (InvalidType, StructureException):
raise InvalidType("Not an iterable type", iter_node)

# CMC 2024-02-09 TODO: use validate_expected_type once we have DArrays
# with generic length.
Expand Down

0 comments on commit 3f514ae

Please sign in to comment.