To: vim_dev@googlegroups.com Subject: Patch 8.2.1176 Fcc: outbox From: Bram Moolenaar Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ------------ Patch 8.2.1176 Problem: Vim9: not enough type checking in Vim9 script. Solution: Use same type checking as in a :def function. Files: src/vim9compile.c, src/proto/vim9compile.pro, src/eval.c, src/testdir/test_vim9_expr.vim *** ../vim-8.2.1175/src/vim9compile.c 2020-07-10 21:50:37.792847476 +0200 --- src/vim9compile.c 2020-07-10 22:13:34.094011585 +0200 *************** *** 820,825 **** --- 820,833 ---- return isntype; } + int + check_compare_types(exptype_T type, typval_T *tv1, typval_T *tv2) + { + if (get_compare_isn(type, tv1->v_type, tv2->v_type) == ISN_DROP) + return FAIL; + return OK; + } + /* * Generate an ISN_COMPARE* instruction with a boolean result. */ *************** *** 4296,4302 **** // Both sides are a constant, compute the result now. // First check for a valid combination of types, this is more // strict than typval_compare(). ! if (get_compare_isn(type, tv1->v_type, tv2->v_type) == ISN_DROP) ret = FAIL; else { --- 4304,4310 ---- // Both sides are a constant, compute the result now. // First check for a valid combination of types, this is more // strict than typval_compare(). ! if (check_compare_types(type, tv1, tv2) == FAIL) ret = FAIL; else { *** ../vim-8.2.1175/src/proto/vim9compile.pro 2020-07-08 19:35:17.767686401 +0200 --- src/proto/vim9compile.pro 2020-07-10 22:14:05.805930961 +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_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); char *vartype_name(vartype_T type); *** ../vim-8.2.1175/src/eval.c 2020-07-08 22:01:43.796114663 +0200 --- src/eval.c 2020-07-10 22:19:21.341118293 +0200 *************** *** 2460,2467 **** } if (evalarg != NULL && (evalarg->eval_flags & EVAL_EVALUATE)) { ! int ret = typval_compare(rettv, &var2, type, ic); clear_tv(&var2); return ret; } --- 2460,2475 ---- } if (evalarg != NULL && (evalarg->eval_flags & EVAL_EVALUATE)) { ! int ret; + if (in_vim9script() && check_compare_types( + type, rettv, &var2) == FAIL) + { + ret = FAIL; + clear_tv(rettv); + } + else + ret = typval_compare(rettv, &var2, type, ic); clear_tv(&var2); return ret; } *** ../vim-8.2.1175/src/testdir/test_vim9_expr.vim 2020-07-10 21:50:37.792847476 +0200 --- src/testdir/test_vim9_expr.vim 2020-07-10 22:24:43.967991894 +0200 *************** *** 557,563 **** enddef def Test_expr4_vimscript() ! " only checks line continuation let lines =<< trim END vim9script let var = 0 --- 557,563 ---- enddef def Test_expr4_vimscript() ! " check line continuation let lines =<< trim END vim9script let var = 0 *************** *** 599,604 **** --- 599,623 ---- assert_equal(1, var) END CheckScriptSuccess(lines) + + " spot check mismatching types + lines =<< trim END + vim9script + echo '' == 0 + END + CheckScriptFailure(lines, 'E1072:') + + lines =<< trim END + vim9script + echo v:true > v:false + END + CheckScriptFailure(lines, 'Cannot compare bool with bool') + + lines =<< trim END + vim9script + echo 123 is 123 + END + CheckScriptFailure(lines, 'Cannot use "is" with number') enddef func Test_expr4_fails() *** ../vim-8.2.1175/src/version.c 2020-07-10 21:50:37.792847476 +0200 --- src/version.c 2020-07-10 22:15:15.289753590 +0200 *************** *** 756,757 **** --- 756,759 ---- { /* Add new patch number below this line */ + /**/ + 1176, /**/ -- A)bort, R)etry, P)lease don't bother me again /// 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 ///