summaryrefslogtreecommitdiffstatshomepage
diff options
context:
space:
mode:
-rw-r--r--py/emitnative.c2
-rw-r--r--tests/basics/assign_expr.py11
-rw-r--r--tests/basics/assign_expr.py.exp2
3 files changed, 14 insertions, 1 deletions
diff --git a/py/emitnative.c b/py/emitnative.c
index 2d694983bd..b02559ad7d 100644
--- a/py/emitnative.c
+++ b/py/emitnative.c
@@ -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
diff --git a/tests/basics/assign_expr.py b/tests/basics/assign_expr.py
index f243905dc2..2cf604193b 100644
--- a/tests/basics/assign_expr.py
+++ b/tests/basics/assign_expr.py
@@ -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)))
diff --git a/tests/basics/assign_expr.py.exp b/tests/basics/assign_expr.py.exp
index e38e1ae7a6..47da56b80d 100644
--- a/tests/basics/assign_expr.py.exp
+++ b/tests/basics/assign_expr.py.exp
@@ -5,6 +5,8 @@ True
5
1 5 5
5
+2 1
+1 0
any True
8
123