To: vim_dev@googlegroups.com Subject: Patch 8.2.1242 Fcc: outbox From: Bram Moolenaar Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ------------ Patch 8.2.1242 Problem: Vim9: no error if calling a function with wrong argument type. Solution: Check types of arguments. (closes #6469) Files: src/vim9compile.c, src/proto/vim9compile.pro, src/vim9execute.c, src/testdir/test_vim9_func.vim *** ../vim-8.2.1241/src/vim9compile.c 2020-07-18 15:16:58.307407071 +0200 --- src/vim9compile.c 2020-07-19 13:48:05.826255120 +0200 *************** *** 560,565 **** --- 560,609 ---- 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(). *** ../vim-8.2.1241/src/proto/vim9compile.pro 2020-07-10 22:45:35.029135161 +0200 --- src/proto/vim9compile.pro 2020-07-18 23:09:23.510122352 +0200 *************** *** 3,8 **** --- 3,9 ---- 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); *** ../vim-8.2.1241/src/vim9execute.c 2020-07-18 18:12:57.758647551 +0200 --- src/vim9execute.c 2020-07-19 13:24:13.065764469 +0200 *************** *** 737,742 **** --- 737,745 ---- // 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; } *** ../vim-8.2.1241/src/testdir/test_vim9_func.vim 2020-07-17 20:35:00.857574357 +0200 --- src/testdir/test_vim9_func.vim 2020-07-19 14:00:46.532305475 +0200 *************** *** 407,412 **** --- 407,423 ---- 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 *** ../vim-8.2.1241/src/version.c 2020-07-18 22:24:18.630245010 +0200 --- src/version.c 2020-07-18 23:05:17.122848585 +0200 *************** *** 756,757 **** --- 756,759 ---- { /* Add new patch number below this line */ + /**/ + 1242, /**/ -- Despite the cost of living, have you noticed how it remains so popular? /// Bram Moolenaar -- Bram@Moolenaar.net -- http://www.Moolenaar.net \\\ /// sponsor Vim, vote for features -- http://www.Vim.org/sponsor/ \\\ \\\ an exciting new programming language -- http://www.Zimbu.org /// \\\ help me help AIDS victims -- http://ICCF-Holland.org ///