Skip to content

Commit 162c74d

Browse files
[mypyc] Remangle redefined names produced by async with (#16408)
Fixes mypyc/mypyc#1001. --------- Co-authored-by: Jelle Zijlstra <[email protected]>
1 parent 5a8cd80 commit 162c74d

File tree

2 files changed

+32
-3
lines changed

2 files changed

+32
-3
lines changed

mypyc/irbuild/builder.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1246,14 +1246,15 @@ def add_var_to_env_class(
12461246
) -> AssignmentTarget:
12471247
# First, define the variable name as an attribute of the environment class, and then
12481248
# construct a target for that attribute.
1249-
self.fn_info.env_class.attributes[var.name] = rtype
1250-
attr_target = AssignmentTargetAttr(base.curr_env_reg, var.name)
1249+
name = remangle_redefinition_name(var.name)
1250+
self.fn_info.env_class.attributes[name] = rtype
1251+
attr_target = AssignmentTargetAttr(base.curr_env_reg, name)
12511252

12521253
if reassign:
12531254
# Read the local definition of the variable, and set the corresponding attribute of
12541255
# the environment class' variable to be that value.
12551256
reg = self.read(self.lookup(var), self.fn_info.fitem.line)
1256-
self.add(SetAttr(base.curr_env_reg, var.name, reg, self.fn_info.fitem.line))
1257+
self.add(SetAttr(base.curr_env_reg, name, reg, self.fn_info.fitem.line))
12571258

12581259
# Override the local definition of the variable to instead point at the variable in
12591260
# the environment class.

mypyc/test-data/run-async.test

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,3 +143,31 @@ async def foo() -> AsyncIterable[int]:
143143
yields, val = run_generator(async_iter(foo()))
144144
assert yields == (0,1,2), yields
145145
assert val == 'lol no', val
146+
147+
[case testAsyncWithVarReuse]
148+
class ConMan:
149+
async def __aenter__(self) -> int:
150+
return 1
151+
async def __aexit__(self, *exc: object):
152+
pass
153+
154+
class ConManB:
155+
async def __aenter__(self) -> int:
156+
return 2
157+
async def __aexit__(self, *exc: object):
158+
pass
159+
160+
async def x() -> None:
161+
value = 2
162+
async with ConMan() as f:
163+
value += f
164+
assert value == 3, value
165+
async with ConManB() as f:
166+
value += f
167+
assert value == 5, value
168+
169+
[typing fixtures/typing-full.pyi]
170+
[file driver.py]
171+
import asyncio
172+
import native
173+
asyncio.run(native.x())

0 commit comments

Comments
 (0)