Skip to content

Commit

Permalink
fix[codegen]: fix iteration over constant literals (#4462)
Browse files Browse the repository at this point in the history
the check in `_parse_For_list` is wrong when the iter is a constant
(since the check is syntactic). the fix is to check the `is_literal`
property on the generated IR.
  • Loading branch information
charles-cooper authored Feb 19, 2025
1 parent 9e396fb commit cd31867
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 2 deletions.
15 changes: 15 additions & 0 deletions tests/functional/codegen/features/iteration/test_for_in_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,21 @@ def data() -> int128:
assert c.data() == sum(xs)


def test_constant_list_iter(get_contract):
code = """
MY_LIST: constant(uint24[4]) = [1, 2, 3, 4]
@external
def foo() -> uint24:
x: uint24 = 0
for s: uint24 in MY_LIST:
x += s
return x
"""
c = get_contract(code)
assert c.foo() == sum([1, 2, 3, 4])


def test_basic_for_list_storage_address(get_contract):
code = """
addresses: address[3]
Expand Down
2 changes: 1 addition & 1 deletion vyper/codegen/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -560,7 +560,7 @@ def _get_element_ptr_array(parent, key, array_bounds_check):
return IRnode.from_list("~empty", subtype)

if parent.value == "multi":
assert isinstance(key.value, int)
assert isinstance(key.value, int), key
return parent.args[key.value]

ix = unwrap_location(key)
Expand Down
2 changes: 1 addition & 1 deletion vyper/codegen/stmt.py
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ def _parse_For_list(self):
ret = ["seq"]

# list literal, force it to memory first
if isinstance(self.stmt.iter, vy_ast.List):
if iter_list.is_literal:
tmp_list = self.context.new_internal_variable(iter_list.typ)
ret.append(make_setter(tmp_list, iter_list))
iter_list = tmp_list
Expand Down

0 comments on commit cd31867

Please sign in to comment.