Skip to content

Commit

Permalink
fix[lang]: fix varinfo comparison
Browse files Browse the repository at this point in the history
for `VarInfo`s which are declared in memory, the `VarInfo`
initialization is missing `decl_node` and therefore different variables
with the same type get detected as overlapping in loop iterator
modification detection. this commit properly initializes the `VarInfo`s
in memory.
  • Loading branch information
charles-cooper committed Jun 22, 2024
1 parent e9db8d9 commit 45d2c48
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
17 changes: 17 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 @@ -897,3 +897,20 @@ def foo():
compile_code(main, input_bundle=input_bundle)

assert e.value._message == "Cannot modify loop variable `queue`"


def test_iterator_modification_memory(get_contract):
code = """
@external
def foo() -> DynArray[uint256, 10]:
# check VarInfos are distinguished by decl_node when they have same type
alreadyDone: DynArray[uint256, 10] = []
_assets: DynArray[uint256, 10] = [1, 2, 3, 4, 3, 2, 1]
for a: uint256 in _assets:
if a in alreadyDone:
continue
alreadyDone.append(a)
return alreadyDone
"""
c = get_contract(code)
assert c.foo() == [1, 2, 3, 4]
4 changes: 2 additions & 2 deletions vyper/semantics/analysis/local.py
Original file line number Diff line number Diff line change
Expand Up @@ -363,7 +363,7 @@ def visit_AnnAssign(self, node):
# validate the value before adding it to the namespace
self.expr_visitor.visit(node.value, typ)

self.namespace[name] = VarInfo(typ, location=DataLocation.MEMORY)
self.namespace[name] = VarInfo(typ, location=DataLocation.MEMORY, decl_node=node)

self.expr_visitor.visit(node.target, typ)

Expand Down Expand Up @@ -575,7 +575,7 @@ def visit_For(self, node):
target_name = node.target.target.id
# maybe we should introduce a new Modifiability: LOOP_VARIABLE
self.namespace[target_name] = VarInfo(
target_type, modifiability=Modifiability.RUNTIME_CONSTANT
target_type, modifiability=Modifiability.RUNTIME_CONSTANT, decl_node=node.target
)

self.expr_visitor.visit(node.target.target, target_type)
Expand Down

0 comments on commit 45d2c48

Please sign in to comment.