Skip to content

Commit 49cf7cc

Browse files
committed
patch 8.2.0529: Vim9: function argument with default not checked
Problem: Vim9: function argument with default not checked. Solution: Check type of argument with default value.
1 parent 0b76b42 commit 49cf7cc

4 files changed

Lines changed: 28 additions & 5 deletions

File tree

src/testdir/test_vim9_func.vim

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,7 @@ def Test_call_def_varargs()
128128
assert_equal('one,foo', MyDefVarargs('one'))
129129
assert_equal('one,two', MyDefVarargs('one', 'two'))
130130
assert_equal('one,two,three', MyDefVarargs('one', 'two', 'three'))
131+
call CheckDefFailure(['MyDefVarargs("one", 22)'], 'E1013: argument 2: type mismatch, expected string but got number')
131132
enddef
132133

133134
" Only varargs

src/userfunc.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3045,8 +3045,8 @@ ex_function(exarg_T *eap)
30453045
{
30463046
p = ((char_u **)argtypes.ga_data)[i];
30473047
if (p == NULL)
3048-
// todo: get type from default value
3049-
type = &t_any;
3048+
// will get the type from the default value
3049+
type = &t_unknown;
30503050
else
30513051
type = parse_type(&p, &fp->uf_type_list);
30523052
if (type == NULL)

src/version.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -738,6 +738,8 @@ static char *(features[]) =
738738

739739
static int included_patches[] =
740740
{ /* Add new patch number below this line */
741+
/**/
742+
529,
741743
/**/
742744
528,
743745
/**/

src/vim9compile.c

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5548,6 +5548,7 @@ compile_def_function(ufunc_T *ufunc, int set_return_type)
55485548
if (ufunc->uf_def_args.ga_len > 0)
55495549
{
55505550
int count = ufunc->uf_def_args.ga_len;
5551+
int first_def_arg = ufunc->uf_args.ga_len - count;
55515552
int i;
55525553
char_u *arg;
55535554
int off = STACK_FRAME_SIZE + (ufunc->uf_va_name != NULL ? 1 : 0);
@@ -5561,11 +5562,30 @@ compile_def_function(ufunc_T *ufunc, int set_return_type)
55615562
goto erret;
55625563
for (i = 0; i < count; ++i)
55635564
{
5565+
garray_T *stack = &cctx.ctx_type_stack;
5566+
type_T *val_type;
5567+
int arg_idx = first_def_arg + i;
5568+
55645569
ufunc->uf_def_arg_idx[i] = instr->ga_len;
55655570
arg = ((char_u **)(ufunc->uf_def_args.ga_data))[i];
5566-
if (compile_expr1(&arg, &cctx) == FAIL
5567-
|| generate_STORE(&cctx, ISN_STORE,
5568-
i - count - off, NULL) == FAIL)
5571+
if (compile_expr1(&arg, &cctx) == FAIL)
5572+
goto erret;
5573+
5574+
// If no type specified use the type of the default value.
5575+
// Otherwise check that the default value type matches the
5576+
// specified type.
5577+
val_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
5578+
if (ufunc->uf_arg_types[arg_idx] == &t_unknown)
5579+
ufunc->uf_arg_types[arg_idx] = val_type;
5580+
else if (check_type(ufunc->uf_arg_types[i], val_type, FALSE)
5581+
== FAIL)
5582+
{
5583+
arg_type_mismatch(ufunc->uf_arg_types[arg_idx], val_type,
5584+
arg_idx + 1);
5585+
goto erret;
5586+
}
5587+
5588+
if (generate_STORE(&cctx, ISN_STORE, i - count - off, NULL) == FAIL)
55695589
goto erret;
55705590
}
55715591

0 commit comments

Comments
 (0)