py: Fix stack underflow with optimised for loop.
This commit is contained in:
parent
049a01d148
commit
5b5562c1d1
@ -1745,7 +1745,7 @@ void compile_while_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
|
||||
// And, if the loop never runs, the loop variable should never be assigned
|
||||
void compile_for_stmt_optimised_range(compiler_t *comp, mp_parse_node_t pn_var, mp_parse_node_t pn_start, mp_parse_node_t pn_end, mp_parse_node_t pn_step, mp_parse_node_t pn_body, mp_parse_node_t pn_else) {
|
||||
START_BREAK_CONTINUE_BLOCK
|
||||
comp->break_label |= MP_EMIT_BREAK_FROM_FOR;
|
||||
// note that we don't need to pop anything when breaking from an optimise for loop
|
||||
|
||||
uint top_label = comp_next_label(comp);
|
||||
uint entry_label = comp_next_label(comp);
|
||||
|
6
py/vm.c
6
py/vm.c
@ -159,8 +159,8 @@ mp_vm_return_kind_t mp_execute_bytecode(const byte *code, const mp_obj_t *args,
|
||||
|
||||
#if DETECT_VM_STACK_OVERFLOW
|
||||
if (vm_return_kind == MP_VM_RETURN_NORMAL) {
|
||||
if (sp != state) {
|
||||
printf("Stack misalign: %d\n", sp - state);
|
||||
if (sp < state) {
|
||||
printf("VM stack underflow: " INT_FMT "\n", sp - state);
|
||||
assert(0);
|
||||
}
|
||||
}
|
||||
@ -178,7 +178,7 @@ mp_vm_return_kind_t mp_execute_bytecode(const byte *code, const mp_obj_t *args,
|
||||
}
|
||||
}
|
||||
if (overflow) {
|
||||
printf("VM stack overflow state=%p n_state+1=%u\n", state, n_state);
|
||||
printf("VM stack overflow state=%p n_state+1=" UINT_FMT "\n", state, n_state);
|
||||
assert(0);
|
||||
}
|
||||
}
|
||||
|
7
tests/basics/for_return.py
Normal file
7
tests/basics/for_return.py
Normal file
@ -0,0 +1,7 @@
|
||||
# test returning from within a for loop
|
||||
|
||||
def f():
|
||||
for i in [1, 2, 3]:
|
||||
return i
|
||||
|
||||
print(f())
|
Loading…
Reference in New Issue
Block a user