summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorBram Moolenaar <Bram@vim.org>2021-07-03 18:56:53 +0200
committerBram Moolenaar <Bram@vim.org>2021-07-03 18:56:53 +0200
commite28d9b3bd4de2c7288add83ec35dc001ba280617 (patch)
tree9703589a42e63637f98b4799da3f5d5157607d4c
parent00aaa512d5e775e0db21e251f43d7afb8fca910f (diff)
patch 8.2.3091: Vim9: default argument expr. cannot use previous argumentv8.2.3091
Problem: Vim9: default argument expression cannot use previous argument Solution: Correct argument index. (closes #8496)
-rw-r--r--src/structs.h2
-rw-r--r--src/testdir/test_vim9_func.vim6
-rw-r--r--src/version.c2
-rw-r--r--src/vim9compile.c7
4 files changed, 13 insertions, 4 deletions
diff --git a/src/structs.h b/src/structs.h
index d6f4988e72..be4082b935 100644
--- a/src/structs.h
+++ b/src/structs.h
@@ -1610,6 +1610,8 @@ typedef struct
int uf_dfunc_idx; // only valid if uf_def_status is UF_COMPILED
garray_T uf_args; // arguments, including optional arguments
garray_T uf_def_args; // default argument expressions
+ int uf_args_visible; // normally uf_args.ga_len, less when
+ // compiling default argument expression.
// for :def (for :function uf_ret_type is NULL)
type_T **uf_arg_types; // argument types (count == uf_args.ga_len)
diff --git a/src/testdir/test_vim9_func.vim b/src/testdir/test_vim9_func.vim
index 4816cb6674..f4142ee851 100644
--- a/src/testdir/test_vim9_func.vim
+++ b/src/testdir/test_vim9_func.vim
@@ -452,6 +452,12 @@ def Test_call_default_args()
MyDefaultThird('->', 'xx', v:none)->assert_equal('->xxbb')
MyDefaultThird('->', v:none, 'yy')->assert_equal('->aayy')
MyDefaultThird('->', 'xx', 'yy')->assert_equal('->xxyy')
+
+ def DefArg(mandatory: any, optional = mandatory): string
+ return mandatory .. optional
+ enddef
+ DefArg(1234)->assert_equal('12341234')
+ DefArg("ok")->assert_equal('okok')
END
CheckDefAndScriptSuccess(lines)
diff --git a/src/version.c b/src/version.c
index 0cefeba32b..5f07483581 100644
--- a/src/version.c
+++ b/src/version.c
@@ -756,6 +756,8 @@ static char *(features[]) =
static int included_patches[] =
{ /* Add new patch number below this line */
/**/
+ 3091,
+/**/
3090,
/**/
3089,
diff --git a/src/vim9compile.c b/src/vim9compile.c
index d074215713..88086cefd6 100644
--- a/src/vim9compile.c
+++ b/src/vim9compile.c
@@ -274,7 +274,7 @@ arg_exists(
if (len == 0)
return FAIL;
- for (idx = 0; idx < cctx->ctx_ufunc->uf_args.ga_len; ++idx)
+ for (idx = 0; idx < cctx->ctx_ufunc->uf_args_visible; ++idx)
{
char_u *arg = FUNCARG(cctx->ctx_ufunc, idx);
@@ -9172,7 +9172,6 @@ compile_def_function(
{
int count = ufunc->uf_def_args.ga_len;
int first_def_arg = ufunc->uf_args.ga_len - count;
- int uf_args_len = ufunc->uf_args.ga_len;
int i;
char_u *arg;
int off = STACK_FRAME_SIZE + (ufunc->uf_va_name != NULL ? 1 : 0);
@@ -9195,12 +9194,11 @@ compile_def_function(
goto erret;
// Make sure later arguments are not found.
- ufunc->uf_args.ga_len = i;
+ ufunc->uf_args_visible = arg_idx;
arg = ((char_u **)(ufunc->uf_def_args.ga_data))[i];
r = compile_expr0(&arg, &cctx);
- ufunc->uf_args.ga_len = uf_args_len;
if (r == FAIL)
goto erret;
@@ -9230,6 +9228,7 @@ compile_def_function(
if (did_set_arg_type)
set_function_type(ufunc);
}
+ ufunc->uf_args_visible = ufunc->uf_args.ga_len;
/*
* Loop over all the lines of the function and generate instructions.