0
0
mirror of https://github.com/vim/vim.git synced 2025-08-30 20:43:35 -04:00

patch 8.2.4542: Vim9: "break" inside try/catch not handled correctly

Problem:    Vim9: "break" inside try/catch not handled correctly.
Solution:   First jump to :endtry. (closes #9927)
This commit is contained in:
Bram Moolenaar 2022-03-10 21:53:44 +00:00
parent e406ff87c8
commit 873f8243f6
4 changed files with 45 additions and 7 deletions

View File

@ -907,6 +907,28 @@ def Test_continue_in_try_in_while()
unlet g:sequence unlet g:sequence
enddef enddef
def Test_break_in_try_in_for()
var lines =<< trim END
vim9script
def Ls(): list<string>
var ls: list<string>
for s in ['abc', 'def']
for _ in [123, 456]
try
eval [][0]
catch
break
endtry
endfor
ls += [s]
endfor
return ls
enddef
assert_equal(['abc', 'def'], Ls())
END
v9.CheckScriptSuccess(lines)
enddef
def Test_nocatch_return_in_try() def Test_nocatch_return_in_try()
# return in try block returns normally # return in try block returns normally
def ReturnInTry(): string def ReturnInTry(): string

View File

@ -750,6 +750,8 @@ static char *(features[]) =
static int included_patches[] = static int included_patches[] =
{ /* Add new patch number below this line */ { /* Add new patch number below this line */
/**/
4542,
/**/ /**/
4541, 4541,
/**/ /**/

View File

@ -121,7 +121,7 @@ typedef enum {
ISN_CATCH, // drop v:exception ISN_CATCH, // drop v:exception
ISN_FINALLY, // start of :finally block ISN_FINALLY, // start of :finally block
ISN_ENDTRY, // take entry off from ec_trystack ISN_ENDTRY, // take entry off from ec_trystack
ISN_TRYCONT, // handle :continue inside a :try statement ISN_TRYCONT, // handle :continue or :break inside a :try statement
// more expression operations // more expression operations
ISN_ADDLIST, // add two lists ISN_ADDLIST, // add two lists

View File

@ -1206,6 +1206,7 @@ compile_continue(char_u *arg, cctx_T *cctx)
compile_break(char_u *arg, cctx_T *cctx) compile_break(char_u *arg, cctx_T *cctx)
{ {
scope_T *scope = cctx->ctx_scope; scope_T *scope = cctx->ctx_scope;
int try_scopes = 0;
endlabel_T **el; endlabel_T **el;
for (;;) for (;;)
@ -1215,16 +1216,29 @@ compile_break(char_u *arg, cctx_T *cctx)
emsg(_(e_break_without_while_or_for)); emsg(_(e_break_without_while_or_for));
return NULL; return NULL;
} }
if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE) if (scope->se_type == FOR_SCOPE)
{
el = &scope->se_u.se_for.fs_end_label;
break; break;
}
if (scope->se_type == WHILE_SCOPE)
{
el = &scope->se_u.se_while.ws_end_label;
break;
}
if (scope->se_type == TRY_SCOPE)
++try_scopes;
scope = scope->se_outer; scope = scope->se_outer;
} }
// Jump to the end of the FOR or WHILE loop. if (try_scopes > 0)
if (scope->se_type == FOR_SCOPE) // Inside one or more try/catch blocks we first need to jump to the
el = &scope->se_u.se_for.fs_end_label; // "finally" or "endtry" to cleanup. Then come to the next JUMP
else // intruction, which we don't know the index of yet.
el = &scope->se_u.se_while.ws_end_label; generate_TRYCONT(cctx, try_scopes, cctx->ctx_instr.ga_len + 1);
// Jump to the end of the FOR or WHILE loop. The instruction index will be
// filled in later.
if (compile_jump_to_end(el, JUMP_ALWAYS, cctx) == FAIL) if (compile_jump_to_end(el, JUMP_ALWAYS, cctx) == FAIL)
return FAIL; return FAIL;