Skip to content

Commit 8b565c2

Browse files
committed
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.
1 parent 02aaad9 commit 8b565c2

8 files changed

Lines changed: 40 additions & 38 deletions

File tree

src/eval.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1291,7 +1291,7 @@ set_var_lval(
12911291
else
12921292
{
12931293
if (lp->ll_type != NULL
1294-
&& check_typval_type(lp->ll_type, rettv) == FAIL)
1294+
&& check_typval_type(lp->ll_type, rettv, 0) == FAIL)
12951295
return;
12961296
set_var_const(lp->ll_name, lp->ll_type, rettv, copy, flags);
12971297
}

src/proto/vim9type.pro

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@ type_T *get_func_type(type_T *ret_type, int argcount, garray_T *type_gap);
77
int func_type_add_arg_types(type_T *functype, int argcount, garray_T *type_gap);
88
type_T *typval2type(typval_T *tv, garray_T *type_gap);
99
type_T *typval2type_vimvar(typval_T *tv, garray_T *type_gap);
10-
int check_typval_type(type_T *expected, typval_T *actual_tv);
10+
int check_typval_type(type_T *expected, typval_T *actual_tv, int argidx);
1111
void type_mismatch(type_T *expected, type_T *actual);
1212
void arg_type_mismatch(type_T *expected, type_T *actual, int argidx);
13-
int check_type(type_T *expected, type_T *actual, int give_msg);
13+
int check_type(type_T *expected, type_T *actual, int give_msg, int argidx);
1414
char_u *skip_type(char_u *start, int optional);
1515
type_T *parse_type(char_u **arg, garray_T *type_gap);
1616
void common_type(type_T *type1, type_T *type2, type_T **dest, garray_T *type_gap);

src/testdir/test_vim9_func.vim

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,7 @@ def Test_call_wrong_args()
231231
enddef
232232
Func([])
233233
END
234-
call CheckScriptFailure(lines, 'E1012: type mismatch, expected string but got list<unknown>', 5)
234+
call CheckScriptFailure(lines, 'E1013: argument 1: type mismatch, expected string but got list<unknown>', 5)
235235
enddef
236236

237237
" Default arg and varargs
@@ -278,7 +278,7 @@ def Test_call_def_varargs()
278278
enddef
279279
Func(1, 2, 3)
280280
END
281-
CheckScriptFailure(lines, 'E1012:')
281+
CheckScriptFailure(lines, 'E1013: argument 1: type mismatch')
282282

283283
lines =<< trim END
284284
vim9script
@@ -287,7 +287,7 @@ def Test_call_def_varargs()
287287
enddef
288288
Func('a', 9)
289289
END
290-
CheckScriptFailure(lines, 'E1012:')
290+
CheckScriptFailure(lines, 'E1013: argument 2: type mismatch')
291291

292292
lines =<< trim END
293293
vim9script
@@ -296,7 +296,7 @@ def Test_call_def_varargs()
296296
enddef
297297
Func(1, 'a')
298298
END
299-
CheckScriptFailure(lines, 'E1012:')
299+
CheckScriptFailure(lines, 'E1013: argument 1: type mismatch')
300300
enddef
301301

302302
def Test_call_call()
@@ -691,7 +691,7 @@ def Test_vim9script_call_fail_type()
691691
enddef
692692
MyFunc(1234)
693693
END
694-
CheckScriptFailure(lines, 'E1012: type mismatch, expected string but got number')
694+
CheckScriptFailure(lines, 'E1013: argument 1: type mismatch, expected string but got number')
695695
enddef
696696

697697
def Test_vim9script_call_fail_const()

src/version.c

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

755755
static int included_patches[] =
756756
{ /* Add new patch number below this line */
757+
/**/
758+
1551,
757759
/**/
758760
1550,
759761
/**/

src/vim9compile.c

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -729,7 +729,7 @@ need_type(
729729
cctx_T *cctx,
730730
int silent)
731731
{
732-
if (check_type(expected, actual, FALSE) == OK)
732+
if (check_type(expected, actual, FALSE, 0) == OK)
733733
return OK;
734734
if (actual->tt_type != VAR_ANY
735735
&& actual->tt_type != VAR_UNKNOWN
@@ -3581,7 +3581,7 @@ compile_expr7t(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
35813581

35823582
generate_ppconst(cctx, ppconst);
35833583
actual = ((type_T **)stack->ga_data)[stack->ga_len - 1];
3584-
if (check_type(want_type, actual, FALSE) == FAIL)
3584+
if (check_type(want_type, actual, FALSE, 0) == FAIL)
35853585
{
35863586
if (need_type(actual, want_type, -1, cctx, FALSE) == FAIL)
35873587
return FAIL;
@@ -6500,13 +6500,9 @@ compile_def_function(ufunc_T *ufunc, int set_return_type, cctx_T *outer_cctx)
65006500
did_set_arg_type = TRUE;
65016501
ufunc->uf_arg_types[arg_idx] = val_type;
65026502
}
6503-
else if (check_type(ufunc->uf_arg_types[arg_idx], val_type, FALSE)
6504-
== FAIL)
6505-
{
6506-
arg_type_mismatch(ufunc->uf_arg_types[arg_idx], val_type,
6507-
arg_idx + 1);
6503+
else if (check_type(ufunc->uf_arg_types[arg_idx], val_type,
6504+
TRUE, arg_idx + 1) == FAIL)
65086505
goto erret;
6509-
}
65106506

65116507
if (generate_STORE(&cctx, ISN_STORE, i - count - off, NULL) == FAIL)
65126508
goto erret;

src/vim9execute.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -792,8 +792,8 @@ call_def_function(
792792
for (idx = 0; idx < argc; ++idx)
793793
{
794794
if (ufunc->uf_arg_types != NULL && idx < ufunc->uf_args.ga_len
795-
&& check_typval_type(ufunc->uf_arg_types[idx], &argv[idx])
796-
== FAIL)
795+
&& check_typval_type(ufunc->uf_arg_types[idx], &argv[idx],
796+
idx + 1) == FAIL)
797797
goto failed_early;
798798
copy_tv(&argv[idx], STACK_TV_BOT(0));
799799
++ectx.ec_stack.ga_len;
@@ -822,7 +822,8 @@ call_def_function(
822822

823823
for (idx = 0; idx < vararg_count; ++idx)
824824
{
825-
if (check_typval_type(expected, &li->li_tv) == FAIL)
825+
if (check_typval_type(expected, &li->li_tv,
826+
argc + idx + 1) == FAIL)
826827
goto failed_early;
827828
li = li->li_next;
828829
}

src/vim9script.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -580,7 +580,7 @@ check_script_var_type(typval_T *dest, typval_T *value, char_u *name)
580580
semsg(_(e_readonlyvar), name);
581581
return FAIL;
582582
}
583-
return check_typval_type(sv->sv_type, value);
583+
return check_typval_type(sv->sv_type, value, 0);
584584
}
585585
}
586586
iemsg("check_script_var_type(): not found");

src/vim9type.c

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -304,7 +304,7 @@ typval2type_vimvar(typval_T *tv, garray_T *type_gap)
304304
* Return FAIL if "expected" and "actual" don't match.
305305
*/
306306
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)
308308
{
309309
garray_T type_list;
310310
type_T *actual_type;
@@ -313,40 +313,41 @@ check_typval_type(type_T *expected, typval_T *actual_tv)
313313
ga_init2(&type_list, sizeof(type_T *), 10);
314314
actual_type = typval2type(actual_tv, &type_list);
315315
if (actual_type != NULL)
316-
res = check_type(expected, actual_type, TRUE);
316+
res = check_type(expected, actual_type, TRUE, argidx);
317317
clear_type_list(&type_list);
318318
return res;
319319
}
320320

321321
void
322322
type_mismatch(type_T *expected, type_T *actual)
323323
{
324-
char *tofree1, *tofree2;
325-
326-
semsg(_(e_type_mismatch_expected_str_but_got_str),
327-
type_name(expected, &tofree1), type_name(actual, &tofree2));
328-
vim_free(tofree1);
329-
vim_free(tofree2);
324+
arg_type_mismatch(expected, actual, 0);
330325
}
331326

332327
void
333328
arg_type_mismatch(type_T *expected, type_T *actual, int argidx)
334329
{
335330
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));
331+
char *typename1 = type_name(expected, &tofree1);
332+
char *typename2 = 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);
340340
vim_free(tofree1);
341341
vim_free(tofree2);
342342
}
343343

344344
/*
345345
* Check if the expected and actual types match.
346346
* Does not allow for assigning "any" to a specific type.
347+
* When "argidx" > 0 it is included in the error message.
347348
*/
348349
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)
350351
{
351352
int ret = OK;
352353

@@ -359,19 +360,21 @@ check_type(type_T *expected, type_T *actual, int give_msg)
359360
if (expected->tt_type != actual->tt_type)
360361
{
361362
if (give_msg)
362-
type_mismatch(expected, actual);
363+
arg_type_mismatch(expected, actual, argidx);
363364
return FAIL;
364365
}
365366
if (expected->tt_type == VAR_DICT || expected->tt_type == VAR_LIST)
366367
{
367368
// "unknown" is used for an empty list or dict
368369
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);
370372
}
371373
else if (expected->tt_type == VAR_FUNC)
372374
{
373375
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);
375378
if (ret == OK && expected->tt_argcount != -1
376379
&& (actual->tt_argcount < expected->tt_min_argcount
377380
|| actual->tt_argcount > expected->tt_argcount))
@@ -383,7 +386,7 @@ check_type(type_T *expected, type_T *actual, int give_msg)
383386
for (i = 0; i < expected->tt_argcount; ++i)
384387
// Allow for using "any" argument type, lambda's have them.
385388
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)
387390
== FAIL)
388391
{
389392
ret = FAIL;
@@ -392,7 +395,7 @@ check_type(type_T *expected, type_T *actual, int give_msg)
392395
}
393396
}
394397
if (ret == FAIL && give_msg)
395-
type_mismatch(expected, actual);
398+
arg_type_mismatch(expected, actual, argidx);
396399
}
397400
return ret;
398401
}

0 commit comments

Comments
 (0)