py/emitnative: Initialise locals as Python object type for native code.
In @micropython.native code the types of variables and expressions are always Python objects, so they can be initialised as such. This prevents problems with compiling optimised code like while-loops where a local may be referenced before it is assigned to. Signed-off-by: Damien George <damien@micropython.org>
This commit is contained in:
@@ -12,6 +12,17 @@ x = 1
|
||||
print(x, x := 5, x)
|
||||
print(x)
|
||||
|
||||
# Test "while" with assignment expression as conditional, assigning to a new local.
|
||||
# The while conditional is compiled after the while body, so this tests how the
|
||||
# compiler handles the case of an unbound local being compiled before it is assigned.
|
||||
def f():
|
||||
l = [0, 1]
|
||||
while local := len(l):
|
||||
print(local, l.pop())
|
||||
|
||||
|
||||
f()
|
||||
|
||||
|
||||
def foo():
|
||||
print("any", any((hit := i) % 5 == 3 and (hit % 2) == 0 for i in range(10)))
|
||||
|
||||
Reference in New Issue
Block a user