Skip to content

Commit

Permalink
patch 9.0.2172: Vim9: compiling :defer may fail
Browse files Browse the repository at this point in the history
Problem:  Vim9: compiling :defer may fail
Solution: compile defer, when ctx_skip is not SKIP_YES

compiling defer fails in an if statement with false condition,
so check the ctx_skip value when compiling :defer

fixes:  #13698
closes: #13702

Signed-off-by: Yegappan Lakshmanan <yegappan@yahoo.com>
Signed-off-by: Christian Brabandt <cb@256bit.org>
  • Loading branch information
yegappan authored and chrisbra committed Dec 16, 2023
1 parent 535e8f5 commit a185a31
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 19 deletions.
13 changes: 13 additions & 0 deletions src/testdir/test_vim9_script.vim
Original file line number Diff line number Diff line change
Expand Up @@ -4783,6 +4783,19 @@ def Test_multidefer_with_exception()
v9.CheckSourceSuccess(lines)
enddef

" Test for using ":defer" inside an if statement with a false condition
def Test_defer_skipped()
var lines =<< trim END
def Foo()
if false
defer execute('echow "hello"', "")
endif
enddef
defcompile
END
v9.CheckSourceSuccess(lines)
enddef

" Keep this last, it messes up highlighting.
def Test_substitute_cmd()
new
Expand Down
2 changes: 1 addition & 1 deletion src/version.c
Original file line number Diff line number Diff line change
Expand Up @@ -705,7 +705,7 @@ static char *(features[]) =
static int included_patches[] =
{ /* Add new patch number below this line */
/**/
2171,
2172,
/**/
2170,
/**/
Expand Down
42 changes: 24 additions & 18 deletions src/vim9cmds.c
Original file line number Diff line number Diff line change
Expand Up @@ -2020,36 +2020,42 @@ compile_defer(char_u *arg_start, cctx_T *cctx)
*paren = '(';

// check for function type
type = get_type_on_stack(cctx, 0);
if (type->tt_type != VAR_FUNC)
if (cctx->ctx_skip != SKIP_YES)
{
emsg(_(e_function_name_required));
return NULL;
type = get_type_on_stack(cctx, 0);
if (type->tt_type != VAR_FUNC)
{
emsg(_(e_function_name_required));
return NULL;
}
}

// compile the arguments
arg = skipwhite(paren + 1);
if (compile_arguments(&arg, cctx, &argcount, CA_NOT_SPECIAL) == FAIL)
return NULL;

if (func_idx >= 0)
if (cctx->ctx_skip != SKIP_YES)
{
type2_T *argtypes = NULL;
type2_T shuffled_argtypes[MAX_FUNC_ARGS];
if (func_idx >= 0)
{
type2_T *argtypes = NULL;
type2_T shuffled_argtypes[MAX_FUNC_ARGS];

if (check_internal_func_args(cctx, func_idx, argcount, FALSE,
&argtypes, shuffled_argtypes) == FAIL)
if (check_internal_func_args(cctx, func_idx, argcount, FALSE,
&argtypes, shuffled_argtypes) == FAIL)
return NULL;
}
else if (check_func_args_from_type(cctx, type, argcount, TRUE,
arg_start) == FAIL)
return NULL;
}
else if (check_func_args_from_type(cctx, type, argcount, TRUE,
arg_start) == FAIL)
return NULL;

defer_var_idx = get_defer_var_idx(cctx);
if (defer_var_idx == 0)
return NULL;
if (generate_DEFER(cctx, defer_var_idx - 1, argcount) == FAIL)
return NULL;
defer_var_idx = get_defer_var_idx(cctx);
if (defer_var_idx == 0)
return NULL;
if (generate_DEFER(cctx, defer_var_idx - 1, argcount) == FAIL)
return NULL;
}

return skipwhite(arg);
}
Expand Down

0 comments on commit a185a31

Please sign in to comment.