Skip to content

Commit

Permalink
py/emitnative: Initialise locals as Python object type for native code.
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
dpgeorge committed Dec 16, 2022
1 parent ed58d6e commit 910f579
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 1 deletion.
2 changes: 1 addition & 1 deletion py/emitnative.c
Expand Up @@ -401,7 +401,7 @@ STATIC void emit_native_start_pass(emit_t *emit, pass_kind_t pass, scope_t *scop

// local variables begin unbound, and have unknown type
for (mp_uint_t i = num_args; i < emit->local_vtype_alloc; i++) {
emit->local_vtype[i] = VTYPE_UNBOUND;
emit->local_vtype[i] = emit->do_viper_types ? VTYPE_UNBOUND : VTYPE_PYOBJ;
}

// values on stack begin unbound
Expand Down
11 changes: 11 additions & 0 deletions tests/basics/assign_expr.py
Expand Up @@ -12,6 +12,17 @@
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)))
Expand Down
2 changes: 2 additions & 0 deletions tests/basics/assign_expr.py.exp
Expand Up @@ -5,6 +5,8 @@ True
5
1 5 5
5
2 1
1 0
any True
8
123
Expand Down

0 comments on commit 910f579

Please sign in to comment.