Skip to content

Commit

Permalink
flush keepalives on operator assignment statements (#13144) (#13151)
Browse files Browse the repository at this point in the history
This PR makes mypyc flush keepalives on operator assignment statements, to prevent accessing undefined variables in the generated C code.

Fixes mypyc/mypyc#941.

Co-authored-by: Zsolt Dollenstein <zsol.zsol@gmail.com>
  • Loading branch information
hauntsaninja and zsol authored Jul 16, 2022
1 parent 88c1b85 commit ee10d29
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 0 deletions.
1 change: 1 addition & 0 deletions mypyc/irbuild/statement.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ def transform_operator_assignment_stmt(builder: IRBuilder, stmt: OperatorAssignm
# usually operator assignments are done in-place
# but when target doesn't support that we need to manually assign
builder.assign(target, res, res.line)
builder.flush_keep_alives()


def transform_import(builder: IRBuilder, node: Import) -> None:
Expand Down
12 changes: 12 additions & 0 deletions mypyc/test-data/run-generators.test
Original file line number Diff line number Diff line change
Expand Up @@ -595,3 +595,15 @@ class C:
self.foo.flag = True
yield
self.foo.flag = False

[case testGeneratorEarlyReturnWithBorrows]
from typing import Iterator
class Bar:
bar = 0
class Foo:
bar = Bar()
def f(self) -> Iterator[int]:
if self:
self.bar.bar += 1
return
yield 0

0 comments on commit ee10d29

Please sign in to comment.