• R/O
  • SSH

vim: Commit

Mirror of the Vim source from https://github.com/vim/vim


Commit MetaInfo

Revisãocfbf40f749b0813414b839cfb4ba8ad79f201ad4 (tree)
Hora2021-07-22 22:00:05
AutorBram Moolenaar <Bram@vim....>
CommiterBram Moolenaar

Mensagem de Log

patch 8.2.3200: Vim9: hard to guess where a type error is given

Commit: https://github.com/vim/vim/commit/7a3fe3e180bdbce8f193abdf399559c5154bdaae
Author: Bram Moolenaar <Bram@vim.org>
Date: Thu Jul 22 14:58:47 2021 +0200

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 https://github.com/vim/vim/issues/8608)

Mudança Sumário

Diff

diff -r 6d48afa9f12d -r cfbf40f749b0 src/dict.c
--- a/src/dict.c Thu Jul 22 12:30:05 2021 +0200
+++ b/src/dict.c Thu Jul 22 15:00:05 2021 +0200
@@ -1073,7 +1073,7 @@
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 @@
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)
diff -r 6d48afa9f12d -r cfbf40f749b0 src/errors.h
--- a/src/errors.h Thu Jul 22 12:30:05 2021 +0200
+++ b/src/errors.h Thu Jul 22 15:00:05 2021 +0200
@@ -192,8 +192,12 @@
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 @@
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[]
diff -r 6d48afa9f12d -r cfbf40f749b0 src/eval.c
--- a/src/eval.c Thu Jul 22 12:30:05 2021 +0200
+++ b/src/eval.c Thu Jul 22 15:00:05 2021 +0200
@@ -1365,8 +1365,8 @@
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 @@
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)
diff -r 6d48afa9f12d -r cfbf40f749b0 src/if_py_both.h
--- a/src/if_py_both.h Thu Jul 22 12:30:05 2021 +0200
+++ b/src/if_py_both.h Thu Jul 22 15:00:05 2021 +0200
@@ -2043,7 +2043,7 @@
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;
diff -r 6d48afa9f12d -r cfbf40f749b0 src/list.c
--- a/src/list.c Thu Jul 22 12:30:05 2021 +0200
+++ b/src/list.c Thu Jul 22 15:00:05 2021 +0200
@@ -605,7 +605,8 @@
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 @@
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 @@
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 @@
21442146 }
21452147 else
21462148 {
2147- semsg(_(e_listdictblobarg), ermsg);
2149+ semsg(_(e_listdictblobarg), func_name);
21482150 goto theend;
21492151 }
21502152
@@ -2210,7 +2212,8 @@
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 @@
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 @@
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 @@
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 @@
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 @@
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 @@
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)
diff -r 6d48afa9f12d -r cfbf40f749b0 src/proto/dict.pro
--- a/src/proto/dict.pro Thu Jul 22 12:30:05 2021 +0200
+++ b/src/proto/dict.pro Thu Jul 22 15:00:05 2021 +0200
@@ -37,7 +37,7 @@
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);
diff -r 6d48afa9f12d -r cfbf40f749b0 src/proto/vim9type.pro
--- a/src/proto/vim9type.pro Thu Jul 22 12:30:05 2021 +0200
+++ b/src/proto/vim9type.pro Thu Jul 22 15:00:05 2021 +0200
@@ -11,7 +11,7 @@
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);
diff -r 6d48afa9f12d -r cfbf40f749b0 src/structs.h
--- a/src/structs.h Thu Jul 22 12:30:05 2021 +0200
+++ b/src/structs.h Thu Jul 22 15:00:05 2021 +0200
@@ -4438,7 +4438,10 @@
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+
diff -r 6d48afa9f12d -r cfbf40f749b0 src/testdir/test_vim9_builtin.vim
--- a/src/testdir/test_vim9_builtin.vim Thu Jul 22 12:30:05 2021 +0200
+++ b/src/testdir/test_vim9_builtin.vim Thu Jul 22 15:00:05 2021 +0200
@@ -795,6 +795,8 @@
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 @@
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()
diff -r 6d48afa9f12d -r cfbf40f749b0 src/version.c
--- a/src/version.c Thu Jul 22 12:30:05 2021 +0200
+++ b/src/version.c Thu Jul 22 15:00:05 2021 +0200
@@ -756,6 +756,8 @@
756756 static int included_patches[] =
757757 { /* Add new patch number below this line */
758758 /**/
759+ 3200,
760+/**/
759761 3199,
760762 /**/
761763 3198,
diff -r 6d48afa9f12d -r cfbf40f749b0 src/vim9compile.c
--- a/src/vim9compile.c Thu Jul 22 12:30:05 2021 +0200
+++ b/src/vim9compile.c Thu Jul 22 15:00:05 2021 +0200
@@ -1033,7 +1033,7 @@
10331033 int silent,
10341034 int actual_is_const)
10351035 {
1036- where_T where;
1036+ where_T where = WHERE_INIT;
10371037
10381038 if (expected == &t_bool && actual != &t_bool
10391039 && (actual->tt_flags & TTFLAG_BOOL_OK))
@@ -1045,7 +1045,6 @@
10451045 }
10461046
10471047 where.wt_index = arg_idx;
1048- where.wt_variable = FALSE;
10491048 if (check_type(expected, actual, FALSE, where) == OK)
10501049 return OK;
10511050
@@ -2804,10 +2803,8 @@
28042803 if (ppconst->pp_used > 0)
28052804 {
28062805 typval_T *tv = &ppconst->pp_tv[ppconst->pp_used - 1];
2807- where_T where;
2808-
2809- where.wt_index = 0;
2810- where.wt_variable = FALSE;
2806+ where_T where = WHERE_INIT;
2807+
28112808 return check_typval_type(&t_bool, tv, where);
28122809 }
28132810 return OK;
@@ -4822,12 +4819,10 @@
48224819 {
48234820 garray_T *stack = &cctx->ctx_type_stack;
48244821 type_T *actual;
4825- where_T where;
4822+ where_T where = WHERE_INIT;
48264823
48274824 generate_ppconst(cctx, ppconst);
48284825 actual = ((type_T **)stack->ga_data)[stack->ga_len - 1];
4829- where.wt_index = 0;
4830- where.wt_variable = FALSE;
48314826 if (check_type(want_type, actual, FALSE, where) == FAIL)
48324827 {
48334828 if (need_type(actual, want_type, -1, 0, cctx, FALSE, FALSE) == FAIL)
@@ -7975,7 +7970,7 @@
79757970 int vimvaridx = -1;
79767971 type_T *type = &t_any;
79777972 type_T *lhs_type = &t_any;
7978- where_T where;
7973+ where_T where = WHERE_INIT;
79797974
79807975 p = skip_var_one(arg, FALSE);
79817976 varlen = p - arg;
@@ -9325,7 +9320,7 @@
93259320 garray_T *stack = &cctx.ctx_type_stack;
93269321 type_T *val_type;
93279322 int arg_idx = first_def_arg + i;
9328- where_T where;
9323+ where_T where = WHERE_INIT;
93299324 int r;
93309325 int jump_instr_idx = instr->ga_len;
93319326 isn_T *isn;
@@ -9348,7 +9343,6 @@
93489343 // specified type.
93499344 val_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
93509345 where.wt_index = arg_idx + 1;
9351- where.wt_variable = FALSE;
93529346 if (ufunc->uf_arg_types[arg_idx] == &t_unknown)
93539347 {
93549348 did_set_arg_type = TRUE;
diff -r 6d48afa9f12d -r cfbf40f749b0 src/vim9execute.c
--- a/src/vim9execute.c Thu Jul 22 12:30:05 2021 +0200
+++ b/src/vim9execute.c Thu Jul 22 15:00:05 2021 +0200
@@ -730,15 +730,18 @@
730730 int idx;
731731 int did_emsg_before = did_emsg;
732732 ectx_T *prev_ectx = current_ectx;
733+ char *save_func_name = ectx->ec_where.wt_func_name;
733734
734735 if (call_prepare(argcount, argvars, ectx) == FAIL)
735736 return FAIL;
737+ ectx->ec_where.wt_func_name = internal_func_name(func_idx);
736738
737739 // Call the builtin function. Set "current_ectx" so that when it
738740 // recursively invokes call_def_function() a closure context can be set.
739741 current_ectx = ectx;
740742 call_internal_func_by_idx(func_idx, argvars, STACK_TV_BOT(-1));
741743 current_ectx = prev_ectx;
744+ ectx->ec_where.wt_func_name = save_func_name;
742745
743746 // Clear the arguments.
744747 for (idx = 0; idx < argcount; ++idx)
@@ -907,7 +910,7 @@
907910 else if (ufunc->uf_va_type != NULL)
908911 type = ufunc->uf_va_type->tt_member;
909912 if (type != NULL && check_typval_arg_type(type,
910- &argv[i], i + 1) == FAIL)
913+ &argv[i], NULL, i + 1) == FAIL)
911914 return FAIL;
912915 }
913916 }
@@ -4535,7 +4538,8 @@
45354538 {
45364539 if (ufunc->uf_arg_types != NULL && idx < ufunc->uf_args.ga_len
45374540 && check_typval_arg_type(
4538- ufunc->uf_arg_types[idx], &argv[idx], idx + 1) == FAIL)
4541+ ufunc->uf_arg_types[idx], &argv[idx],
4542+ NULL, idx + 1) == FAIL)
45394543 goto failed_early;
45404544 copy_tv(&argv[idx], STACK_TV_BOT(0));
45414545 }
@@ -4567,7 +4571,7 @@
45674571 for (idx = 0; idx < vararg_count; ++idx)
45684572 {
45694573 if (check_typval_arg_type(expected, &li->li_tv,
4570- argc + idx + 1) == FAIL)
4574+ NULL, argc + idx + 1) == FAIL)
45714575 goto failed_early;
45724576 li = li->li_next;
45734577 }
diff -r 6d48afa9f12d -r cfbf40f749b0 src/vim9type.c
--- a/src/vim9type.c Thu Jul 22 12:30:05 2021 +0200
+++ b/src/vim9type.c Thu Jul 22 15:00:05 2021 +0200
@@ -428,12 +428,16 @@
428428 }
429429
430430 int
431-check_typval_arg_type(type_T *expected, typval_T *actual_tv, int arg_idx)
431+check_typval_arg_type(
432+ type_T *expected,
433+ typval_T *actual_tv,
434+ char *func_name,
435+ int arg_idx)
432436 {
433- where_T where;
437+ where_T where = WHERE_INIT;
434438
435439 where.wt_index = arg_idx;
436- where.wt_variable = FALSE;
440+ where.wt_func_name = func_name;
437441 return check_typval_type(expected, actual_tv, where);
438442 }
439443
@@ -465,10 +469,9 @@
465469 void
466470 arg_type_mismatch(type_T *expected, type_T *actual, int arg_idx)
467471 {
468- where_T where;
472+ where_T where = WHERE_INIT;
469473
470474 where.wt_index = arg_idx;
471- where.wt_variable = FALSE;
472475 type_mismatch_where(expected, actual, where);
473476 }
474477
@@ -481,14 +484,23 @@
481484
482485 if (where.wt_index > 0)
483486 {
484- semsg(_(where.wt_variable
485- ? e_variable_nr_type_mismatch_expected_str_but_got_str
486- : e_argument_nr_type_mismatch_expected_str_but_got_str),
487+ if (where.wt_func_name == NULL)
488+ semsg(_(where.wt_variable
489+ ? e_variable_nr_type_mismatch_expected_str_but_got_str
490+ : e_argument_nr_type_mismatch_expected_str_but_got_str),
487491 where.wt_index, typename1, typename2);
492+ else
493+ semsg(_(where.wt_variable
494+ ? e_variable_nr_type_mismatch_expected_str_but_got_str_in_str
495+ : e_argument_nr_type_mismatch_expected_str_but_got_str_in_str),
496+ where.wt_index, typename1, typename2, where.wt_func_name);
488497 }
489- else
498+ else if (where.wt_func_name == NULL)
490499 semsg(_(e_type_mismatch_expected_str_but_got_str),
491500 typename1, typename2);
501+ else
502+ semsg(_(e_type_mismatch_expected_str_but_got_str_in_str),
503+ typename1, typename2, where.wt_func_name);
492504 vim_free(tofree1);
493505 vim_free(tofree2);
494506 }
@@ -604,7 +616,7 @@
604616 expected = type->tt_args[type->tt_argcount - 1]->tt_member;
605617 else
606618 expected = type->tt_args[i];
607- if (check_typval_arg_type(expected, &argvars[i], i + 1) == FAIL)
619+ if (check_typval_arg_type(expected, &argvars[i], NULL, i + 1) == FAIL)
608620 return FAIL;
609621 }
610622 return OK;
Show on old repository browser