To: vim_dev@googlegroups.com Subject: Patch 8.2.0973 Fcc: outbox From: Bram Moolenaar Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ------------ Patch 8.2.0973 Problem: Vim9: type is not checked when assigning to a script variable. Solution: Check the type. Files: src/evalvars.c, src/vim9script.c, src/proto/vim9script.pro, src/vim9compile.c, src/proto/vim9compile.pro, src/testdir/test_vim9_script.vim *** ../vim-8.2.0972/src/evalvars.c 2020-06-13 18:09:16.096199169 +0200 --- src/evalvars.c 2020-06-13 18:51:21.277408917 +0200 *************** *** 2883,2894 **** || var_check_lock(di->di_tv.v_lock, name, FALSE)) return; ! if ((flags & LET_NO_COMMAND) == 0 ! && is_script_local ! && current_sctx.sc_version == SCRIPT_VERSION_VIM9) { ! semsg(_("E1041: Redefining script item %s"), name); ! return; } } else --- 2883,2899 ---- || var_check_lock(di->di_tv.v_lock, name, FALSE)) return; ! if (is_script_local ! && current_sctx.sc_version == SCRIPT_VERSION_VIM9) { ! if ((flags & LET_NO_COMMAND) == 0) ! { ! semsg(_("E1041: Redefining script item %s"), name); ! return; ! } ! ! // check the type ! check_script_var_type(&di->di_tv, tv, name); } } else *** ../vim-8.2.0972/src/vim9script.c 2020-06-13 18:09:16.096199169 +0200 --- src/vim9script.c 2020-06-13 18:51:36.705343801 +0200 *************** *** 488,492 **** --- 488,517 ---- return p; } + /* + * Check if the type of script variable "dest" allows assigning "value". + */ + void + check_script_var_type(typval_T *dest, typval_T *value, char_u *name) + { + scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid); + int idx; + + // Find the svar_T in sn_var_vals. + for (idx = 0; idx < si->sn_var_vals.ga_len; ++idx) + { + svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx; + + if (sv->sv_tv == dest) + { + if (sv->sv_const) + semsg(_(e_readonlyvar), name); + else + check_type(sv->sv_type, typval2type(value), TRUE); + return; + } + } + iemsg("check_script_var_type(): not found"); + } #endif // FEAT_EVAL *** ../vim-8.2.0972/src/proto/vim9script.pro 2020-06-13 18:09:16.096199169 +0200 --- src/proto/vim9script.pro 2020-06-13 18:51:28.085380211 +0200 *************** *** 7,10 **** --- 7,11 ---- int find_exported(int sid, char_u **argp, int *name_len, ufunc_T **ufunc, type_T **type); char_u *handle_import(char_u *arg_start, garray_T *gap, int import_sid, void *cctx); char_u *vim9_declare_scriptvar(exarg_T *eap, char_u *arg); + void check_script_var_type(typval_T *dest, typval_T *value, char_u *name); /* vim: set ft=c : */ *** ../vim-8.2.0972/src/vim9compile.c 2020-06-13 18:09:16.096199169 +0200 --- src/vim9compile.c 2020-06-13 18:50:39.829588746 +0200 *************** *** 139,145 **** static void delete_def_function_contents(dfunc_T *dfunc); static void arg_type_mismatch(type_T *expected, type_T *actual, int argidx); - static int check_type(type_T *expected, type_T *actual, int give_msg); /* * Lookup variable "name" in the local scope and return it. --- 139,144 ---- *************** *** 461,467 **** /* * Return the type_T for a typval. Only for primitive types. */ ! static type_T * typval2type(typval_T *tv) { if (tv->v_type == VAR_NUMBER) --- 460,466 ---- /* * Return the type_T for a typval. Only for primitive types. */ ! type_T * typval2type(typval_T *tv) { if (tv->v_type == VAR_NUMBER) *************** *** 504,510 **** * Check if the expected and actual types match. * Does not allow for assigning "any" to a specific type. */ ! static int check_type(type_T *expected, type_T *actual, int give_msg) { int ret = OK; --- 503,509 ---- * Check if the expected and actual types match. * Does not allow for assigning "any" to a specific type. */ ! int check_type(type_T *expected, type_T *actual, int give_msg) { int ret = OK; *** ../vim-8.2.0972/src/proto/vim9compile.pro 2020-05-24 23:45:20.525386105 +0200 --- src/proto/vim9compile.pro 2020-06-13 18:49:54.701786302 +0200 *************** *** 1,5 **** --- 1,7 ---- /* vim9compile.c */ int check_defined(char_u *p, int len, cctx_T *cctx); + type_T *typval2type(typval_T *tv); + int check_type(type_T *expected, type_T *actual, int give_msg); char_u *skip_type(char_u *start); type_T *parse_type(char_u **arg, garray_T *type_gap); char *vartype_name(vartype_T type); *** ../vim-8.2.0972/src/testdir/test_vim9_script.vim 2020-06-13 18:09:16.096199169 +0200 --- src/testdir/test_vim9_script.vim 2020-06-13 18:53:30.000863464 +0200 *************** *** 1831,1836 **** --- 1831,1845 ---- unlet g:var_test enddef + def Test_let_type_check() + let lines =<< trim END + vim9script + let var: string + var = 1234 + END + CheckScriptFailure(lines, 'E1013:') + enddef + def Test_forward_declaration() let lines =<< trim END vim9script *** ../vim-8.2.0972/src/version.c 2020-06-13 18:09:16.100199150 +0200 --- src/version.c 2020-06-13 18:40:47.584172679 +0200 *************** *** 756,757 **** --- 756,759 ---- { /* Add new patch number below this line */ + /**/ + 973, /**/ -- ARTHUR: Well, I AM king... DENNIS: Oh king, eh, very nice. An' how'd you get that, eh? By exploitin' the workers -- by 'angin' on to outdated imperialist dogma which perpetuates the economic an' social differences in our society! If there's ever going to be any progress-- The Quest for the Holy Grail (Monty Python) /// 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 ///