diff options
author | Bram Moolenaar <Bram@vim.org> | 2020-07-19 14:03:09 +0200 |
---|---|---|
committer | Bram Moolenaar <Bram@vim.org> | 2020-07-19 14:03:09 +0200 |
commit | 65b9545f4494abcb455400c08e51de27bc305866 (patch) | |
tree | 092740eb6b516020e030a285cf7089ea496b33f1 | |
parent | 6434fc574dfbde11461e70e5a62712370edf38e6 (diff) | |
download | vim-git-65b9545f4494abcb455400c08e51de27bc305866.tar.gz |
patch 8.2.1242: Vim9: no error if calling a function with wrong typev8.2.1242
Problem: Vim9: no error if calling a function with wrong argument type.
Solution: Check types of arguments. (closes #6469)
-rw-r--r-- | src/proto/vim9compile.pro | 1 | ||||
-rw-r--r-- | src/testdir/test_vim9_func.vim | 11 | ||||
-rw-r--r-- | src/version.c | 2 | ||||
-rw-r--r-- | src/vim9compile.c | 44 | ||||
-rw-r--r-- | src/vim9execute.c | 3 |
5 files changed, 61 insertions, 0 deletions
diff --git a/src/proto/vim9compile.pro b/src/proto/vim9compile.pro index 03862a271..fb379239d 100644 --- a/src/proto/vim9compile.pro +++ b/src/proto/vim9compile.pro @@ -3,6 +3,7 @@ int check_defined(char_u *p, size_t len, cctx_T *cctx); void clear_type_list(garray_T *gap); type_T *typval2type(typval_T *tv); int check_type(type_T *expected, type_T *actual, int give_msg); +int check_argtype(type_T *expected, typval_T *actual_tv); int check_compare_types(exptype_T type, typval_T *tv1, typval_T *tv2); char_u *skip_type(char_u *start); type_T *parse_type(char_u **arg, garray_T *type_gap); diff --git a/src/testdir/test_vim9_func.vim b/src/testdir/test_vim9_func.vim index 9b61821a5..6d83ddc51 100644 --- a/src/testdir/test_vim9_func.vim +++ b/src/testdir/test_vim9_func.vim @@ -407,6 +407,17 @@ def Test_vim9script_call_fail_decl() delete('Xcall_decl.vim') enddef +def Test_vim9script_call_fail_type() + let lines =<< trim END + vim9script + def MyFunc(arg: string) + echo arg + enddef + MyFunc(1234) + END + CheckScriptFailure(lines, 'E1013: type mismatch, expected string but got number') +enddef + def Test_vim9script_call_fail_const() let lines =<< trim END vim9script diff --git a/src/version.c b/src/version.c index 2d0fbb2e4..2e8f4231d 100644 --- a/src/version.c +++ b/src/version.c @@ -755,6 +755,8 @@ static char *(features[]) = static int included_patches[] = { /* Add new patch number below this line */ /**/ + 1242, +/**/ 1241, /**/ 1240, diff --git a/src/vim9compile.c b/src/vim9compile.c index 634706b50..81d5324cc 100644 --- a/src/vim9compile.c +++ b/src/vim9compile.c @@ -560,6 +560,50 @@ check_type(type_T *expected, type_T *actual, int give_msg) return ret; } +/* + * Return FAIl if "expected" and "actual" don't match. + * TODO: better type comparison + */ + int +check_argtype(type_T *expected, typval_T *actual_tv) +{ + type_T actual; + type_T member; + + // TODO: should should be done with more levels + CLEAR_FIELD(actual); + actual.tt_type = actual_tv->v_type; + if (actual_tv->v_type == VAR_LIST + && actual_tv->vval.v_list != NULL + && actual_tv->vval.v_list->lv_first != NULL) + { + // Use the type of the first member, it is the most specific. + CLEAR_FIELD(member); + member.tt_type = actual_tv->vval.v_list->lv_first->li_tv.v_type; + member.tt_member = &t_any; + actual.tt_member = &member; + } + else if (actual_tv->v_type == VAR_DICT + && actual_tv->vval.v_dict != NULL + && actual_tv->vval.v_dict->dv_hashtab.ht_used > 0) + { + dict_iterator_T iter; + typval_T *value; + + // Use the type of the first value, it is the most specific. + dict_iterate_start(actual_tv, &iter); + dict_iterate_next(&iter, &value); + CLEAR_FIELD(member); + member.tt_type = value->v_type; + member.tt_member = &t_any; + actual.tt_member = &member; + } + else + actual.tt_member = &t_any; + return check_type(expected, &actual, TRUE); +} + + ///////////////////////////////////////////////////////////////////// // Following generate_ functions expect the caller to call ga_grow(). diff --git a/src/vim9execute.c b/src/vim9execute.c index 9f037a3be..7ba76e544 100644 --- a/src/vim9execute.c +++ b/src/vim9execute.c @@ -737,6 +737,9 @@ call_def_function( // Put arguments on the stack. for (idx = 0; idx < argc; ++idx) { + if (ufunc->uf_arg_types != NULL && idx < ufunc->uf_args.ga_len + && check_argtype(ufunc->uf_arg_types[idx], &argv[idx]) == FAIL) + goto failed_early; copy_tv(&argv[idx], STACK_TV_BOT(0)); ++ectx.ec_stack.ga_len; } |