Skip to content

Commit cd31867

Browse files
fix[codegen]: fix iteration over constant literals (vyperlang#4462)
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.
1 parent 9e396fb commit cd31867

File tree

3 files changed

+17
-2
lines changed

3 files changed

+17
-2
lines changed

tests/functional/codegen/features/iteration/test_for_in_list.py

+15
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,21 @@ def data() -> int128:
214214
assert c.data() == sum(xs)
215215

216216

217+
def test_constant_list_iter(get_contract):
218+
code = """
219+
MY_LIST: constant(uint24[4]) = [1, 2, 3, 4]
220+
221+
@external
222+
def foo() -> uint24:
223+
x: uint24 = 0
224+
for s: uint24 in MY_LIST:
225+
x += s
226+
return x
227+
"""
228+
c = get_contract(code)
229+
assert c.foo() == sum([1, 2, 3, 4])
230+
231+
217232
def test_basic_for_list_storage_address(get_contract):
218233
code = """
219234
addresses: address[3]

vyper/codegen/core.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -560,7 +560,7 @@ def _get_element_ptr_array(parent, key, array_bounds_check):
560560
return IRnode.from_list("~empty", subtype)
561561

562562
if parent.value == "multi":
563-
assert isinstance(key.value, int)
563+
assert isinstance(key.value, int), key
564564
return parent.args[key.value]
565565

566566
ix = unwrap_location(key)

vyper/codegen/stmt.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -258,7 +258,7 @@ def _parse_For_list(self):
258258
ret = ["seq"]
259259

260260
# list literal, force it to memory first
261-
if isinstance(self.stmt.iter, vy_ast.List):
261+
if iter_list.is_literal:
262262
tmp_list = self.context.new_internal_variable(iter_list.typ)
263263
ret.append(make_setter(tmp_list, iter_list))
264264
iter_list = tmp_list

0 commit comments

Comments
 (0)