vyperlang / vyper

Pythonic Smart Contract Language for the EVM
https://vyperlang.org
Other
4.85k stars 791 forks source link

`Cannot modify loop variable` issue when trying to append something to an array during a loop. #4163

Closed rockorama closed 1 month ago

rockorama commented 3 months ago

Version Information

What's your issue about?

When iterating through an array of addresses, I have to append to another array the ones already processed. When trying to append to that array I get the compilation error Cannot modify loop variable

It works if I don't iterate through the addresses array, but use indices to do so instead.

@external
def foo():
    remainingToRepay: uint256 = 0
    alreadyDone: DynArray[address, 10] = []
    _assets: DynArray[address, 10] = []
    for a: address in _assets:
        if remainingToRepay == 0:
            break
        if not a in alreadyDone:
            continue
        alreadyDone.append(a)

works with:

  for i: uint256 in range(len(_asserts), bound=MAX_ASSETS):
        a: address = _assets[i]
        if remainingToRepay == 0:
            break
        if not a in alreadyDone:
            continue
        alreadyDone.append(a)