comparison src/vim9type.c @ 22004:a9e60176dcd3 v8.2.1551

patch 8.2.1551: Vim9: error for argument type does not mention the number Commit: https://github.com/vim/vim/commit/8b565c2c1522e0c41e3d18e1bb6e1bc4b3686842 Author: Bram Moolenaar <Bram@vim.org> Date: Sun Aug 30 23:24:20 2020 +0200 patch 8.2.1551: Vim9: error for argument type does not mention the number Problem: Vim9: error for argument type does not mention the number. Solution: Pass the argument number to where the error is given.
author Bram Moolenaar <Bram@vim.org>
date Sun, 30 Aug 2020 23:30:04 +0200
parents a211bca98bc3
children 2463b3d89ce2
comparison
equal deleted inserted replaced
22003:b22488a6de96 22004:a9e60176dcd3
302 302
303 /* 303 /*
304 * Return FAIL if "expected" and "actual" don't match. 304 * Return FAIL if "expected" and "actual" don't match.
305 */ 305 */
306 int 306 int
307 check_typval_type(type_T *expected, typval_T *actual_tv) 307 check_typval_type(type_T *expected, typval_T *actual_tv, int argidx)
308 { 308 {
309 garray_T type_list; 309 garray_T type_list;
310 type_T *actual_type; 310 type_T *actual_type;
311 int res = FAIL; 311 int res = FAIL;
312 312
313 ga_init2(&type_list, sizeof(type_T *), 10); 313 ga_init2(&type_list, sizeof(type_T *), 10);
314 actual_type = typval2type(actual_tv, &type_list); 314 actual_type = typval2type(actual_tv, &type_list);
315 if (actual_type != NULL) 315 if (actual_type != NULL)
316 res = check_type(expected, actual_type, TRUE); 316 res = check_type(expected, actual_type, TRUE, argidx);
317 clear_type_list(&type_list); 317 clear_type_list(&type_list);
318 return res; 318 return res;
319 } 319 }
320 320
321 void 321 void
322 type_mismatch(type_T *expected, type_T *actual) 322 type_mismatch(type_T *expected, type_T *actual)
323 { 323 {
324 arg_type_mismatch(expected, actual, 0);
325 }
326
327 void
328 arg_type_mismatch(type_T *expected, type_T *actual, int argidx)
329 {
324 char *tofree1, *tofree2; 330 char *tofree1, *tofree2;
325 331 char *typename1 = type_name(expected, &tofree1);
326 semsg(_(e_type_mismatch_expected_str_but_got_str), 332 char *typename2 = type_name(actual, &tofree2);
327 type_name(expected, &tofree1), type_name(actual, &tofree2)); 333
334 if (argidx > 0)
335 semsg(_(e_argument_nr_type_mismatch_expected_str_but_got_str),
336 argidx, typename1, typename2);
337 else
338 semsg(_(e_type_mismatch_expected_str_but_got_str),
339 typename1, typename2);
328 vim_free(tofree1); 340 vim_free(tofree1);
329 vim_free(tofree2); 341 vim_free(tofree2);
330 } 342 }
331 343
332 void
333 arg_type_mismatch(type_T *expected, type_T *actual, int argidx)
334 {
335 char *tofree1, *tofree2;
336
337 semsg(_(e_argument_nr_type_mismatch_expected_str_but_got_str),
338 argidx,
339 type_name(expected, &tofree1), type_name(actual, &tofree2));
340 vim_free(tofree1);
341 vim_free(tofree2);
342 }
343
344 /* 344 /*
345 * Check if the expected and actual types match. 345 * Check if the expected and actual types match.
346 * Does not allow for assigning "any" to a specific type. 346 * Does not allow for assigning "any" to a specific type.
347 * When "argidx" > 0 it is included in the error message.
347 */ 348 */
348 int 349 int
349 check_type(type_T *expected, type_T *actual, int give_msg) 350 check_type(type_T *expected, type_T *actual, int give_msg, int argidx)
350 { 351 {
351 int ret = OK; 352 int ret = OK;
352 353
353 // When expected is "unknown" we accept any actual type. 354 // When expected is "unknown" we accept any actual type.
354 // When expected is "any" we accept any actual type except "void". 355 // When expected is "any" we accept any actual type except "void".
357 358
358 { 359 {
359 if (expected->tt_type != actual->tt_type) 360 if (expected->tt_type != actual->tt_type)
360 { 361 {
361 if (give_msg) 362 if (give_msg)
362 type_mismatch(expected, actual); 363 arg_type_mismatch(expected, actual, argidx);
363 return FAIL; 364 return FAIL;
364 } 365 }
365 if (expected->tt_type == VAR_DICT || expected->tt_type == VAR_LIST) 366 if (expected->tt_type == VAR_DICT || expected->tt_type == VAR_LIST)
366 { 367 {
367 // "unknown" is used for an empty list or dict 368 // "unknown" is used for an empty list or dict
368 if (actual->tt_member != &t_unknown) 369 if (actual->tt_member != &t_unknown)
369 ret = check_type(expected->tt_member, actual->tt_member, FALSE); 370 ret = check_type(expected->tt_member, actual->tt_member,
371 FALSE, 0);
370 } 372 }
371 else if (expected->tt_type == VAR_FUNC) 373 else if (expected->tt_type == VAR_FUNC)
372 { 374 {
373 if (expected->tt_member != &t_unknown) 375 if (expected->tt_member != &t_unknown)
374 ret = check_type(expected->tt_member, actual->tt_member, FALSE); 376 ret = check_type(expected->tt_member, actual->tt_member,
377 FALSE, 0);
375 if (ret == OK && expected->tt_argcount != -1 378 if (ret == OK && expected->tt_argcount != -1
376 && (actual->tt_argcount < expected->tt_min_argcount 379 && (actual->tt_argcount < expected->tt_min_argcount
377 || actual->tt_argcount > expected->tt_argcount)) 380 || actual->tt_argcount > expected->tt_argcount))
378 ret = FAIL; 381 ret = FAIL;
379 if (expected->tt_args != NULL && actual->tt_args != NULL) 382 if (expected->tt_args != NULL && actual->tt_args != NULL)
381 int i; 384 int i;
382 385
383 for (i = 0; i < expected->tt_argcount; ++i) 386 for (i = 0; i < expected->tt_argcount; ++i)
384 // Allow for using "any" argument type, lambda's have them. 387 // Allow for using "any" argument type, lambda's have them.
385 if (actual->tt_args[i] != &t_any && check_type( 388 if (actual->tt_args[i] != &t_any && check_type(
386 expected->tt_args[i], actual->tt_args[i], FALSE) 389 expected->tt_args[i], actual->tt_args[i], FALSE, 0)
387 == FAIL) 390 == FAIL)
388 { 391 {
389 ret = FAIL; 392 ret = FAIL;
390 break; 393 break;
391 } 394 }
392 } 395 }
393 } 396 }
394 if (ret == FAIL && give_msg) 397 if (ret == FAIL && give_msg)
395 type_mismatch(expected, actual); 398 arg_type_mismatch(expected, actual, argidx);
396 } 399 }
397 return ret; 400 return ret;
398 } 401 }
399 402
400 /* 403 /*