Skip to content

Commit

Permalink
[mypyc] Fix bad C generated for multiple assignment (#13147)
Browse files Browse the repository at this point in the history
We need to flush keep alives also after a multiple assignment, as otherwise
the generated IR will be badly formed and confuse the reference count transform.

Fixes mypyc/mypyc#942.
  • Loading branch information
JukkaL committed Jul 16, 2022
1 parent d3e0db7 commit b13a450
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
1 change: 1 addition & 0 deletions mypyc/irbuild/statement.py
Expand Up @@ -101,6 +101,7 @@ def transform_assignment_stmt(builder: IRBuilder, stmt: AssignmentStmt) -> None:
for (left, temp) in zip(first_lvalue.items, temps):
assignment_target = builder.get_assignment_target(left)
builder.assign(assignment_target, temp, stmt.line)
builder.flush_keep_alives()
return

line = stmt.rvalue.line
Expand Down
26 changes: 25 additions & 1 deletion mypyc/test-data/run-generators.test
Expand Up @@ -606,4 +606,28 @@ class Foo:
if self:
self.bar.bar += 1
return
yield 0
yield 0

[case testBorrowingInGeneratorInTupleAssignment]
from typing import Iterator

class Foo:
flag1: bool
flag2: bool

class C:
foo: Foo

def genf(self) -> Iterator[None]:
self.foo.flag1, self.foo.flag2 = True, True
yield
self.foo.flag1, self.foo.flag2 = False, False

def test_generator() -> None:
c = C()
c.foo = Foo()
gen = c.genf()
next(gen)
assert c.foo.flag1 == c.foo.flag2 == True
assert list(gen) == []
assert c.foo.flag1 == c.foo.flag2 == False

0 comments on commit b13a450

Please sign in to comment.