Skip to content

Commit 7a3fe3e

Browse files
committed
patch 8.2.3200: Vim9: hard to guess where a type error is given
Problem: Vim9: hard to guess where a type error is given. Solution: Add the function name where possible. (closes #8608)
1 parent a749019 commit 7a3fe3e

13 files changed

Lines changed: 78 additions & 49 deletions

File tree

src/dict.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1073,7 +1073,7 @@ eval_dict(char_u **arg, typval_T *rettv, evalarg_T *evalarg, int literal)
10731073
* Otherwise duplicate keys are ignored ("action" is "keep").
10741074
*/
10751075
void
1076-
dict_extend(dict_T *d1, dict_T *d2, char_u *action)
1076+
dict_extend(dict_T *d1, dict_T *d2, char_u *action, char *func_name)
10771077
{
10781078
dictitem_T *di1;
10791079
hashitem_T *hi2;
@@ -1106,8 +1106,8 @@ dict_extend(dict_T *d1, dict_T *d2, char_u *action)
11061106
}
11071107

11081108
if (type != NULL
1109-
&& check_typval_arg_type(type, &HI2DI(hi2)->di_tv, 0)
1110-
== FAIL)
1109+
&& check_typval_arg_type(type, &HI2DI(hi2)->di_tv,
1110+
func_name, 0) == FAIL)
11111111
break;
11121112

11131113
if (di1 == NULL)

src/errors.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,8 +192,12 @@ EXTERN char e_name_too_long_str[]
192192
INIT(= N_("E1011: Name too long: %s"));
193193
EXTERN char e_type_mismatch_expected_str_but_got_str[]
194194
INIT(= N_("E1012: Type mismatch; expected %s but got %s"));
195+
EXTERN char e_type_mismatch_expected_str_but_got_str_in_str[]
196+
INIT(= N_("E1012: Type mismatch; expected %s but got %s in %s"));
195197
EXTERN char e_argument_nr_type_mismatch_expected_str_but_got_str[]
196198
INIT(= N_("E1013: Argument %d: type mismatch, expected %s but got %s"));
199+
EXTERN char e_argument_nr_type_mismatch_expected_str_but_got_str_in_str[]
200+
INIT(= N_("E1013: Argument %d: type mismatch, expected %s but got %s in %s"));
197201
EXTERN char e_invalid_key_str[]
198202
INIT(= N_("E1014: Invalid key: %s"));
199203
EXTERN char e_name_expected_str[]
@@ -494,6 +498,8 @@ EXTERN char e_register_name_must_be_one_char_str[]
494498
INIT(= N_("E1162: Register name must be one character: %s"));
495499
EXTERN char e_variable_nr_type_mismatch_expected_str_but_got_str[]
496500
INIT(= N_("E1163: Variable %d: type mismatch, expected %s but got %s"));
501+
EXTERN char e_variable_nr_type_mismatch_expected_str_but_got_str_in_str[]
502+
INIT(= N_("E1163: Variable %d: type mismatch, expected %s but got %s in %s"));
497503
EXTERN char e_vim9cmd_must_be_followed_by_command[]
498504
INIT(= N_("E1164: vim9cmd must be followed by a command"));
499505
EXTERN char e_cannot_use_range_with_assignment_str[]

src/eval.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1365,8 +1365,8 @@ set_var_lval(
13651365
}
13661366
else
13671367
{
1368-
if (lp->ll_type != NULL
1369-
&& check_typval_arg_type(lp->ll_type, rettv, 0) == FAIL)
1368+
if (lp->ll_type != NULL && check_typval_arg_type(lp->ll_type, rettv,
1369+
NULL, 0) == FAIL)
13701370
return;
13711371
set_var_const(lp->ll_name, lp->ll_type, rettv, copy,
13721372
flags, var_idx);
@@ -1450,7 +1450,8 @@ set_var_lval(
14501450
}
14511451

14521452
if (lp->ll_valtype != NULL
1453-
&& check_typval_arg_type(lp->ll_valtype, rettv, 0) == FAIL)
1453+
&& check_typval_arg_type(lp->ll_valtype, rettv,
1454+
NULL, 0) == FAIL)
14541455
return;
14551456

14561457
if (lp->ll_newkey != NULL)

src/if_py_both.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2043,7 +2043,7 @@ DictionaryUpdate(DictionaryObject *self, PyObject *args, PyObject *kwargs)
20432043
return NULL;
20442044

20452045
VimTryStart();
2046-
dict_extend(self->dict, tv.vval.v_dict, (char_u *) "force");
2046+
dict_extend(self->dict, tv.vval.v_dict, (char_u *) "force", NULL);
20472047
clear_tv(&tv);
20482048
if (VimTryEnd())
20492049
return NULL;

src/list.c

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -605,7 +605,8 @@ list_append_tv(list_T *l, typval_T *tv)
605605
listitem_T *li;
606606

607607
if (l->lv_type != NULL && l->lv_type->tt_member != NULL
608-
&& check_typval_arg_type(l->lv_type->tt_member, tv, 0) == FAIL)
608+
&& check_typval_arg_type(l->lv_type->tt_member, tv,
609+
NULL, 0) == FAIL)
609610
return FAIL;
610611
li = listitem_alloc();
611612
if (li == NULL)
@@ -722,7 +723,8 @@ list_insert_tv(list_T *l, typval_T *tv, listitem_T *item)
722723
listitem_T *ni;
723724

724725
if (l->lv_type != NULL && l->lv_type->tt_member != NULL
725-
&& check_typval_arg_type(l->lv_type->tt_member, tv, 0) == FAIL)
726+
&& check_typval_arg_type(l->lv_type->tt_member, tv,
727+
NULL, 0) == FAIL)
726728
return FAIL;
727729
ni = listitem_alloc();
728730
if (ni == NULL)
@@ -2085,9 +2087,9 @@ filter_map(typval_T *argvars, typval_T *rettv, filtermap_T filtermap)
20852087
blob_T *b = NULL;
20862088
int rem;
20872089
int todo;
2088-
char_u *ermsg = (char_u *)(filtermap == FILTERMAP_MAP ? "map()"
2090+
char *func_name = filtermap == FILTERMAP_MAP ? "map()"
20892091
: filtermap == FILTERMAP_MAPNEW ? "mapnew()"
2090-
: "filter()");
2092+
: "filter()";
20912093
char_u *arg_errmsg = (char_u *)(filtermap == FILTERMAP_MAP
20922094
? N_("map() argument")
20932095
: filtermap == FILTERMAP_MAPNEW
@@ -2144,7 +2146,7 @@ filter_map(typval_T *argvars, typval_T *rettv, filtermap_T filtermap)
21442146
}
21452147
else
21462148
{
2147-
semsg(_(e_listdictblobarg), ermsg);
2149+
semsg(_(e_listdictblobarg), func_name);
21482150
goto theend;
21492151
}
21502152

@@ -2210,7 +2212,8 @@ filter_map(typval_T *argvars, typval_T *rettv, filtermap_T filtermap)
22102212
if (filtermap == FILTERMAP_MAP)
22112213
{
22122214
if (type != NULL && check_typval_arg_type(
2213-
type->tt_member, &newtv, 0) == FAIL)
2215+
type->tt_member, &newtv,
2216+
func_name, 0) == FAIL)
22142217
{
22152218
clear_tv(&newtv);
22162219
break;
@@ -2345,7 +2348,8 @@ filter_map(typval_T *argvars, typval_T *rettv, filtermap_T filtermap)
23452348
{
23462349
if (filtermap == FILTERMAP_MAP && type != NULL
23472350
&& check_typval_arg_type(
2348-
type->tt_member, &newtv, 0) == FAIL)
2351+
type->tt_member, &newtv,
2352+
func_name, 0) == FAIL)
23492353
{
23502354
clear_tv(&newtv);
23512355
break;
@@ -2389,7 +2393,7 @@ filter_map(typval_T *argvars, typval_T *rettv, filtermap_T filtermap)
23892393
if (filtermap == FILTERMAP_MAP)
23902394
{
23912395
if (type != NULL && check_typval_arg_type(
2392-
type->tt_member, &newtv, 0) == FAIL)
2396+
type->tt_member, &newtv, func_name, 0) == FAIL)
23932397
{
23942398
clear_tv(&newtv);
23952399
break;
@@ -2627,6 +2631,7 @@ extend(typval_T *argvars, typval_T *rettv, char_u *arg_errmsg, int is_new)
26272631
{
26282632
type_T *type = NULL;
26292633
garray_T type_list;
2634+
char *func_name = is_new ? "extendnew()" : "extend()";
26302635

26312636
if (!is_new && in_vim9script())
26322637
{
@@ -2680,7 +2685,7 @@ extend(typval_T *argvars, typval_T *rettv, char_u *arg_errmsg, int is_new)
26802685
else
26812686
item = NULL;
26822687
if (type != NULL && check_typval_arg_type(
2683-
type, &argvars[1], 2) == FAIL)
2688+
type, &argvars[1], func_name, 2) == FAIL)
26842689
goto theend;
26852690
list_extend(l1, l2, item);
26862691

@@ -2737,10 +2742,10 @@ extend(typval_T *argvars, typval_T *rettv, char_u *arg_errmsg, int is_new)
27372742
else
27382743
action = (char_u *)"force";
27392744

2740-
if (type != NULL && check_typval_arg_type(
2741-
type, &argvars[1], 2) == FAIL)
2745+
if (type != NULL && check_typval_arg_type(type, &argvars[1],
2746+
func_name, 2) == FAIL)
27422747
goto theend;
2743-
dict_extend(d1, d2, action);
2748+
dict_extend(d1, d2, action, func_name);
27442749

27452750
if (is_new)
27462751
{
@@ -2753,7 +2758,7 @@ extend(typval_T *argvars, typval_T *rettv, char_u *arg_errmsg, int is_new)
27532758
}
27542759
}
27552760
else
2756-
semsg(_(e_listdictarg), is_new ? "extendnew()" : "extend()");
2761+
semsg(_(e_listdictarg), func_name);
27572762

27582763
theend:
27592764
if (type != NULL)

src/proto/dict.pro

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ char_u *dict2string(typval_T *tv, int copyID, int restore_copyID);
3737
char_u *skip_literal_key(char_u *key);
3838
char_u *get_literal_key(char_u **arg);
3939
int eval_dict(char_u **arg, typval_T *rettv, evalarg_T *evalarg, int literal);
40-
void dict_extend(dict_T *d1, dict_T *d2, char_u *action);
40+
void dict_extend(dict_T *d1, dict_T *d2, char_u *action, char *func_name);
4141
dictitem_T *dict_lookup(hashitem_T *hi);
4242
int dict_equal(dict_T *d1, dict_T *d2, int ic, int recursive);
4343
void f_items(typval_T *argvars, typval_T *rettv);

src/proto/vim9type.pro

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ int func_type_add_arg_types(type_T *functype, int argcount, garray_T *type_gap);
1111
int need_convert_to_bool(type_T *type, typval_T *tv);
1212
type_T *typval2type(typval_T *tv, int copyID, garray_T *type_gap, int do_member);
1313
type_T *typval2type_vimvar(typval_T *tv, garray_T *type_gap);
14-
int check_typval_arg_type(type_T *expected, typval_T *actual_tv, int arg_idx);
14+
int check_typval_arg_type(type_T *expected, typval_T *actual_tv, char *func_name, int arg_idx);
1515
int check_typval_type(type_T *expected, typval_T *actual_tv, where_T where);
1616
void type_mismatch(type_T *expected, type_T *actual);
1717
void arg_type_mismatch(type_T *expected, type_T *actual, int arg_idx);

src/structs.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4438,7 +4438,10 @@ typedef enum {
44384438

44394439
// Struct used to pass to error messages about where the error happened.
44404440
typedef struct {
4441+
char *wt_func_name; // function name or NULL
44414442
char wt_index; // argument or variable index, 0 means unknown
44424443
char wt_variable; // "variable" when TRUE, "argument" otherwise
44434444
} where_T;
44444445

4446+
#define WHERE_INIT {NULL, 0, 0}
4447+

src/testdir/test_vim9_builtin.vim

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -795,6 +795,8 @@ def Test_extend_arg_types()
795795

796796
CheckDefFailure(['extend([1], ["b"])'], 'E1013: Argument 2: type mismatch, expected list<number> but got list<string>')
797797
CheckDefExecFailure(['extend([1], ["b", 1])'], 'E1013: Argument 2: type mismatch, expected list<number> but got list<any>')
798+
799+
CheckScriptFailure(['vim9script', 'extend([1], ["b", 1])'], 'E1013: Argument 2: type mismatch, expected list<number> but got list<any> in extend()')
798800
enddef
799801

800802
func g:ExtendDict(d)
@@ -1741,19 +1743,19 @@ def Test_map_item_type()
17411743
var l: list<number> = [0]
17421744
echo map(l, (_, v) => [])
17431745
END
1744-
CheckDefExecAndScriptFailure(lines, 'E1012: Type mismatch; expected number but got list<unknown>', 2)
1746+
CheckDefExecAndScriptFailure(lines, 'E1012: Type mismatch; expected number but got list<unknown> in map()', 2)
17451747

17461748
lines =<< trim END
17471749
var l: list<number> = range(2)
17481750
echo map(l, (_, v) => [])
17491751
END
1750-
CheckDefExecAndScriptFailure(lines, 'E1012: Type mismatch; expected number but got list<unknown>', 2)
1752+
CheckDefExecAndScriptFailure(lines, 'E1012: Type mismatch; expected number but got list<unknown> in map()', 2)
17511753

17521754
lines =<< trim END
17531755
var d: dict<number> = {key: 0}
17541756
echo map(d, (_, v) => [])
17551757
END
1756-
CheckDefExecAndScriptFailure(lines, 'E1012: Type mismatch; expected number but got list<unknown>', 2)
1758+
CheckDefExecAndScriptFailure(lines, 'E1012: Type mismatch; expected number but got list<unknown> in map()', 2)
17571759
enddef
17581760

17591761
def Test_maparg()

src/version.c

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

756756
static int included_patches[] =
757757
{ /* Add new patch number below this line */
758+
/**/
759+
3200,
758760
/**/
759761
3199,
760762
/**/

0 commit comments

Comments
 (0)