Skip to content

Commit

Permalink
analysis: fix the order of lookup
Browse files Browse the repository at this point in the history
We need to search the scopes in reverse order. It exposed a second
bug. Which is that a variable defined in the outer scope is redefined
in the current scope according to the current logic in visit_Assign_one.

Fix the logic to define it only if it hasn't been defined before.

Fixes: py2many#417
  • Loading branch information
adsharma committed Jul 12, 2021
1 parent e350e70 commit c7419cf
Show file tree
Hide file tree
Showing 10 changed files with 30 additions and 11 deletions.
4 changes: 3 additions & 1 deletion py2many/rewriters.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@ def visit_Assign(self, node):

assigns = []
for assign_target in node.targets:
definition = node.scopes.find(get_id(assign_target))
definition = node.scopes.parent_scopes.find(get_id(assign_target))
if definition is None:
definition = node.scopes.find(get_id(assign_target))
if definition is not assign_target:
previous_type = get_inferred_type(definition)
if get_id(previous_type) == get_id(annotation):
Expand Down
6 changes: 5 additions & 1 deletion py2many/scope.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ def find_definition(scope, var_attr="vars"):
if get_id(var) == lookup:
return var

for scope in self:
for scope in reversed(self):
defn = None
if not defn and hasattr(scope, "vars"):
defn = find_definition(scope, "vars")
Expand All @@ -72,6 +72,10 @@ def find_import(self, lookup):
if imp.name == lookup:
return imp

@property
def parent_scopes(self):
return ScopeList(self[:-1])


class ScopeTransformer(ast.NodeTransformer, ScopeMixin):
"""
Expand Down
5 changes: 3 additions & 2 deletions pycpp/transpiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -608,8 +608,9 @@ def _visit_AssignOne(self, node, target):
value = self.visit(node.value)
return "{0} = {1};".format(target, value)

target_id = get_id(target)
definition = node.scopes.find(target_id)
definition = node.scopes.parent_scopes.find(get_id(target))
if definition is None:
definition = node.scopes.find(get_id(target))
if isinstance(target, ast.Name) and defined_before(definition, node):
target = self.visit(target)
value = self.visit(node.value)
Expand Down
4 changes: 3 additions & 1 deletion pydart/transpiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -513,7 +513,9 @@ def _visit_AssignOne(self, node, target):
value = self.visit(node.value)
return f"{target} = {value};"

definition = node.scopes.find(get_id(target))
definition = node.scopes.parent_scopes.find(get_id(target))
if definition is None:
definition = node.scopes.find(get_id(target))
if isinstance(target, ast.Name) and defined_before(definition, node):
target = self.visit(target)
value = self.visit(node.value)
Expand Down
4 changes: 3 additions & 1 deletion pygo/transpiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -644,7 +644,9 @@ def _visit_AssignOne(self, node, target):
value, typename, left_annotation, right_annotation
)

definition = node.scopes.find(get_id(target))
definition = node.scopes.parent_scopes.find(get_id(target))
if definition is None:
definition = node.scopes.find(get_id(target))
if isinstance(target, ast.Name) and defined_before(definition, node):
return f"{target_str} = {value}"
else:
Expand Down
4 changes: 3 additions & 1 deletion pyjl/transpiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -519,7 +519,9 @@ def _visit_AssignOne(self, node, target):
value = "None"
return "{0} = {1}".format(target, value)

definition = node.scopes.find(get_id(target))
definition = node.scopes.parent_scopes.find(get_id(target))
if definition is None:
definition = node.scopes.find(get_id(target))
if isinstance(target, ast.Name) and defined_before(definition, node):
target_str = self.visit(target)
value = self.visit(node.value)
Expand Down
4 changes: 3 additions & 1 deletion pykt/transpiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -497,7 +497,9 @@ def _visit_AssignOne(self, node, target):
value = self.visit(node.value)
return f"{target} = {value}"

definition = node.scopes.find(get_id(target))
definition = node.scopes.parent_scopes.find(get_id(target))
if definition is None:
definition = node.scopes.find(get_id(target))
if isinstance(target, ast.Name) and defined_before(definition, node):
target = self.visit(target)
value = self.visit(node.value)
Expand Down
4 changes: 3 additions & 1 deletion pynim/transpiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -494,7 +494,9 @@ def _visit_AssignOne(self, node, target):
value = self.visit(node.value)
return f"{target} = {value}"

definition = node.scopes.find(get_id(target))
definition = node.scopes.parent_scopes.find(get_id(target))
if definition is None:
definition = node.scopes.find(get_id(target))
if isinstance(target, ast.Name) and defined_before(definition, node):
target = self.visit(target)
value = self.visit(node.value)
Expand Down
4 changes: 3 additions & 1 deletion pyrs/transpiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -814,7 +814,9 @@ def _visit_AssignOne(self, node, target):
value = "None"
return "{0} = {1};".format(target, value)

definition = node.scopes.find(get_id(target))
definition = node.scopes.parent_scopes.find(get_id(target))
if definition is None:
definition = node.scopes.find(get_id(target))
if isinstance(target, ast.Name) and defined_before(definition, node):
needs_cast = self._needs_cast(target, node.value)
target_str = self.visit(target)
Expand Down
2 changes: 1 addition & 1 deletion tests/expected/comb_sort.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ def comb_sort(seq: List[int]) -> List[int]:
for i in range(len(seq) - gap):
if seq[i] > seq[i + gap]:
if True:
(__tmp1, __tmp2) = (seq[i + gap], seq[i])
__tmp1, __tmp2 = seq[i + gap], seq[i]
seq[i] = __tmp1
seq[i + gap] = __tmp2
swap = True
Expand Down

0 comments on commit c7419cf

Please sign in to comment.