Skip to content

Commit

Permalink
fix: enforce loop iterators are valid names (#3242)
Browse files Browse the repository at this point in the history
Co-authored-by: Tanguy Rocher <tanguy.rocher@protonmail.com>
  • Loading branch information
trocher and Tanguy Rocher authored Jan 20, 2023
1 parent d4f04c6 commit 8316720
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
20 changes: 20 additions & 0 deletions tests/parser/syntax/test_for_range.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,26 @@
import pytest

from vyper import compiler
from vyper.exceptions import StructureException

fail_list = [
(
"""
@external
def foo():
for a[1] in range(10):
pass
""",
StructureException,
)
]


@pytest.mark.parametrize("bad_code", fail_list)
def test_range_fail(bad_code):
with pytest.raises(bad_code[1]):
compiler.compile_code(bad_code[0])


valid_list = [
"""
Expand Down
3 changes: 3 additions & 0 deletions vyper/semantics/analysis/local.py
Original file line number Diff line number Diff line change
Expand Up @@ -434,6 +434,9 @@ def visit_For(self, node):
)
self.expr_visitor.visit(node.iter)

if not isinstance(node.target, vy_ast.Name):
raise StructureException("Invalid syntax for loop iterator", node.target)

for_loop_exceptions = []
iter_name = node.target.id
for type_ in type_list:
Expand Down

0 comments on commit 8316720

Please sign in to comment.