]> git.ipfire.org Git - thirdparty/vim.git/commitdiff
patch 8.2.1176: Vim9: not enough type checking in Vim9 script v8.2.1176
authorBram Moolenaar <Bram@vim.org>
Fri, 10 Jul 2020 20:45:38 +0000 (22:45 +0200)
committerBram Moolenaar <Bram@vim.org>
Fri, 10 Jul 2020 20:45:38 +0000 (22:45 +0200)
Problem:    Vim9: not enough type checking in Vim9 script.
Solution:   Use same type checking as in a :def function.

src/eval.c
src/proto/vim9compile.pro
src/testdir/test_vim9_expr.vim
src/version.c
src/vim9compile.c

index 6a1bc4c7a0992ec8af47851dc33d0e2aef8c1859..595cd06663946b9f7e535642cee903ace69cf811 100644 (file)
@@ -2460,8 +2460,16 @@ eval4(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
        }
        if (evalarg != NULL && (evalarg->eval_flags & EVAL_EVALUATE))
        {
-           int ret = typval_compare(rettv, &var2, type, ic);
+           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;
        }
index 5486bd923624f0eb86a37a156c932b1ee9981f42..03862a271ba5207de50b4569c5eb1819a89fed7e 100644 (file)
@@ -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_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);
index 1f4bde9123f94fb628cf68342306a8e9e2e51965..ef56a671118f9358069d156c4ac81b3c2561e3d3 100644 (file)
@@ -557,7 +557,7 @@ def RetVoid()
 enddef
 
 def Test_expr4_vimscript()
-  " only checks line continuation
+  " check line continuation
   let lines =<< trim END
       vim9script
       let var = 0
@@ -599,6 +599,25 @@ def Test_expr4_vimscript()
       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()
index ac5425ddd5c2f416813f51e45d6b421ff26dff3b..6c6211da1aa97df474720d540fea4173559349b9 100644 (file)
@@ -754,6 +754,8 @@ static char *(features[]) =
 
 static int included_patches[] =
 {   /* Add new patch number below this line */
+/**/
+    1176,
 /**/
     1175,
 /**/
index 3c0336ac4ae68c1192b46ed43181d73a6f861ff3..e7f90e1c7fe88d29acf7de9a805adb0f8559465a 100644 (file)
@@ -820,6 +820,14 @@ get_compare_isn(exptype_T exptype, vartype_T type1, vartype_T type2)
     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,7 +4304,7 @@ compile_expr4(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
            // 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)
+           if (check_compare_types(type, tv1, tv2) == FAIL)
                ret = FAIL;
            else
            {