mypyc icon indicating copy to clipboard operation
mypyc copied to clipboard

Support yield and await nested in expressions

Open msullivan opened this issue 6 years ago • 3 comments

We currently don't support code like x = foo() + await bar(), since the result of calling foo() will be stored in a temporary that is not spilled to the environment.

I think that the best approach to solve this is to add another compiler pass that detects values that live across yields (which may just be the values that are live on function entry?) and spills them to the environment.

I think that scheme would also let us get rid of the manual spilling we do of compiler-induced temporaries in a bunch of situations (exception handling, with, etc). With some care it could maybe also subsume a bunch of the spill handling for nested functions too?

msullivan avatar Aug 28 '19 01:08 msullivan

Would it be possible to confirm if the below is this same issue? It seems very similar, but it results in a runtime error as opposed to a compile-time -Werror=maybe-uninitialized error as in your example.

def gen():
    a, b = 1, (yield 0)
    print(a)
    print(b)
    yield 3

it = gen()
print(next(it))
print(it.send(2))
Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "tupassign.py", line 9, in <module>
    print(it.send(2))
  File "tupassign.py", line -1, in gen
UnboundLocalError: local variable '' referenced before assignment

No rush or anything. Thanks!

gurnec avatar May 11 '21 23:05 gurnec

Yes, I believe so

msullivan avatar May 11 '21 23:05 msullivan

OK, thank you for taking a look.

gurnec avatar May 11 '21 23:05 gurnec