Skip to content

Commit

Permalink
fix: guard against kwargs for range expressions with two arguments (#…
Browse files Browse the repository at this point in the history
…3551)

and slight refactor -- extract `node.iter` expr to `range_` for clarity

---------

Co-authored-by: Charles Cooper <[email protected]>
  • Loading branch information
tserg and charles-cooper authored Aug 7, 2023
1 parent 728a276 commit 43c8d85
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 4 deletions.
21 changes: 20 additions & 1 deletion tests/parser/syntax/test_for_range.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,26 @@ def foo():
pass
""",
StructureException,
)
),
(
"""
@external
def bar():
for i in range(1,2,bound=2):
pass
""",
StructureException,
),
(
"""
@external
def bar():
x:uint256 = 1
for i in range(x,x+1,bound=2):
pass
""",
StructureException,
),
]


Expand Down
14 changes: 11 additions & 3 deletions vyper/semantics/analysis/local.py
Original file line number Diff line number Diff line change
Expand Up @@ -346,10 +346,11 @@ def visit_For(self, node):
raise IteratorException(
"Cannot iterate over the result of a function call", node.iter
)
validate_call_args(node.iter, (1, 2), kwargs=["bound"])
range_ = node.iter
validate_call_args(range_, (1, 2), kwargs=["bound"])

args = node.iter.args
kwargs = {s.arg: s.value for s in node.iter.keywords or []}
args = range_.args
kwargs = {s.arg: s.value for s in range_.keywords or []}
if len(args) == 1:
# range(CONSTANT)
n = args[0]
Expand All @@ -371,6 +372,13 @@ def visit_For(self, node):
type_list = get_common_types(n, bound)

else:
if range_.keywords:
raise StructureException(
"Keyword arguments are not supported for `range(N, M)` and"
"`range(x, x + N)` expressions",
range_.keywords[0],
)

validate_expected_type(args[0], IntegerT.any())
type_list = get_common_types(*args)
if not isinstance(args[0], vy_ast.Constant):
Expand Down

0 comments on commit 43c8d85

Please sign in to comment.