summaryrefslogtreecommitdiffstats
path: root/src/vim9compile.c
diff options
context:
space:
mode:
authorBram Moolenaar <Bram@vim.org>2020-04-07 22:45:00 +0200
committerBram Moolenaar <Bram@vim.org>2020-04-07 22:45:00 +0200
commit49cf7cc8d2df1509cbe23647166c6cc954d59513 (patch)
tree99aa1fd77604c8b0a1f0dc325d529cbae4fdbb11 /src/vim9compile.c
parent0b76b42d0a09fb6f1ed79cfc153da4edd6154c89 (diff)
patch 8.2.0529: Vim9: function argument with default not checkedv8.2.0529
Problem: Vim9: function argument with default not checked. Solution: Check type of argument with default value.
Diffstat (limited to 'src/vim9compile.c')
-rw-r--r--src/vim9compile.c26
1 files changed, 23 insertions, 3 deletions
diff --git a/src/vim9compile.c b/src/vim9compile.c
index fb0adb3b42..a8a7647468 100644
--- a/src/vim9compile.c
+++ b/src/vim9compile.c
@@ -5548,6 +5548,7 @@ compile_def_function(ufunc_T *ufunc, int set_return_type)
if (ufunc->uf_def_args.ga_len > 0)
{
int count = ufunc->uf_def_args.ga_len;
+ int first_def_arg = ufunc->uf_args.ga_len - count;
int i;
char_u *arg;
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)
goto erret;
for (i = 0; i < count; ++i)
{
+ garray_T *stack = &cctx.ctx_type_stack;
+ type_T *val_type;
+ int arg_idx = first_def_arg + i;
+
ufunc->uf_def_arg_idx[i] = instr->ga_len;
arg = ((char_u **)(ufunc->uf_def_args.ga_data))[i];
- if (compile_expr1(&arg, &cctx) == FAIL
- || generate_STORE(&cctx, ISN_STORE,
- i - count - off, NULL) == FAIL)
+ if (compile_expr1(&arg, &cctx) == FAIL)
+ goto erret;
+
+ // If no type specified use the type of the default value.
+ // Otherwise check that the default value type matches the
+ // specified type.
+ val_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
+ if (ufunc->uf_arg_types[arg_idx] == &t_unknown)
+ ufunc->uf_arg_types[arg_idx] = val_type;
+ else if (check_type(ufunc->uf_arg_types[i], val_type, FALSE)
+ == FAIL)
+ {
+ arg_type_mismatch(ufunc->uf_arg_types[arg_idx], val_type,
+ arg_idx + 1);
+ goto erret;
+ }
+
+ if (generate_STORE(&cctx, ISN_STORE, i - count - off, NULL) == FAIL)
goto erret;
}