comparison src/vim9compile.c @ 19944:3055cd26e139 v8.2.0528

patch 8.2.0528: Vim9: function arguments insufficiently tested Commit: https://github.com/vim/vim/commit/0b76b42d0a09fb6f1ed79cfc153da4edd6154c89 Author: Bram Moolenaar <Bram@vim.org> Date: Tue Apr 7 22:05:08 2020 +0200 patch 8.2.0528: Vim9: function arguments insufficiently tested Problem: Vim9: function arguments insufficiently tested. Solution: Check types. Add more tests. Fix function with varargs only.
author Bram Moolenaar <Bram@vim.org>
date Tue, 07 Apr 2020 22:15:05 +0200
parents b471038ec3ea
children 8466e62a2481
comparison
equal deleted inserted replaced
19943:a456f90669a4 19944:3055cd26e139
128 128
129 static int compile_expr1(char_u **arg, cctx_T *cctx); 129 static int compile_expr1(char_u **arg, cctx_T *cctx);
130 static int compile_expr2(char_u **arg, cctx_T *cctx); 130 static int compile_expr2(char_u **arg, cctx_T *cctx);
131 static int compile_expr3(char_u **arg, cctx_T *cctx); 131 static int compile_expr3(char_u **arg, cctx_T *cctx);
132 static void delete_def_function_contents(dfunc_T *dfunc); 132 static void delete_def_function_contents(dfunc_T *dfunc);
133 static void arg_type_mismatch(type_T *expected, type_T *actual, int argidx);
134 static int check_type(type_T *expected, type_T *actual, int give_msg);
133 135
134 /* 136 /*
135 * Lookup variable "name" in the local scope and return the index. 137 * Lookup variable "name" in the local scope and return the index.
136 */ 138 */
137 static int 139 static int
1236 } 1238 }
1237 if (argcount < regular_args - ufunc->uf_def_args.ga_len) 1239 if (argcount < regular_args - ufunc->uf_def_args.ga_len)
1238 { 1240 {
1239 semsg(_(e_toofewarg), ufunc->uf_name); 1241 semsg(_(e_toofewarg), ufunc->uf_name);
1240 return FAIL; 1242 return FAIL;
1243 }
1244
1245 if (ufunc->uf_dfunc_idx >= 0)
1246 {
1247 int i;
1248
1249 for (i = 0; i < argcount; ++i)
1250 {
1251 type_T *expected;
1252 type_T *actual;
1253
1254 if (i < regular_args)
1255 {
1256 if (ufunc->uf_arg_types == NULL)
1257 continue;
1258 expected = ufunc->uf_arg_types[i];
1259 }
1260 else
1261 expected = ufunc->uf_va_type->tt_member;
1262 actual = ((type_T **)stack->ga_data)[stack->ga_len - argcount + i];
1263 if (check_type(expected, actual, FALSE) == FAIL)
1264 {
1265 arg_type_mismatch(expected, actual, i + 1);
1266 return FAIL;
1267 }
1268 }
1241 } 1269 }
1242 1270
1243 // Turn varargs into a list. 1271 // Turn varargs into a list.
1244 if (ufunc->uf_va_name != NULL) 1272 if (ufunc->uf_va_name != NULL)
1245 { 1273 {
2401 type_name(expected, &tofree1), type_name(actual, &tofree2)); 2429 type_name(expected, &tofree1), type_name(actual, &tofree2));
2402 vim_free(tofree1); 2430 vim_free(tofree1);
2403 vim_free(tofree2); 2431 vim_free(tofree2);
2404 } 2432 }
2405 2433
2434 static void
2435 arg_type_mismatch(type_T *expected, type_T *actual, int argidx)
2436 {
2437 char *tofree1, *tofree2;
2438
2439 semsg(_("E1013: argument %d: type mismatch, expected %s but got %s"),
2440 argidx,
2441 type_name(expected, &tofree1), type_name(actual, &tofree2));
2442 vim_free(tofree1);
2443 vim_free(tofree2);
2444 }
2445
2406 /* 2446 /*
2407 * Check if the expected and actual types match. 2447 * Check if the expected and actual types match.
2408 */ 2448 */
2409 static int 2449 static int
2410 check_type(type_T *expected, type_T *actual, int give_msg) 2450 check_type(type_T *expected, type_T *actual, int give_msg)