Skip to content

Commit b13a450

Browse files
authored
[mypyc] Fix bad C generated for multiple assignment (#13147)
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.
1 parent d3e0db7 commit b13a450

File tree

2 files changed

+26
-1
lines changed

2 files changed

+26
-1
lines changed

mypyc/irbuild/statement.py

+1
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ def transform_assignment_stmt(builder: IRBuilder, stmt: AssignmentStmt) -> None:
101101
for (left, temp) in zip(first_lvalue.items, temps):
102102
assignment_target = builder.get_assignment_target(left)
103103
builder.assign(assignment_target, temp, stmt.line)
104+
builder.flush_keep_alives()
104105
return
105106

106107
line = stmt.rvalue.line

mypyc/test-data/run-generators.test

+25-1
Original file line numberDiff line numberDiff line change
@@ -606,4 +606,28 @@ class Foo:
606606
if self:
607607
self.bar.bar += 1
608608
return
609-
yield 0
609+
yield 0
610+
611+
[case testBorrowingInGeneratorInTupleAssignment]
612+
from typing import Iterator
613+
614+
class Foo:
615+
flag1: bool
616+
flag2: bool
617+
618+
class C:
619+
foo: Foo
620+
621+
def genf(self) -> Iterator[None]:
622+
self.foo.flag1, self.foo.flag2 = True, True
623+
yield
624+
self.foo.flag1, self.foo.flag2 = False, False
625+
626+
def test_generator() -> None:
627+
c = C()
628+
c.foo = Foo()
629+
gen = c.genf()
630+
next(gen)
631+
assert c.foo.flag1 == c.foo.flag2 == True
632+
assert list(gen) == []
633+
assert c.foo.flag1 == c.foo.flag2 == False

0 commit comments

Comments
 (0)