Skip to content

Commit

Permalink
fix: prevent range over decimal
Browse files Browse the repository at this point in the history
  • Loading branch information
trocher committed Feb 21, 2024
1 parent 015cf81 commit eb96f7e
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 5 deletions.
27 changes: 26 additions & 1 deletion tests/functional/syntax/test_for_range.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ def foo():
pass
""",
StateAccessViolation,
"Bound must be a literal",
"Bound must be a literal integer",
None,
"x",
),
Expand Down Expand Up @@ -303,6 +303,31 @@ def foo():
"Did you mean 'uint96', or maybe 'uint8'?",
"uint9",
),
(
"""
@external
def foo():
for i:decimal in range(1.1, 2.2):
pass
""",
StateAccessViolation,
"Value must be a literal integer, unless a bound is specified",
None,
"1.1",
),
(
"""
@external
def foo():
x:decimal = 1.1
for i:decimal in range(x, x + 2.0, bound=10.1):
pass
""",
StateAccessViolation,
"Bound must be a literal integer",
None,
"10.1",
),
]

for_code_regex = re.compile(r"for .+ in (.*):", re.DOTALL)
Expand Down
8 changes: 4 additions & 4 deletions vyper/semantics/analysis/local.py
Original file line number Diff line number Diff line change
Expand Up @@ -870,16 +870,16 @@ def _validate_range_call(node: vy_ast.Call):
bound = kwargs["bound"]
if bound.has_folded_value:
bound = bound.get_folded_value()
if not isinstance(bound, vy_ast.Num):
raise StateAccessViolation("Bound must be a literal", bound)
if not isinstance(bound, vy_ast.Int):
raise StateAccessViolation("Bound must be a literal integer", bound)
if bound.value <= 0:
raise StructureException("Bound must be at least 1", bound)
if isinstance(start, vy_ast.Num) and isinstance(end, vy_ast.Num):
if isinstance(start, vy_ast.Int) and isinstance(end, vy_ast.Int):
error = "Please remove the `bound=` kwarg when using range with constants"
raise StructureException(error, bound)
else:
for arg in (start, end):
if not isinstance(arg, vy_ast.Num):
if not isinstance(arg, vy_ast.Int):
error = "Value must be a literal integer, unless a bound is specified"
raise StateAccessViolation(error, arg)
if end.value <= start.value:
Expand Down

0 comments on commit eb96f7e

Please sign in to comment.