patch 8.2.0206: calling Vim9 function using default argument fails
Problem: Calling Vim9 function using default argument fails.
Solution: Give an appropriate error. (closes #5572)
diff --git a/src/testdir/test_vim9_script.vim b/src/testdir/test_vim9_script.vim
index 2eeea54..fee0708 100644
--- a/src/testdir/test_vim9_script.vim
+++ b/src/testdir/test_vim9_script.vim
@@ -131,6 +131,34 @@
assert_equal('one,two,three', MyVarargs('one', 'two', 'three'))
enddef
+"def Test_call_func_defined_later()
+" call assert_equal('one', DefineLater('one'))
+" call assert_fails('call NotDefined("one")', 'E99:')
+"enddef
+
+func DefineLater(arg)
+ return a:arg
+endfunc
+
+def MyDefaultArgs(name = 'string'): string
+ return name
+enddef
+
+func Test_call_default_args_from_func()
+ " TODO: implement using default value for optional argument
+ "call assert_equal('string', MyDefaultArgs())
+ call assert_fails('call MyDefaultArgs()', 'optional arguments not implemented yet')
+ call assert_equal('one', MyDefaultArgs('one'))
+ call assert_fails('call MyDefaultArgs("one", "two")', 'E118:')
+endfunc
+
+def Test_call_default_args()
+ " TODO: implement using default value for optional argument
+ "assert_equal('string', MyDefaultArgs())
+ assert_equal('one', MyDefaultArgs('one'))
+ assert_fails('call MyDefaultArgs("one", "two")', 'E118:')
+enddef
+
def Test_return_type_wrong()
" TODO: why is ! needed for Mac and FreeBSD?
CheckScriptFailure(['def Func(): number', 'return "a"', 'enddef'], 'expected number but got string')
diff --git a/src/version.c b/src/version.c
index 63461bd..1b8a0a8 100644
--- a/src/version.c
+++ b/src/version.c
@@ -743,6 +743,8 @@
static int included_patches[] =
{ /* Add new patch number below this line */
/**/
+ 206,
+/**/
205,
/**/
204,
diff --git a/src/vim9compile.c b/src/vim9compile.c
index 40411c4..d7c7c8b 100644
--- a/src/vim9compile.c
+++ b/src/vim9compile.c
@@ -1024,9 +1024,11 @@
isn->isn_arg.ufunc.cuf_argcount = argcount;
stack->ga_len -= argcount; // drop the arguments
-
- // drop the funcref/partial, get back the return value
- ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_any;
+ if (ga_grow(stack, 1) == FAIL)
+ return FAIL;
+ // add return value
+ ((type_T **)stack->ga_data)[stack->ga_len] = &t_any;
+ ++stack->ga_len;
return OK;
}
diff --git a/src/vim9execute.c b/src/vim9execute.c
index e4aa7fe..e84c70e 100644
--- a/src/vim9execute.c
+++ b/src/vim9execute.c
@@ -362,6 +362,7 @@
int idx;
int ret = FAIL;
dfunc_T *dfunc;
+ int optcount = ufunc_argcount(ufunc) - argc;
// Get pointer to item in the stack.
#define STACK_TV(idx) (((typval_T *)ectx.ec_stack.ga_data) + idx)
@@ -392,6 +393,12 @@
ectx.ec_frame = ectx.ec_stack.ga_len;
initial_frame_ptr = ectx.ec_frame;
+// TODO: Put omitted argument default values on the stack.
+ if (optcount > 0)
+ {
+ emsg("optional arguments not implemented yet");
+ return FAIL;
+ }
// dummy frame entries
for (idx = 0; idx < STACK_FRAME_SIZE; ++idx)
{