py/compile2: Fix bug with break/continue in else of optimised for-range.

A port of 4c5f108321.
This commit is contained in:
Damien George 2017-08-30 11:24:29 +10:00
parent d88eab7445
commit d7576c4ba7
1 changed files with 15 additions and 0 deletions

View File

@ -1455,8 +1455,19 @@ STATIC void compile_for_stmt_optimised_range(compiler_t *comp, const byte *pn_va
// break/continue apply to outer loop (if any) in the else block // break/continue apply to outer loop (if any) in the else block
END_BREAK_CONTINUE_BLOCK END_BREAK_CONTINUE_BLOCK
// Compile the else block. We must pop the iterator variables before
// executing the else code because it may contain break/continue statements.
uint end_label = 0;
if (pn_else != NULL) { if (pn_else != NULL) {
// discard final value of "var", and possible "end" value
EMIT(pop_top);
if (end_on_stack) {
EMIT(pop_top);
}
compile_node(comp, pn_else); compile_node(comp, pn_else);
end_label = comp_next_label(comp);
EMIT_ARG(jump, end_label);
EMIT_ARG(adjust_stack_size, 1 + end_on_stack);
} }
EMIT_ARG(label_assign, break_label); EMIT_ARG(label_assign, break_label);
@ -1468,6 +1479,10 @@ STATIC void compile_for_stmt_optimised_range(compiler_t *comp, const byte *pn_va
if (end_on_stack) { if (end_on_stack) {
EMIT(pop_top); EMIT(pop_top);
} }
if (pn_else != NULL) {
EMIT_ARG(label_assign, end_label);
}
} }
STATIC void compile_for_stmt(compiler_t *comp, const byte *p, const byte *ptop) { STATIC void compile_for_stmt(compiler_t *comp, const byte *p, const byte *ptop) {