Skip to content

Commit

Permalink
fix: guard against iterating over empty list in for loop (#3197)
Browse files Browse the repository at this point in the history
without this guard, the typechecker tries every possible type, which
results in a bad error message
  • Loading branch information
tserg authored Jan 6, 2023
1 parent a16af3d commit 13ebcbe
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
14 changes: 14 additions & 0 deletions tests/parser/features/iteration/test_for_in_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -658,6 +658,20 @@ def foo():
for i in range(0):
pass
""",
"""
@external
def foo():
for i in []:
pass
""",
"""
FOO: constant(DynArray[uint256, 3]) = []
@external
def foo():
for i in FOO:
pass
""",
(
"""
@external
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 @@ -383,6 +383,9 @@ def visit_For(self, node):

else:
# iteration over a variable or literal list
if isinstance(node.iter, vy_ast.List) and len(node.iter.elements) == 0:
raise StructureException("For loop must have at least 1 iteration", node.iter)

type_list = [
i.value_type
for i in get_possible_types_from_node(node.iter)
Expand Down

0 comments on commit 13ebcbe

Please sign in to comment.